文章目錄
- 必須的準備工作
- 串連的理論知識
- 串連測試
- 用 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;");