欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JDBC連接的六步實例代碼(與mysql連接)

 更新時間:2021年05月12日 09:13:21   作者:香風智乃哈~  
這篇文章主要給大家介紹了關(guān)于JDBC連接的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

JDBC的六步:

1.注冊驅(qū)動

2.獲取數(shù)據(jù)庫的連接

3.獲取數(shù)據(jù)庫的操作對象

4.執(zhí)行sql語句

5.處理查詢結(jié)果集(如果執(zhí)行的語句中沒有select語句這一步不用寫)

6.關(guān)閉資源

第一步:注冊驅(qū)動

//異常一定是需要處理的
//根據(jù)版本不同書寫的代碼有一些變化,老版本是
DriverManager.register(new com.mysql.jdbc.Driver());
//或者
Class.forName("com.mysql.jdbc.Driver");
 
 
//新版本是
DriverManager.register(new com.mysql.cj.jdbc.Driver());
//或者
Class.forName("com.mysql.cj.jdbc.Driver");

第二步:獲取數(shù)據(jù)庫的連接

//因為要進行try..catch,還要關(guān)閉這個資源,因此寫在try外邊且賦值為空
Connection conn = null;
...
//返回值是Connection,老版本和新版的url書寫也不一樣
// jdbc:mysql://localhost:3306/t_use這個前面的都一樣,把t_use更改為自己的數(shù)據(jù)庫名
//老版
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_use","用戶名","密碼");
 
 
//新版
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/t_use?serverTimezone=GMT%2B8","用戶名","密碼");

第三步:獲取數(shù)據(jù)庫操作對象

//這個和剛才的Connection是一樣的
Statement st = null;
...
//這樣就把對象創(chuàng)建好了
st = conn.createStatement();

第四步:執(zhí)行sql語句

//引號里面寫sql語句
String sql = " ";
//再用剛才創(chuàng)建好的對象接入這個sql語句
//這里還有需要說的,如果不是select語句就用下面這個,返回值是一個int,這個就不需要第五步了
st.executeUpdate(sql);
 
//如果sql語句是select的話,就要用下面的這個語句了,還需要定義一個ResultSet對象,去接收這個值
//然后進行第五步的處理
ResultSet rs = null; //和Connection一樣的
rs = st.executeQuery(sql);

第五步:處理查詢結(jié)果集

//這個只有select語句才進行處理,不僅可以getString還可以getInt等
while(rs.next()){
    String str = rs.getString("需要輸出的字段的名字");
}

第六步:關(guān)閉資源

//前面五步都是在try里面進行的,第六步是在finally進行的
//關(guān)閉連接
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
 
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
 
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

關(guān)閉連接也是有順序的,先關(guān)ResultSet,再關(guān)Statement對象,最后關(guān)Connection對象.

完整代碼

package jdbc.com;
import com.mysql.cj.protocol.Resultset;
 
import java.sql.*;
public class Test02 {
    public static void main(String[] args) {
 
        Connection conn = null;
        Statement st = null;
        ResultSet rt = null;
        try {
            //注冊連接(可以寫到一行里面)
            //com.mysql.cj.jdbc.Driver Driver = new com.mysql.cj.jdbc.Driver();
            DriverManager.registerDriver(new com.mysql.cj.jdbc.Driver());
            //獲取數(shù)據(jù)庫連接
            conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/liu2?serverTimezone=GMT%2B8","用戶名","密碼");
            //獲取數(shù)據(jù)庫操作對象
            st = conn.createStatement();
            //執(zhí)行sql語句
            String sql = "select ename,sal from emp order by sal desc";
            rt = st.executeQuery(sql);
            //處理查詢語句
           while(rt.next()){
               String ename = rt.getString("ename");
               String sal = rt.getString("sal");
               System.out.println(ename + "," + sal);
           }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            //關(guān)閉連接
            if (rt != null) {
                try {
                    rt.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
 
            if (st != null) {
                try {
                    st.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
 
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
 
 
        }
    }
}

最后,這個如果要傳入值的話,可能會造成sql的注入,解決辦法就是把Statement變成PreparedStatement,就可以避免sql的注入問題了,只不過第三步和第四步代碼有點變化,就不再多說了。。

總結(jié)

到此這篇關(guān)于JDBC連接(與mysql連接)的文章就介紹到這了,更多相關(guān)JDBC與mysql連接內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論