用JDBC查询学生成绩单

Collection conn = null ;
PreparedStatement st = null ;
ResultSet rs = null ;
try{
    Class.forname(driverClassName) ;
    conn = DriverManager.getConnection(url,username,password) ;
    ps = conn.prepareStatement("select score.* from score,student where score.stuid = student.id and student.name=?") ;
    ps.setString(1,studentName) ;
    Result rs = ps.executeQuery() ;
    while(rs.next()){
        system.out.println(rs.getInt("subject")+rs.getFloat("score")) ;
    }
    catch(Exception e){
        e.printStackTrace() ;
    }
    finally{
        if(rs!=null) {rs.close()}
        if(ps!=null) {ps.close()}
        if(conn!=null) {conn.close}
    }
}

或者是

import java.sql.* ;
public class Select{
    //定义数据库驱动程序
    public static final String driver = "com.mysql.jdbc.Driver" ;
    //定义数据库连接
    public static final String url = "jdbc:mysql://localhost:3306/user" ;
    //定义数据库名称
    public static final String username = "root"  ;
    //定义数据库密码
    pulic static final String password = "123" ;
    Collection conn = null ;
    PreparedStatement ps = null ;
    ResultSet rs = null ;
    try{
        Class.forName(driver) ;
        conn = DriverManager.getConnection(url,username,password) ;
        ps = conn.getPrepareStatement("select score.* from score,student where score.id = student.id and student.name=null") ;
        ResultSet rs = ps.executeQuery() ;
        while(rs.next()){
            System.out.println(rs.getInt("subject")+rs.getFloat("score"))
        }
     catch(Exception e){
         e.printStackTract() ;
     }
     finally{
         if(rs!=null){
             try{
                 rs.close();
             }catch(Exception e){
                 e.printStackTrace() ;
             }
         }
         if(ps!=null){
             try{
                 ps.close();
             }catch(Exception e){
                 e.printStackTrace() ;
             }
         }
         if(conn!=null){
             try{
                 conn.close() ;
             }catch(Exception e){
                 e.printStackTrace() ;
             }
         }
     }
    }
    
}




转载于:https://my.oschina.net/bugkiller/blog/661417

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值