JDBC簡介,jdbc

來源:互聯網
上載者:User

JDBC簡介,jdbc

jdbc串連資料庫的四個對象

DriverManager  驅動類     

       DriverManager.registerDriver(new com.mysql.jdbc.Driver());不建議使用

           原因有2個:

                        > 導致驅動被註冊2次。

                        > 強烈依賴資料庫的驅動jar

             解決辦法:

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

 Connection  串連類

   static Connection getConnection(String url, String user, String password)

           試圖建立到給定資料庫 URL 的串連。

          getConnection("jdbc:mysql://localhost:3306/day06","root", "root");

          URL:SUN公司與資料庫廠商之間的一種協議。

                 jdbc:mysql://localhost:3306/day06

                協議 子協議  IP :連接埠號碼 資料庫

                   資料庫類型

                          mysql: jdbc:mysql://localhost:3306/day14 或者 jdbc:mysql:///day14(預設本機串連)

                          oracle: jdbc:oracle:thin:@localhost:1521:sid

            getConnection(String url, Properties info)

                  Propertiesinfo = new Properties();//要參考資料庫文檔  可以用檔案代替

                  info.setProperty("user","root");//使用者名稱

                  info.setProperty("password","root");//密碼

    //擷取連線物件傳回值connection

   Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/day14?user=root&password=root");

Statement  操作資料庫類

           //建立操作資料庫對象

       Statement state=conn.createStatement();

     String sql="sql語句";

    Result rt= state.executeQuery(sql);//返回結果集

      問題:存在sql注入

      解決辦法

         使用傳入參數方式 防止sql注入
Statement (PreparedStatement)//先行編譯對象PreparedStatement

    特點:

           1.效能要高

           2.會把sql語句先編譯

           3.sql語句中的參數會發生變化,過濾掉使用者輸入的關鍵字。

  Statement state= conn.preparedStatement();

       String sql="select * from user where username=? and password=?";

       Result rt= state.executeQuery(sql);//返回結果集

       state.setString(1,username);

       state.setString(2,password);

      執行對象返回的結果
         ResultSet executeQuery();
         int executeUpdate();
         boolean execute();

         delete from users where id=?
         ps.setInt(1,5);


ResultSet 結果集

  結果集(用戶端存表資料的對象)

           //擷取資料

            next();

            getString();
            getDouble();
            getDate();

總結
利用四個核心對象編寫代碼


try{
              //載入驅動
              Class.forName("com.mysql.jdbc.Driver");
              //建立串連Connection
              Connection conn = DriverManager.getConnection("jdbc:mysql:///day06","root","abc");
              //得到執行sql的statement對象
              //conn.createStatement();
              PreparedStatement ps = conn.prepareStatement("select * from users where name=? and pwd=?");
              ps.setString(1,"tom");
              ps.setString(2,"123");
              //執行語句,並返回結果
              ResultSet rs = ps.executeQuery();
              //處理結果
             while(rs.next()){
                  User u = new User();
                  u.setId(rs.getInt(1));
                     .... }
                       }

          catch(Exception e){
              e.printSt...  }

          finally{
            //關閉資源
            if(rs!=null)
            {rs.close();}
            if(ps!=null)
            {ps.close();}
            if(conn!=null)
            {conn.close();}
               }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.