【Java】JDBC之與資料庫MySQL的串連以及增刪改查等操作

來源:互聯網
上載者:User
文章目錄
  • 必須的準備工作
  • 串連的理論知識
  • 串連測試
  • 用 Java  對SQL進行相關操作
必須的準備工作一、MySQL的安裝。可以參考博文:http://blog.csdn.net/jueblog/article/details/9499245二、下載 jdbc 驅動。可以從在官網上下載,或者點擊 http://download.csdn.net/detail/oyuntaolianwu/5822697 下載三、在 Eclipse 的 Java 工程中建立一個 lib 檔案夾,將所準備的 JAR 包複製進去。四、右鍵該 JAR 包:Build Path --> Add to Build Path 串連的理論知識需要用到 Java.sql.*; 中的幾個相關類:Connection類

負責建立與指定URL(包含資料庫IP地址、庫名、使用者名稱和密碼的資訊)的串連;
Connection conn = DriverManager.getConnection(url,user,password);DriverManager.getConnection(url);
利用驅動管理器類擷取指定URL串連

String url = "jdbc:mysql://localhost:3306/test";    //串連URL為   jdbc:mysql//伺服器位址/資料庫名 
Statement類stmt = conn.createStatment();
語句對象,用來向資料庫發送一條SQL語句
rs = stmt.executeQuery(sql)  //返回記錄集對象,用於查詢
int count = stmt.executeUpdate(sql) //執行一條增刪改語句,返回int
stmt.execute(sql) //增刪改都可以,返回布爾值(執行成功or失敗)
ResultSet類

記錄集對象,儲存executeQuery()方法返回的記錄集合。用相關的rs.getString("列名") rs.getInt("列名")等方法擷取指定列的值。串連測試Java代碼如下:

package com.sql;import java.sql.*;public class JDBC0726_Base {Connection connection;Statement statement;ResultSet rSet;//返回一個與特定資料庫的串連public Connection getConnection() {try {//串連URL為   jdbc:mysql//伺服器位址/資料庫名  ,後面的2個參數分別是登陸使用者名稱和密碼connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "yongqiang");} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return connection;}public static void main(String[] args) {JDBC0726_Base jDao = new JDBC0726_Base();System.out.println(jDao.getConnection());}}

如果輸出相應的對象地址,而不是異常,則證明串連成功。如輸出:com.mysql.jdbc.JDBC4Connection@200bde用 Java  對SQL進行相關操作【注】以下操作需要依託於上面的 getConnection() 方法非查詢類SQL語句

//非查詢類public int Update(String sql) {getConnection();int count =0;try {statement = connection.createStatement();count = statement.executeUpdate(sql);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{try {if (statement != null) {statement.close();statement = null;}if (connection != null) {connection.close();connection = null;}} catch (SQLException e2) {// TODO: handle exception}}return count;}

應用:

System.out.println(jDao.Update("INSERT INTO t_user(username,password,sex) values('hehe','131','n');"));

輸出值為 1 則證明添加成功。

查詢類 SQL 並返回多條記錄
//執行一條查詢類SQL,返回多條記錄集public void Qurty(String sql) {getConnection();try {statement = connection.createStatement();rSet = statement.executeQuery(sql);System.out.println("id\t" + "realName\t"+"school\t");while (rSet.next()) {System.out.println(rSet.getRow()+ "----" + rSet.getString("id") + "\t" + rSet.getString("realName") + "\t" + rSet.getString("school") + "\t");}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{try {if (statement != null) {statement.close();statement = null;}if (rSet != null) {rSet.close();rSet = null;}} catch (SQLException e2) {// TODO: handle exception}}}

應用舉例:

jDao.Qurty("SELECT * FROM t_user WHERE sex='女';");System.out.println("-----------------------------");jDao.Qurty("SELECT * FROM t_user;");System.out.println("-----------------------------");
查詢類 SQL 並返回一條記錄
//執行一條查詢類SQL,返回單條記錄集public void QurtyByUnique(String sql) {getConnection();try {statement = connection.createStatement();rSet = statement.executeQuery(sql);System.out.println("id\t" + "realName\t"+"school\t");if (rSet.next()) {System.out.println(rSet.getInt("id") + "\t" + rSet.getString("realName") + "\t" + rSet.getString("school") + "\t");}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{try {if (statement != null) {statement.close();statement = null;}if (rSet != null) {rSet.close();rSet = null;}} catch (SQLException e2) {// TODO: handle exception}}}

應用舉例:

jDao.QurtyByUnique("SELECT * FROM t_user WHERE sex='女';");System.out.println("-----------------------------");jDao.QurtyByUnique("SELECT * FROM t_user;");System.out.println("-----------------------------");
輸出表單所有資料
public void QurtyTest(String sql) {getConnection();try {statement = connection.createStatement();rSet = statement.executeQuery(sql);while (rSet.next()) {System.out.print(rSet.getRow()+ "----" );for (int i = 1; i < 14; i++) {System.out.print(rSet.getString(i) +"\t");}System.out.println();}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} finally{try {if (statement != null) {statement.close();statement = null;}if (rSet != null) {rSet.close();rSet = null;}} catch (SQLException e2) {// TODO: handle exception}}}

應用舉例

jDao.QurtyTest("SELECT * FROM t_user;");

聯繫我們

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