原生JDBC的使用

public class ConnDB {
    private Connection ct = null;
   //驱动程序名
    String driver = "com.mysql.jdbc.Driver";
    // URL指向要访问的数据库名scutcs
    String url = "jdbc:mysql://127.0.0.1:3306/develop";
    // MySQL配置时的用户名
    String user = "root";
    // Java连接MySQL配置时的密码
    String password = "wzy123";

    public static Connection getConn(){
        try{
            Class.forName(driver);    
            ct = DriverManager.getConnection(url, user, password);
        }
        catch(Exception e){
            e.printStackTrace();
        }
        return ct;
    }

    public int add(String day,String time,
                String volname,String volclass,String voltel,String volemail){
            Connection con = DbConnect.this.getConn();
            int num = -1;
            
            try {
                java.sql.Statement statement = con.createStatement();
                 num = statement.executeUpdate( "insert into tb_volinfo(day,time,volname,volclass,voltel,volemail) "
                        + "values('"+day+"','"+time+"','"+volname+"','"+volclass+"','"+voltel+"','"+volemail+"')");
            
                if(statement!=null){
                    statement.close();
                }
                if(con!=null){
                    con.close();//关闭数据库连接
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return num;
        }
        public boolean SelectVal(String volname,String voltel){
            Connection con1 = DbConnect.this.getConn();
            ResultSet result =null;
            
            try {
                java.sql.Statement statement = con1.createStatement();
                result = statement.executeQuery("select * from tb_volinfo where volname='"+volname+"' and voltel='"+voltel+"';");
                while(result.next()){
                    return true;
                }
                if(statement!=null){
                    statement.close();
                }
                if(con1!=null){
                    con1.close();//关闭数据库连接
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }
}

 

转载于:https://www.cnblogs.com/wwzyy/p/8686876.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值