package com.jdbc;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
public class Select {
public Student findById(String sno){
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
Student s = new Student();
try {
conn = DBUtil.getConnection();
StringBuilder sb = new StringBuilder();
sb.append(" select * from student where SNO = ?");
ps = conn.prepareStatement(sb.toString());
ps.setString(1, sno);
rs = ps.executeQuery();
System.out.println(sb);
System.out.println(rs.next());
if(rs.next()){
s.setSno(rs.getString("SNO"));
s.setSname(rs.getString("SNAME"));
s.setAge(rs.getInt("AGE"));
s.setSex(rs.getString("AGR"));
s.setDept(rs.getString("DEPT"));
System.out.println(s.getSno());
return s;
}else{
return null;
}
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
rs = null;
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
ps = null;
}
}
}
public static void main(String[] args) {
Select s = new Select();
Student student = s.findById("1004");
System.out.println(student);
}
/**
* 控制台输出:
* select * from student where SNO = ?
*true
*null
*/
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.