jdbc基礎(chǔ)

JDBC概念

java連接數(shù)據(jù)庫(kù)的一系列接口,接口的實(shí)現(xiàn)類(lèi)交給第三方數(shù)據(jù)庫(kù)廠商去實(shí)現(xiàn)

四大參數(shù)

驅(qū)動(dòng) com.mysql.jdbc.Driver

DriverManager

Class.forname("com.mysql.jdbc.Driver");

Connection

String url="jdbc:mysql://localhost:3306/數(shù)據(jù)庫(kù)名稱(chēng)?useUnicode=true&characterEncoding=utf-8";
String user="root";
String password ="";
Connection con = DriverManager.getConnection(url,user,password);

Statement

        Statement statement=con.createStatement();
        String sql="update employee set username='YYT'where id=1 ";
        int i = statement.executeUpdate(sql);
        System.out.println(i);
        con.close();

ResultSet

//得到一條記錄
String sql="select * from employee ";
 ResultSet  rs =statement.executeQuery(sql);
 if(rs.next()){
     rs.getInt("字段名");
     String empno = rs.getString("empno");
 }

//得到所有記錄
        while(rs.next()){
                int id = rs.getInt("id");
                String empno = rs.getString("empno");
        }

事務(wù)

1.完整性
2.一致性
3.原子性
4.隔離性

       try{
            con.setAutoCommit(false);
           String sql="delete from employee where id=1";
           String sql1="delete from employee where id=2";
           statement.executeUpdate(sql);
            statement.executeUpdate(sql1);
            con.commit();
        }catch(Exception e){
        System.out.println("操作失敗");
            con.rollback();
        }

簡(jiǎn)單的封裝登錄方法

static String url="jdbc:mysql://localhost:3306/employee?useUnicode=true&characterEncoding=utf-8";
    static String us="root";
    static String psw="";
    public void driver(){
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        }
    }
   public static Connection connect(){
       try {
          Connection con=DriverManager.getConnection(url,us,psw);
          return con;
    } catch (SQLException e) {
        
        e.printStackTrace();
    }
           return null;
   }
 
   public User  login(String usn,String psw){
       Statement statement=null;
       Connection con=null;
       try {
         con = connect();
         statement = con.createStatement();
        String sql="select * from user where username='"+usn+"'and password="+psw;
        ResultSet qs = statement.executeQuery(sql);
        if(qs.next()){
            String username = qs.getString("username");
            String password = qs.getString("password");
            User user=new User(username,password);
            return user;
        }
    } catch (SQLException e) {
        e.printStackTrace();
        
    }   
     finally{
         try {
            if(statement!=null){
                statement.close();
            }
            if(con!=null){
            con.close();
            }
        } catch (SQLException e) {
    
            e.printStackTrace();
        }
        
     }
        return null; 
       
   }


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 一,JDBC概述 1.為什么要使用JDBC 1.1沒(méi)有JDBC 1.2有了JDBC后 2.JDBC的概念 2.1....
    有樓萬(wàn)事足閱讀 224評(píng)論 0 0
  • JDBC JDBC標(biāo)準(zhǔn) JDBC是什么 Java Database Connectivity:Java訪問(wèn)數(shù)據(jù)庫(kù)的...
    駭客與畫(huà)家閱讀 859評(píng)論 0 3
  • JDBC API 是一系列的接口。它使得應(yīng)用程序能夠進(jìn)行數(shù)據(jù)庫(kù)聯(lián)接,執(zhí)行SQL語(yǔ)句,并且得到返回結(jié)果。 Drive...
    怪蜀黍Zzzzlw閱讀 414評(píng)論 0 2
  • 簡(jiǎn)介 1、JDBC(java Data Base Connectivity,java數(shù)據(jù)庫(kù)連接),由一些接口和類(lèi)組...
    exmexm閱讀 128評(píng)論 0 0
  • JDBC概述 在Java中,數(shù)據(jù)庫(kù)存取技術(shù)可分為如下幾類(lèi):JDBC直接訪問(wèn)數(shù)據(jù)庫(kù)、JDO技術(shù)、第三方O/R工具,如...
    usopp閱讀 3,558評(píng)論 3 75