1. 程式人生 > >【JAVA】 JAVA連線MySql資料庫

【JAVA】 JAVA連線MySql資料庫

步驟:

1.eclipse工具連線

工具:eclipse

   MySQL5.6

   MySQL連線驅動:mysql-connector-java-5.1.27.jar  

http://cdn.mysql.com//Downloads/Connector-J/mysql-connector-java-5.0.8.zip

解壓取jar檔案匯入

載入驅動:

  1. 在工程目錄中建立lib資料夾,將下載好的JDBC放到該資料夾下,如下圖所示:

     2. 右鍵工程名,在java build path中的Libraries分頁中選擇Add JARs...,選擇剛才新增的JDBC,如下圖:

資料包準備:
    計算機--管理--程式:開啟MySql程式並執行

    開啟MYSqlWorkbench,連線本地資料庫,建立一個新的schema,建立相關表格。


     insert into tables values();插入資料

    儲存,退出。

連線資料庫並讀取資料(查詢)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class main {

    public static void main(String[] args) {
        //宣告Connection物件
        Connection con;
        //驅動程式名
        String driver = "com.mysql.jdbc.Driver";
        //URL指向要訪問的資料庫名mydata
        String url = "jdbc:mysql://localhost:3306/sqltestdb";
        //MySQL配置時的使用者名稱
        String user = "root";
        //MySQL配置時的密碼
        String password = "123456";
        //遍歷查詢結果集
        try {
            //載入驅動程式
            Class.forName(driver);
            //1.getConnection()方法,連線MySQL資料庫!!
            con = DriverManager.getConnection(url,user,password);
            if(!con.isClosed())
                System.out.println("Succeeded connecting to the Database!");
            //2.建立statement類物件,用來執行SQL語句!!
            Statement statement = con.createStatement();
            //要執行的SQL語句
            String sql = "select * from emp";
            //3.ResultSet類,用來存放獲取的結果集!!
            ResultSet rs = statement.executeQuery(sql);


              String n=null;
            String p=null;
            while(rs.next()) {

                //獲取name列的數

                 n=rs.getString("name");

                //獲取password列的資料

                 p=rs.getString("password");
                System.out.println(n+"\t"+p);
            }
 
            rs.close();
            con.close();
        } catch(ClassNotFoundException e) {   
            //資料庫驅動類異常處理
            System.out.println("Sorry,can`t find the Driver!");   
            e.printStackTrace();   
            } catch(SQLException e) {
            //資料庫連線失敗異常處理
            e.printStackTrace();  
            }catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally{
            System.out.println("資料庫資料成功獲取!!");
        }
    }

}

插入資料:

            String insert="insert into password values (\"123\",\"123\")";
            PreparedStatement psql=con.prepareStatement(insert);
            psql.executeUpdate();//執行更新

刪除資料:

            PreparedStatement psql2;//預處理刪除資料
            psql2 = con.prepareStatement("delete from password where name=\"123\"");
            psql2.executeUpdate();
            psql2.close();
2.命令列連線

和上面一樣的程式碼結構,在命令列已經編譯完成的情況下,同一個資料夾內,用:

java -cp mysql.jar javaApplication

連線並使用

參考:

【1】風雪夜歸人shen的部落格:https://www.cnblogs.com/centor/p/6142775.html

【2】W3C SQL教程:http://www.w3school.com.cn/sql/index.asp