JDBC查询程序没错控制台不出结果

来源:百度知道 编辑:UC知道 时间:2024/05/30 11:00:13
package com.lh.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.lh.dto.Student;
import com.lh.util.DBManager;

public class StudentDao {

Connection conn = DBManager.getConn();
Statement st = null;
PreparedStatement prst = null;
ResultSet rs = null;

public List<Student> show() {

List<Student> list = new ArrayList<Student>();
String sql = "select * from user";

try {

st = conn.createStatement();
rs = st.executeQuery(sql);
while (rs.next()) {
Student s = new Student(rs.getInt("id"), rs.getString("name"),
rs.getInt("age"), rs.getFloat("score"));
list.add(s);

问题出在show()方法里,

之所有没有输出是因为你没有打印List中的值
StudentDao sd = new StudentDao();
List<Student> list = sd.show();

for(int i=0;i<list.size();i++){
System.out.println(list.get(i).实体);
}

加上这句就行了