Connection cn = null;
PreparedStatement pstmt = null;
Resultset rs = null;
try{
Class.forname(driveClassName);
cn = DriverManager.getConnection(url,username,password);
pstmt = cn.prepareStatement("select * from user where username=?");
pstmt.setString(1,userName);
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){
}
提示:当然现在已经不再使用此种方法连接数据库了,这里只是提供给那些正在打基础的人。