package homework7_二_1_20221548; import java.sql.*; import javax.swing.JOptionPane; public class Demo { public static void main(String args[]) { String url = "jdbc:mysql://127.0.0.1:3306/student"; String user = "root", pwd = ""; Connection con; Statement sql; ResultSet rs; try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("test" + e); } try { con = DriverManager.getConnection(url, user, pwd); sql = con.createStatement(); String sqlStatement; sqlStatement = "SELECT * From message order by height desc"; rs = sql.executeQuery(sqlStatement); while (rs.next()) { // String number = rs.getString("number"); // double height = rs.getDouble("height"); // Date birth = rs.getDate("birthday"); // String name = rs.getString("name"); // System.out.println(number + "," + name + "," + birth + "," + height); double customerID = rs.getDouble("customerID"); String name = rs.getString("name"); double phone = rs.getDouble("phone"); System.out.println(customerID + " " + name + " " + phone); } con.close(); } catch (SQLException e) { System.out.println("数据库操作失败" + e); } } }为什么数不出结果?
时间: 2024-02-14 10:11:17 浏览: 119
这段代码中存在一些问题,可能导致无法正确输出结果。首先,代码中的 SQL 语句是查询 message 表,但是在代码注释中提到的字段(number、height、birthday、name)并不属于 message 表,因此需要确认查询的表名和列名是否正确。其次,代码中的输出语句只打印了部分字段,如果需要输出所有字段,需要把注释掉的部分代码恢复。最后,如果无法输出结果,可能是数据库连接出现问题,需要检查数据库连接的用户名、密码、数据库名、表名等信息是否正确。可以尝试打印出异常信息,以便更好地定位问题。
阅读全文