【已解决】数据库中有数据,但是JDBC查询出来的为null

【已解决】数据库中有数据,但是JDBC查询出来的为null

​ 当我遇到这个问题的时候,我的第一反应就是上CSDN找答案,可是大多数的博客都解释说可能是数据表的字段名和实体类的属性名不一致,例如:表字段名为user_id,那么实体类的属性名就应该是userId,即使用驼峰这种写法。但是我的表字段根本就不存在这样的问题,如下图:

image-20220516185548029

字段名都不包含下划线这种结构,所以排除这种可能。那就纳了闷了?怎么查询就为空呢?这时我在翻笔记的时候,忽然注意到一张关于SQL数据类型与Java数据类型的转换这样一张表,它长这样:

Java类型SQL类型
booleanBIT
byteTINYINT
shortSMALLINT
intINTEGER
longBIGINT
StringCHAR、VARCHAR、LONGVARCHAR
byte arrayBINARY、VAR BINARY
java.sql.DateDATE
java.sql.TimeTIME
java.sql.TimestampTIMESTAMP

那有没有可能是我在遍历ResultSet集合的时候,没有注意到SQL类型和Java类型的转换呢?哇靠,还真是!

​ 我的实体类长这样:

public class smbmsUser {
    private Integer id; // id
    private String userCode; // 用户编码
    private String userName; // 用户名称
    private String userPassword; // 用户密码
    private Integer gender; // 性别
    private Date birthday; // 出生日期
    private String phone; // 电话
    private String address; // 地址
    private Integer userRole; // 用户角色
    private Integer createdBy; // 创建者
    private Date creationDate; // 创建时间
    private Integer modifyBy; // 更新者
    private Date modifyDate; // 更新时间
    private String idPicPath; // 证件照路径
    private String workPicPath; // 工作证照片路径


    public smbmsUser() {
    }
	
    public smbmsUser(Integer id, String userCode, String userName, String userPassword, Integer gender, Date birthday, String phone, String address, Integer userRole, Integer createdBy, Date creationDate, Integer modifyBy, Date modifyDate, String idPicPath, String workPicPath) {
        this.id = id;
        this.userCode = userCode;
        this.userName = userName;
        this.userPassword = userPassword;
        this.gender = gender;
        this.birthday = birthday;
        this.phone = phone;
        this.address = address;
        this.userRole = userRole;
        this.createdBy = createdBy;
        this.creationDate = creationDate;
        this.modifyBy = modifyBy;
        this.modifyDate = modifyDate;
        this.idPicPath = idPicPath;
        this.workPicPath = workPicPath;
    }




    @Override
    public String toString() {
        return "smbmsUser{" +
                "id=" + id +
                ", userCode='" + userCode + '\'' +
                ", userName='" + userName + '\'' +
                ", userPassword='" + userPassword + '\'' +
                ", gender=" + gender +
                ", birthday=" + birthday +
                ", phone='" + phone + '\'' +
                ", address='" + address + '\'' +
                ", userRole=" + userRole +
                ", createdBy=" + createdBy +
                ", creationDate=" + creationDate +
                ", modifyBy=" + modifyBy +
                ", modifyDate=" + modifyDate +
                ", idPicPath='" + idPicPath + '\'' +
                ", workPicPath='" + workPicPath + '\'' +
                '}';
    }
}

emmm,有点小长,再来看看遍历ResultSet集合时候使用的方法:

//遍历结果集
        while (rs.next()){
        
            String id = rs.getString(1);
            String userCode = rs.getString(2);
            String userName = rs.getString(3);
            String userPassword = rs.getString(4);
            String gender = rs.getString(5);
            String birthday = rs.getString(6);
            String phone = rs.getString(7);
            String address = rs.getString(8);
            String userRole = rs.getString(9);
            String createdBy = rs.getString(10);
            String creationDate = rs.getString(11);
            String modifyBy = rs.getString(12);
            String modifyDate = rs.getString(13);
            String idPicPath = rs.getString(14);
            String workPicPath = rs.getString(15);
                   
            smbmsUser smbmsUser = new smbmsUser(id, userCode, userName, userPassword, gender, birthday, phone, address, userRole, createdBy, creationDate, modifyBy, modifyDate, idPicPath, workPicPath);
            System.out.println(smbmsUser);

        }

