主要是看异常的处理
Connection cn = null;
PreparedStatement pstmt =null;
Resultset rs = null;
try
{
Class.forname(driveClassName);
cn = DriverManager.getConnection(url,username,password);
pstmt = cn.prepareStatement(“select score.* from score ,student “ +
“where score.stuId = student.id and student.name = ?”);
pstmt.setString(1,studentName);
Resultset rs = pstmt.executeQuery();
while(rs.next())
{
system.out.println(rs.getInt(“subject”) + “ ” + 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){}
}
|