weixin_44384586
洋盘2号
采纳率0%
2019-11-20 11:13 浏览 698

java连接数据库返回数据为空

package servlet;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Demo2 {
/*
查询所有student对象
*/
public static void main(String[] args){
List list = new Demo2().findAll();
System.out.println(list);
}
public List findAll(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List list = null;
try {
//注册驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/kaorou","root", "root");
//定义sql
String sql = "select * from user ";
//获取执行sql的对象
stmt = conn.createStatement();
//执行sql
rs = stmt.executeQuery(sql);
//便利结果集,封装对象,装载集合
Student stu = null;
list = new ArrayList();
while(rs.next()){ //rs如果有下一个就循环,括号中的参数与数据库中的字段名一致
int id = rs.getInt("id");
String name = rs.getString("name");
int email = rs.getInt("email");
int age = rs.getInt("age");

            //创建stu对象
            stu = new Student();
            stu.getId(id);
            stu.getName(name);
            stu.getEmail(email);
            stu.getAge(age);


            //装载集合
             list.add(stu);
         }

     } catch (ClassNotFoundException | SQLException e) {
         e.printStackTrace();
     }finally { //先声明的后关
         if (rs != null) {
             try {
                 rs.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
         }
         if (stmt != null){
             try {
                 stmt.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
         }
         if (conn != null){
             try {
                 conn.close();
             } catch (SQLException e) {
                 e.printStackTrace();
             }
         }
     }
     return list;
 }

}

package servlet;
/*
封装student表数据
*/
public class Student {
private int id;
private String name;
private int email;
private int age;

public int getId(int id) {
    return this.id;
}

public void setId(int id) {
    this.id = id;
}

public void getName(String name) {
}

public void setName(String name) {
    this.name = name;
}

public int getEmail(int email) {
    return this.email;
}

public void setEmail(int email) {
    this.email = email;
}

public int getAge(int age) {
    return this.age;
}

public void setAge(int age) {
    this.age = age;
}


@Override
public String toString() {
    return "Student{" +
            "id=" + id +
            ", name='" + name + '\'' +
            ", sex='" + email + '\'' +
            ", age=" + age +

            '}';
}

}

控制台报错:
[Student{id=0, name='null', sex='0', age=0}, Student{id=0, name='null', sex='0', age=0}, Student{id=0, name='null', sex='0', age=0}, Student{id=0, name='null', sex='0', age=0}, Student{id=0, name='null', sex='0', age=0}]

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答 默认 最新

  • zml_moxueli 呓语的风 2019-11-20 11:23

    这不是已经查询出来了吗,有5条记录啊

    点赞 评论 复制链接分享
  • zhshchilss zhshchilss 2019-11-20 11:43

    int id = rs.getInt("id");
    String name = rs.getString("name");
    int email = rs.getInt("email");
    int age = rs.getInt("age");
    打个断点跟一下,看rs里面有没有这些字段

    点赞 评论 复制链接分享
  • sinat_19671783 cap_jack_sp 2019-11-20 13:34
    int id = rs.getInt("id");
    String name = rs.getString("name");
    int email = rs.getInt("email");
    int age = rs.getInt("age");
    

    看你数据库里的列名是否与取值一致,还有email是int类型的?

    点赞 评论 复制链接分享
  • lppzyt lppzyt 2019-11-20 13:56

    创建stu对象应该是set属性值。 下面的代码相当于new 了一个空的student。然后你又加到list集合中。所以返回的list中的都是未赋值的student对象。
    stu = new Student();
    stu.getId(id);
    stu.getName(name);
    stu.getEmail(email);
    stu.getAge(age);
    //装载集合
    list.add(stu);

    点赞 评论 复制链接分享

相关推荐