用 JDBC查询学生成绩单,把主要代码写出来(考试概率极大)

JDBC浏览:244收藏:0
答案:
Connection cn = null;
PreparedStatement pstmt =null;
Resultset rs = null;
try {
Class.forname(driveClassName);
cn = DriverManager.getConnection(url,username,password);
pstmt =cn.prepareStatement(“select score.* fromscore ,student “ +
“wherescore.stuId = student.id and student.name = ?”);
pstmt.setString(1,studentName);
Resultset rs =pstmt.executeQuery();
while(rs.next()) {
system.out.println(rs.getInt(“subject”) + “\t”+ rs.getFloat(“score”) );
}
}catch(Exception e){e.printStackTrace();}
finally{
if(rs != null) try{rs.close(); }catch(exception e){}
if(pstmt != null)try{pstmt.close();}catch(exception e){}
if(cn != null) try{cn.close(); }catch(exception e){}
}