JAVA问题,我想返回数据表中的所有信息,应该如何修改?
数据表中有Sno,Sname,Saddr,Sclass,Sphone五个属性,通过Sno查询出其他4个属性,目前源程序在下面能够查找是不是有这个学生,想修改下能返回这5个...
数据表中有Sno,Sname ,Saddr,Sclass,Sphone 五个属性,通过Sno 查询出其他4个属性,目前源程序在下面能够查找 是不是有这个学生,想修改下 能返回这5个属性,求大神赐教
package chaxungeren; //全表查询
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class chaxungeren {
public static void main(String[] args) {
String url = "jdbc:microsoft:sqlserver:"
+ "//localhost:1433;databasename=student manager";
String user = "sa";
String pwd = "123123";
String driverClassName
= "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "SELECT * FROM Sinfo "
+ " WHERE Sno = ?";
ResultSet rs = null;
boolean isNotEmpty = false;
String inputSno = "";
System.out.println("请输入查询学生的学号:");
inputSno = (new Scanner(System.in)).next();
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(url, user, pwd);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, inputSno);
rs = pstmt.executeQuery();
isNotEmpty = rs.next();
if (isNotEmpty) {
// 用户名存在,需要判断密码是否正确
if (inputSno.equals(rs.getString("Sno"))) {
System.out.println("查询成功!");
}
else {
System.out.println("查询错误!");
}
}
else {
System.out.println("学生不存在:" + inputSno);
}
}
catch (ClassNotFoundException e) {
System.err.println("没找到JDBC驱动程序!");
}
catch (SQLException se) {
System.err.println("产生了SQLException!");
System.err.println(se.getMessage());
}
finally {
try {
rs.close();
// stmt cannot be resolved
pstmt.close();
conn.close();
}
catch (SQLException se) {
}
}
}
} 展开
package chaxungeren; //全表查询
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class chaxungeren {
public static void main(String[] args) {
String url = "jdbc:microsoft:sqlserver:"
+ "//localhost:1433;databasename=student manager";
String user = "sa";
String pwd = "123123";
String driverClassName
= "com.microsoft.jdbc.sqlserver.SQLServerDriver";
Connection conn = null;
PreparedStatement pstmt = null;
String sql = "SELECT * FROM Sinfo "
+ " WHERE Sno = ?";
ResultSet rs = null;
boolean isNotEmpty = false;
String inputSno = "";
System.out.println("请输入查询学生的学号:");
inputSno = (new Scanner(System.in)).next();
try {
Class.forName(driverClassName);
conn = DriverManager.getConnection(url, user, pwd);
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, inputSno);
rs = pstmt.executeQuery();
isNotEmpty = rs.next();
if (isNotEmpty) {
// 用户名存在,需要判断密码是否正确
if (inputSno.equals(rs.getString("Sno"))) {
System.out.println("查询成功!");
}
else {
System.out.println("查询错误!");
}
}
else {
System.out.println("学生不存在:" + inputSno);
}
}
catch (ClassNotFoundException e) {
System.err.println("没找到JDBC驱动程序!");
}
catch (SQLException se) {
System.err.println("产生了SQLException!");
System.err.println(se.getMessage());
}
finally {
try {
rs.close();
// stmt cannot be resolved
pstmt.close();
conn.close();
}
catch (SQLException se) {
}
}
}
} 展开
展开全部
您好,1.首先DBConn.java类中的public void ListUser()返回值是空,应该返回一个list才对
修改如下:
Java code
public List ListUser() { try { List<Users> list=new ArrayList<List>(); Users user=null; stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from users"); while (rs.next()) { user=new Users(); user.setName(rs.getString("name")); user.setXXX(rs.getString("XXX")); . . list.add(user); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }
修改如下:
Java code
public List ListUser() { try { List<Users> list=new ArrayList<List>(); Users user=null; stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from users"); while (rs.next()) { user=new Users(); user.setName(rs.getString("name")); user.setXXX(rs.getString("XXX")); . . list.add(user); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询