JDBC 5中常見資料的串連擷取

來源:互聯網
上載者:User

標籤:style   io   ar   color   os   使用   sp   for   java   

JDBC在開發中很少直接使用(持久化層有許多傑出的架構,如:Hibernate、mybatis...),但這又是Java程式員必須清楚的基礎知識,下面是一些知識的基類,方便以後複習時使用。

Java對資料庫的操作主要以下幾步

1)載入驅動,擷取資料庫連接

2)開啟事務

3)建立語句

4)批量執行操作或執行查詢擷取結果集,處理結果

5)提交事務

6)關閉串連


第一步:載入驅動類,擷取資料庫連接,條件是要有對應的第三方jar包

1)、Sqlserver ,需要 sqljdbc4.jar

try {   String username = "sa"; // 使用者名稱   String password = "123"; // 密碼   Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");// 載入驅動類   con = DriverManager.getConnection("jdbc:sqlserver://192.168.4.116:1433;DatabaseName=UniMonDB",username,password);// 擷取串連  } catch (ClassNotFoundException e) {   e.printStackTrace();  } catch (SQLException e) {   e.printStackTrace();  }

2)MySQL ,需要mysql-connector-java-5.1.6-bin.jar

try {   String username = "sa";   // 使用者名稱   String password = "123456";   // 密碼   Class.forName("com.mysql.jdbc.Driver");// 載入驅動類   Connection con = DriverManager     .getConnection(       "jdbc:mysql://192.168.4.116:55555/emm_manager",       username, password);// 擷取串連   System.out.println(con);   con.close();  } catch (ClassNotFoundException e) {   e.printStackTrace();  } catch (SQLException e) {   e.printStackTrace();  }

3)PostgreSQL,需要 postgresql-9.0-801.jdbc3.jar

try {   String username = "sa";   // 使用者名稱   String password = "123456";   // 密碼   Class.forName("org.postgresql.Driver");// 載入驅動類   Connection con = DriverManager     .getConnection(       "jdbc:postgresql://192.168.4.10:5432/UniMonDB",       username, password);// 擷取串連   System.out.println(con);   con.close();  } catch (ClassNotFoundException e) {   e.printStackTrace();  } catch (SQLException e) {   e.printStackTrace();  }

4)Oracle , 需要ojdbc14.jar

try {   String username = "sa";   // 使用者名稱   String password = "123456";   // 密碼   Class.forName("oracle.jdbc.driver.OracleDriver");   Connection con = DriverManager.getConnection(       "jdbc:oracle:thin:@192.168.0.39:1521:TARENADB",       username, password);// 擷取串連   System.out.println(con);   con.close();  } catch (ClassNotFoundException e) {   e.printStackTrace();  } catch (SQLException e) {   e.printStackTrace();  }

5)SQLite , 需要 sqlitejdbc-v033-nested.jar

try {   Class.forName("org.sqlite.JDBC"); // 載入驅動   Connection con = DriverManager.getConnection(     "jdbc:sqlite://d:/sqlite/learnsqlite.db");// 擷取串連   System.out.println(con);   con.close();  } catch (ClassNotFoundException e) {   e.printStackTrace();  } catch (SQLException e) {   e.printStackTrace();  }

第二步:設定事務提交

con.setAutoCommit(false);//.................資料庫操作(增刪改)con.commit();


關於jar包,這裡沒有提供下載,如果有學習的同學,可以私聊.........................


JDBC 5中常見資料的串連擷取

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.