1. 程式人生 > >java中Mysql資料庫連線及例項

java中Mysql資料庫連線及例項

目標:通過在eclipse中載入mysql驅動,將users表中的資料提取出來。
1.建立資料庫
在mysql資料庫中test目錄下建立users資料庫。存取欄位:id username password。
2.構建java專案
2.1 在構建的java專案中src下面建立檔案db.properties,裡面設定內容為:
name=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=password
2.2 讀取上面檔案中的配置資訊

public  class FileRead {

    public static  String[]  read() throws FileNotFoundException{
        Properties properties = new  Properties();
        BufferedInputStream read = new BufferedInputStream(new FileInputStream("src/db.properties"));

        String [] messages = new String[4];

        try
{ properties.load(read); } catch (IOException e) { } messages[0] = properties.getProperty("name"); messages[1] = properties.getProperty("url"); messages[2] = properties.getProperty("username"); messages[3] = properties.getProperty("password"
); return messages; } }

2.3 建立連線
這裡主要有三個物件:Connection Statement ResultSet
* Conncetion:與目標資料庫建立連線
* Statement:向連線的資料庫傳送指令,並返回操作
* ResultSet:裝載資料庫指令執行結果
連結資料庫的基本操作分六步:
* 1.載入驅動
* 2.獲取連線
* 3.獲取向資料發sql語句的statement物件
* 4.向資料庫傳送sql請求,獲取資料庫返回結果集
* 5.從結果集中獲取資料
* 6.釋放資源
2.4 資料庫連線和釋放
建立FilereadParaGet檔案,在檔案中建立兩個函式getconnect()和release(),分別用來連線和釋放資料庫連線。
資料庫連線函式:

public static Connection getconnect(){

    Connection connection = null;
        try {
            String mess[] = FileRead.read();

            connection = DriverManager.getConnection(mess[1], mess[2], mess[3]);
        } catch (FileNotFoundException e) {
            System.out.println("沒有讀取到配置檔案!");
        } catch (SQLException e) {
            System.out.println("沒有連線到資料庫!");
        }
        return connection;  

    }

資料庫釋放:

public static void release(Connection con,Statement statement,ResultSet rs){

        if (rs!=null) {
            try {
                rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            rs=null;
        }
        if (statement!=null) {
            try {
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            statement=null;
        }
        if (con!=null) {
            try {
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            con=null;
        }

    }

2.5 例項操作
建立測試例項,實現從資料庫獲取資料:

public class TestMsql {
     @Test
    public void test() throws SQLException {

        Connection con=FilereadParaGet.getconnect();
        Statement statement = (Statement) con.createStatement();
         String sql="select * from users;";
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()) {
            System.out.println("id:"+rs.getInt("id")+"   "+"username:"+rs.getString("username")+ " "+"password:"+rs.getString("password"));
        }

        FilereadParaGet.release(con, statement, rs);
    }
}

執行結果:
id:1 username:zs password:123346
id:2 username:lisi password:454545
id:3 username:wangwu password:12455632
id:4 username:eee password:123
ok!