1. 程式人生 > >JDBC —— 簡單的連線資料庫和封裝

JDBC —— 簡單的連線資料庫和封裝

連線資料庫,並讀取表裡的內容

eclipse連線資料庫的一般過程是:
1,載入JDBC驅動(jar驅動包自己去下,這裡有匯入jar到eclipse的方法Eclipse下匯入外部jar包的3種方式
2,建立資料庫連線(Connection介面)
3,建立與執行SQL語句(Statement介面)
4,處理結果集(選擇適當的介面)
5,關閉資料連線(這裡要注意順序,要先關小門後關大門)

表Student中內容:

上程式碼:

import java.sql.*;

public class Demo1 {

    // 資料庫地址,1433是SQLServer埠,資料庫名稱是Student
private static String dbUrl="jdbc:sqlserver://localhost:1433;DatabaseName=Student"; // 使用者名稱 private static String dbUserName="sa"; // 密碼 private static String dbPassword="0123456789"; // 驅動名稱 //Mysql驅動語句是com.mysql.jdbc.Driver(這個具體的可以查到) private static String jdbcName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
; public static void main(String[] args) { try { Class.forName(jdbcName); System.out.println("載入驅動成功!"); } catch (ClassNotFoundException e) { e.printStackTrace(); System.out.println("載入驅動失敗!"); } Connection con=null
; try { // 獲取資料庫連線 con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword); System.out.println("獲取資料庫連線成功!"); System.out.println("進行資料庫操作!"); Statement statement=con.createStatement(); String sql="select * from Student"; ResultSet rs=statement.executeQuery(sql); while(rs.next()) { System.out.println(rs.getString(1)+' '+rs.getString(2) +' '+rs.getString(3)+' '+rs.getString(6)); } rs.close(); //結果集關閉 statement.close(); //執行關閉 con.close(); //連線關閉 } catch (SQLException e) { e.printStackTrace(); } } }

//output:

大家可以試一試這個程式碼,我這裡是SQLServer的,其他資料庫相應的地方要改一下。資料庫裡的內容就不展示了,自己用到裡面的表實現相應的功能就行。
有什麼問題可以在下面留言。

實現封裝

寫過連線資料庫程式碼的同志會發現,常常有很多重寫的程式碼。
此時,我們就可以用到封裝了,把重用的程式碼用一個類裝起來。
我們先來建一個DButil類:

import java.sql.*;

public class DButil {

    // 資料庫地址
    private static String dbUrl="jdbc:sqlserver://localhost:1433;DatabaseName=Student";
    // 使用者名稱
    private static String dbUserName="sa";
    // 密碼
    private static String dbPassword="0123456789";
    // 驅動名稱
    private static String jdbcName="com.microsoft.sqlserver.jdbc.SQLServerDriver";

    //獲取資料庫連線
    public Connection getCon()throws Exception{
        Class.forName(jdbcName);
        //通過訪問資料庫的URL獲取資料庫連線物件
        Connection con=DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
        return con;
       }

    public void close(Statement stmt,Connection con)throws Exception{
        if(stmt!=null){
            stmt.close();
         if(con!=null) {
             con.close();
         }
        }
    }
}

再來測試一下這個類的功能。
這裡我試一下statement介面中的executeUpdate方法。
檢視API我們不難知道,
這個方法可以實現對資料庫insert,delectable,update
這裡我們就試一下插入操作。
上程式碼:

import java.sql.*;
import DButil.DButil;

public class Demo2 {
     public static void main(String[] args) throws Exception {
         DButil dbutil=new DButil();
        String sql="insert into Student values('19931206','張三','男','ZY10','洪山廣場',null,'1.75',7)";
        //獲取資料庫連線
        Connection con =dbutil.getCon();
        Statement stmt =con.createStatement();
        int result =stmt.executeUpdate(sql);
        System.out.println("操作的結果:"+result+"資料");
        stmt.close();    //關閉Statement
        con.close();     //關閉Connection
    }
}

//output:

插入之後資料庫中Student表

大家可以對比之前資料庫中的表看,會發現插入資料成功了。