会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132647个问题
JAVA 全系列/第三阶段:数据库编程/MySQL数据库 137楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 138楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 140楼

package com.liu.jdbc;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

public class PreparedStatementTest4 {
    public static void main(String[] args) throws Exception{
        String sql="select id from scott.users_info where id<?";
        List<Users> u=queryList(sql,5);
        System.out.println();
    }
    public static List<Users> queryList(String sql,Object...args) throws Exception{
        Connection conn=JdbcUtils.getConnection();
        PreparedStatement ps=conn.prepareStatement(sql);
        for(int i=0;i< args.length;i++){
            ps.setObject(i+1,args[i]);
        }
        List<Users> users=new ArrayList<>();
        ResultSet rs=ps.executeQuery();
        ResultSetMetaData rsmd=ps.getMetaData();
        int columnCnt=rsmd.getColumnCount();
        while (rs.next()){
            Users u=new Users();
            for (int i=0;i<columnCnt;i++){
                Object value=rs.getObject(i+1);
                System.out.println(value);
                String columnName=rsmd.getColumnName(i+1).toLowerCase();
                Field field=u.getClass().getDeclaredField(columnName);
                System.out.println(columnName);
                field.setAccessible(true);
                field.set(u,value);
            }
            users.add(u);
        }
        JdbcUtils.close(conn,ps);
        rs.close();
        return users;
    }
}

我连接的是oracle数据库,为什么对id赋值会报这个错误呢?String类型的就没问题1642413962(1).jpg

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 141楼

封装jdbc

package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class jdbcUtil {
    //获取connection对象
    //关闭connection
    //关闭statement对象
    private static String driver="com.mysql.jdbc.Driver";
    private static String connectionUrl="jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8";
    private static String username="root";
    private  static String psw="123456";

    static {
        //读取propertile
        /*ResourceBundle  bundle=ResourceBundle.getBundle("jdbc");
        driver=bundle.getString("driver");
        connectionUrl=bundle.getString("connectionUrl");
        username=bundle.getString("username");
        psw=bundle.getString("psw");*/
        
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection conn=null;
        try {
            conn= DriverManager.getConnection(connectionUrl,username,psw);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void closeStatement(Statement state){
        if(state!=null){
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeConnection(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

测试

package jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
public class testJdbc {
    public void insertTable(){
        //驱动注册
        Connection conn=null;
        Statement state=null;
        try {
            conn=jdbcUtil.getConnection();
            String str="insert into usertable values(4,'zhao','123','123@1qq')";
            state=conn.createStatement();
            int flag=state.executeUpdate(str);
            System.out.print(flag);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            jdbcUtil.closeConnection(conn);
            jdbcUtil.closeStatement(state);
        }

    }
    public static void main(String[] args) {
        testJdbc test=new testJdbc();
        test.insertTable();

    }
}

不用读取properties,运行如下

image.png

我用读取properties文件

properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.connectionUrl=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.psw=123456
package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

public class jdbcUtil {
    //获取connection对象
    //关闭connection
    //关闭statement对象
    private static String driver";
    private static String connectionUrl;
    private  static String psw;

    static {
        //读取propertile
        ResourceBundle  bundle=ResourceBundle.getBundle("jdbc");
        driver=bundle.getString("driver");
        connectionUrl=bundle.getString("connectionUrl");
        username=bundle.getString("username");
        psw=bundle.getString("psw");
        
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection(){
        Connection conn=null;
        try {
            conn= DriverManager.getConnection(connectionUrl,username,psw);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void closeStatement(Statement state){
        if(state!=null){
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public static void closeConnection(Connection conn){
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

测试类代码没变就改了插入的数据,结果运行如下

image.png

我上网查了原因,没有找到加载该类,初始化没有成功,有些博客说在properties文件中加上jdbc.我加了还是不对

image.png

这是为啥

JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 142楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 144楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 145楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术 146楼
JAVA 全系列/第三阶段:数据库编程/SQL 语言 147楼
JAVA 全系列/第三阶段:数据库编程/Oracle 数据库的使用 149楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637