当时为了快点,直接复制粘贴,方法都是清一色的getString(),好家伙,现在看我直接好家伙。上面构造器中的参数类型有Integer,Java.sql.Date,下面我就new对象就new了个寂寞,数据类型不一样,参数肯定传不进去啊!于是我把方法逐个修改过来,变成这样(和上面的属性对应即可):

 while (rs.next()){
            Integer id = rs.getInt(1);
            String userCode = rs.getString(2);
            String userName = rs.getString(3);
            String userPassword = rs.getString(4);
            Integer gender = rs.getInt(5);
            Date birthday = rs.getDate(6);
            String phone = rs.getString(7);
            String address = rs.getString(8);
            Integer userRole = rs.getInt(9);
            Integer createdBy = rs.getInt(10);
            Date creationDate = rs.getDate(11);
            Integer modifyBy = rs.getInt(12);
            Date modifyDate = rs.getDate(13);
            String idPicPath = rs.getString(14);
            String workPicPath = rs.getString(15);

           
            


            smbmsUser smbmsUser = new smbmsUser(id, userCode, userName, userPassword, gender, birthday, phone, address, userRole, createdBy, creationDate, modifyBy, modifyDate, idPicPath, workPicPath);
            System.out.println(smbmsUser);

        }

再来测试,就有数据了!记录一下,以免再此入坑。

image-20220516191521503

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
在 IDEA 中使用 JDBC 创建数据库表和实现增删改查等操作需要以下步骤: 1. 配置数据库连接 首先需要在 IDEA 中配置数据库连接,可以通过菜单栏的 "View" -> "Tool Windows" -> "Database" 打开数据库工具窗口,然后在窗口中点击加号按钮,选择 "Data source" -> "MySQL",填写数据库连接信息,包括数据库地址、端口号、用户名和密码等。 2. 创建 JavaBean 类 创建一个名为 User 的 JavaBean 类,表示要在数据库中创建的数据表。 ```java public class User { private int id; private String name; private String password; private int age; // getter 和 setter 方法省略 } ``` 3. 创建数据表 在 IDEA 中使用 JDBC 创建数据表需要编写 SQL 语句,可以使用以下代码创建名为 user 的数据表: ```java Connection conn = null; Statement stmt = null; try { conn = dataSource.getConnection(); // 获取数据库连接 stmt = conn.createStatement(); // 创建 Statement 对象 String sql = "CREATE TABLE IF NOT EXISTS user (" + "id INT(11) NOT NULL AUTO_INCREMENT," + "name VARCHAR(50) NOT NULL," + "password VARCHAR(50) NOT NULL," + "age INT(11)," + "PRIMARY KEY (id))"; stmt.executeUpdate(sql); // 执行 SQL 语句 } catch (SQLException e) { e.printStackTrace(); } finally { try { if (stmt != null) { stmt.close(); // 关闭 Statement 对象 } if (conn != null) { conn.close(); // 关闭数据库连接 } } catch (SQLException e) { e.printStackTrace(); } } ``` 以上代码中,首先通过 dataSource.getConnection() 方法获取数据库连接,然后创建 Statement 对象并编写 SQL 语句,使用 executeUpdate() 方法执行 SQL 语句创建数据表。最后需要在 finally 块中关闭 Statement 对象和数据库连接。 4. 测试创建数据表 可以使用以下代码测试是否成功创建数据表: ```java Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = dataSource.getConnection(); // 获取数据库连接 stmt = conn.createStatement(); // 创建 Statement 对象 String sql = "SELECT * FROM user"; rs = stmt.executeQuery(sql); // 执行查询语句 ResultSetMetaData metaData = rs.getMetaData(); // 获取结果集元数据 int columnCount = metaData.getColumnCount(); // 获取结果集列数 for (int i = 1; i <= columnCount; i++) { System.out.print(metaData.getColumnName(i) + "\t"); // 输出列名 } System.out.println(); while (rs.next()) { System.out.print(rs.getInt("id") + "\t"); System.out.print(rs.getString("name") + "\t"); System.out.print(rs.getString("password") + "\t"); System.out.print(rs.getInt("age") + "\t"); System.out.println(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); // 关闭 ResultSet 对象 } if (stmt != null) { stmt.close(); // 关闭 Statement 对象 } if (conn != null) { conn.close(); // 关闭数据库连接 } } catch (SQLException e) { e.printStackTrace(); } } ``` 以上代码中,首先执行查询语句获取数据表的所有记录,然后使用 ResultSetMetaData 获取结果集的元数据,输出列名和每条记录的具体值。如果执行成功,则说明已经成功创建了名为 user 的数据表。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Steph Wae

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值