Java中的JDBC的使用方法有哪些?

JDBC其實一套規(guī)范(接口)

數(shù)據(jù)庫廠商需要實現(xiàn)此接口(實現(xiàn)類)--數(shù)據(jù)庫驅動

jdbc的作用

可以和數(shù)據(jù)庫創(chuàng)建鏈接

發(fā)送sql語句

接收返回值,處理結果

api詳解(java.sql或者javaX.sql)

DriverManager 類:

管理一組 JDBC 驅動程序的基本服務。

常用方法:? registerDriver(Driver):注冊驅動

查看 mysql的Driver的時候有下面一段代碼:?

發(fā)現(xiàn)在類加載的時候已經(jīng)注冊過驅動,我們以后只需要把Driver加載到內(nèi)存即可

類.Class

對象.getClass()

Class.forName("全限定名(包名+類名)")

以后開發(fā)中我們通過Class.forName("com.mysql.jdbc.Driver")把驅動注冊進去即可.

Connection 接口:

創(chuàng)建語句執(zhí)行者:? Connection

conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/database", "root", "123456");

常用方法:?

Statement createStatement():創(chuàng)建Statement -語句執(zhí)行者

PreparedStatement prepareStatement(String sql) :創(chuàng)建一個預編譯的語句執(zhí)行對象

CallableStatement prepareCall(String sql) :(了解) 創(chuàng)建一個 CallableStatement 對象來調用數(shù)據(jù)庫存儲過程。? Statement 接口(容易產(chǎn)生sql注入, 后期使用PreparedStatement. 后面會有blog說明這個問題)

sql語句執(zhí)行者:

Statement st=conn.createStatement();

常用方法:

ResultSet executeQuery(String sql) :執(zhí)行查詢語句,返回一個集合

int executeUpdate(String sql) :執(zhí)行更新 插入 刪除語句,返回影響行數(shù).

boolean execute(sql):執(zhí)行給定的 SQL 語句,該語句可能返回多個結果。

若返回true ,執(zhí)行是的查詢語句

調用 getResultSet 獲取查詢結果

若返回false,執(zhí)行的是更新 插入 刪除語句

調用 getUpdateCount 獲取影響的行數(shù)

ResultSet 接口

返回的查詢結果:

String sql = "...";

ResultSet rs=st.executeQuery(sql);

常用方法:

boolean next() :判斷是否有下一條記錄,并且移動到下一行

獲取內(nèi)容:getXXX(參數(shù))

參數(shù)的寫法:

1.字段名稱 字符串

2.第幾列 從1開始

getInt()

getString()

getObject()?

實例JDBCUtil類的書寫

(1)配置文件 jdbc.properties

drivername=com.mysql.jdbc.Driver

url=jdbc:mysql://localhost:3306/ddatabaseName

user=root

password=1234

(2)JDBCUtil.java

1 import java.sql.Connection;

2 import java.sql.DriverManager;

3 import java.sql.ResultSet;

4 import java.sql.SQLException;

5 import java.sql.Statement;

6 import java.util.ResourceBundle;

7

8 public class JDBCUtil {

9? ? //ctrl + shift + x? 轉成大寫

10? ? //ctrl + shift + y? 轉成小寫

11? ? static final String DRIVERNAME;

12? ? static final String URL;

13? ? static final String USERNAME;

14? ? static final String PASSWORD;

15? ?

16? ? static{

17? ? ? ?

18? ? ? ? /*通過ResourceBundle 專門用來加載properties文件

19? ? ? ? ResourceBundle bundle=ResourceBundle.getBundle("文件名稱");

20? ? ? ?

21? ? ? ? 通過 bundle.getString(鍵的名字)

22? ? ? ? String value=bundle.getString("url");

23? ? */

24? ?

25? ? ? ? ResourceBundle bundle=ResourceBundle.getBundle("jdbc");

26? ? ? ? DRIVERNAME=bundle.getString("drivername");

27? ? ? ? URL=bundle.getString("url");

28? ? ? ? USERNAME=bundle.getString("user");

29? ? ? ? PASSWORD=bundle.getString("password");

30? ? }

31? ?

32? ? static{

33? ? ? ? try {

34? ? ? ? ? ? Class.forName(DRIVERNAME);

35? ? ? ? } catch (ClassNotFoundException e) {

36? ? ? ? ? ? e.printStackTrace();

37? ? ? ? }

38? ? }

39? ?

40? ? //獲取鏈接

41? ? public static Connection getConnection() throws SQLException{

42? ? ? ? return DriverManager.getConnection(URL,USERNAME,PASSWORD);

43? ? }

44? ?

45? ? //釋放資源

46? ? public static void closeResource(Connection conn,Statement st,ResultSet rs){

47? ? ? ? if (rs!=null) {

48? ? ? ? ? ? try {

49? ? ? ? ? ? ? ? rs.close();

50? ? ? ? ? ? } catch (SQLException e) {

51? ? ? ? ? ? ? ? e.printStackTrace();

52? ? ? ? ? ? }

53? ? ? ? }

54? ? ? ?

55? ? ? ? if (st!=null) {

56? ? ? ? ? ? try {

57? ? ? ? ? ? ? ? st.close();

58? ? ? ? ? ? } catch (SQLException e) {

59? ? ? ? ? ? ? ? e.printStackTrace();

60? ? ? ? ? ? }

61? ? ? ? }

62? ? ? ?

63? ? ? ? if (conn!=null) {

64? ? ? ? ? ? try {

65? ? ? ? ? ? ? ? conn.close();

66? ? ? ? ? ? } catch (SQLException e) {

67? ? ? ? ? ? ? ? e.printStackTrace();

68? ? ? ? ? ? }

69? ? ? ? }

70? ? ? ?

71? ? }

72 }

(3)CRUDDemo, 使用PreparedStatement方式

? 1 public class PPCRUDDemo {

? 2? ? public static void main(String[] args) {

? 3? ? ? ? //插入

? 4? ? ? ? //insert("趙四","123","zhaosi@163.com");

? 5? ? ? ? //更新

? 6? ? ? ? //updateByName("趙四","尼古拉斯.趙四");

? 7? ? ? ? //獲取

? 8? ? ? ? //getByName("尼古拉斯.趙四");

? 9? ? ? ? //刪除

10? ? ? ? deleteByName("尼古拉斯.趙四");

11? ? }

12

13? ? private static void deleteByName(String string) {

14? ? ? ? //模版

15? ? ? ? Connection conn=null;

16? ? ? ? PreparedStatement st=null;

17? ? ? ? ResultSet rs=null;

18? ? ? ?

19? ? ? ? try {

20? ? ? ? ? ? //獲取鏈接

21? ? ? ? ? ? conn=JDBCUtil.getConnection();

22? ? ? ? ? ? //編寫sql

23? ? ? ? ? ? String sql="delete from user where username =?";

24? ? ? ? ? ? //獲取預編譯執(zhí)行者

25? ? ? ? ? ? st=conn.prepareStatement(sql);

26? ? ? ? ? ? //設置參數(shù)

27? ? ? ? ? ? st.setString(1, string);

28? ? ? ? ? ? //執(zhí)行sql

29? ? ? ? ? ? int i = st.executeUpdate();

30? ? ? ? ? ? //處理結果

31? ? ? ? ? ? if (i>0) {

32? ? ? ? ? ? ? ? System.out.println("成功");

33? ? ? ? ? ? }else{

34? ? ? ? ? ? ? ? System.out.println("失敗");

35? ? ? ? ? ? }

36? ? ? ? } catch (Exception e) {

37? ? ? ? ? ? e.printStackTrace();

38? ? ? ? }finally{

39? ? ? ? ? ? //釋放資源

40? ? ? ? ? ? JDBCUtil.closeResource(conn, st, rs);

41? ? ? ? }

42? ? ? ?

43? ? }

44

45? ? private static void getByName(String string) {

46

47? ? ? ? Connection conn=null;

48? ? ? ? PreparedStatement st=null;

49? ? ? ? ResultSet rs=null;

50? ? ? ?

51? ? ? ? try {

52? ? ? ? ? ? conn=JDBCUtil.getConnection();

53? ? ? ? ? ? String sql="select * from user where username=? limit 1";

54? ? ? ? ? ? st=conn.prepareStatement(sql);

55? ? ? ? ? ?

56? ? ? ? ? ? st.setString(1, string);

57? ? ? ? ? ? rs=st.executeQuery();

58? ? ? ? ? ? if (rs.next()) {

59? ? ? ? ? ? ? ? System.out.println(rs.getInt(1)+":"+rs.getString(2)+":"+rs.getObject(3)+":"+rs.getObject(4));

60? ? ? ? ? ? }

61? ? ? ? } catch (Exception e) {

62? ? ? ? ? ? // TODO: handle exception

63? ? ? ? ? ? e.printStackTrace();

64? ? ? ? }finally{

65? ? ? ? ? ?

66? ? ? ? ? ? JDBCUtil.closeResource(conn, st, rs);

67? ? ? ? }

68? ? ? ?

69? ? }

70

71? ? private static void updateByName(String oldName, String newName) {

72? ? ? ? Connection conn=null;

73? ? ? ? PreparedStatement st=null;

74? ? ? ? ResultSet rs=null;

75? ? ? ?

76? ? ? ? try {

77? ? ? ? ? ? conn=JDBCUtil.getConnection();

78? ? ? ? ? ? String sql="update user set username = ? where username = ?";

79? ? ? ? ? ? st=conn.prepareStatement(sql);

80? ? ? ? ? ? st.setString(1, newName);

81? ? ? ? ? ? st.setString(2, oldName);

82? ? ? ? ? ?

83? ? ? ? ? ? int i=st.executeUpdate();

84? ? ? ? ? ? //處理結果

85? ? ? ? ? ? if (i>0) {

86? ? ? ? ? ? ? ? System.out.println("成功");

87? ? ? ? ? ? }else{

88? ? ? ? ? ? ? ? System.out.println("失敗");

89? ? ? ? ? ? }

90? ? ? ? ? ?

91? ? ? ? } catch (Exception e) {

92? ? ? ? ? ? e.printStackTrace();

93? ? ? ? }finally{

94? ? ? ? ? ? JDBCUtil.closeResource(conn, st, rs);

95? ? ? ? }

96? ? ? ?

97? ? ? ?

98? ? }

99

100? ? private static void insert(String username, String password, String email) {

101? ? ? ? Connection conn=null;

102? ? ? ? PreparedStatement st=null;

103? ? ? ? ResultSet rs=null;

104? ? ? ?

105? ? ? ? try {

106? ? ? ? ? ? //獲取鏈接

107? ? ? ? ? ? conn=JDBCUtil.getConnection();

108? ? ? ? ? ? //編寫sql

109? ? ? ? ? ? String sql="insert into user values(null,?,?,?)";

110? ? ? ? ? ? //獲取預編譯語句執(zhí)行者

111? ? ? ? ? ? st=conn.prepareStatement(sql);

112? ? ? ? ? ? //設置參數(shù)

113? ? ? ? ? ? st.setString(1, username);

114? ? ? ? ? ? st.setString(2, password);

115? ? ? ? ? ? st.setString(3, email);

116? ? ? ? ? ? //執(zhí)行sql

117? ? ? ? ? ? int i=st.executeUpdate();

118? ? ? ? ? ? //處理結果

119? ? ? ? ? ? if (i>0) {

120? ? ? ? ? ? ? ? System.out.println("成功");

121? ? ? ? ? ? }else{

122? ? ? ? ? ? ? ? System.out.println("失敗");

123? ? ? ? ? ? }

124? ? ? ? } catch (Exception e) {

125? ? ? ? ? ? e.printStackTrace();

126? ? ? ? }finally{

127? ? ? ? ? ? JDBCUtil.closeResource(conn, st, rs);

128? ? ? ? }

129? ? }

130

131 }

這里沒有多么高深的東西在,都要靠熟能生巧!

Java學習視頻

Java基礎:

Java300集,Java必備優(yōu)質視頻_手把手圖解學習Java,讓學習成為一種享受

Java項目:

【Java游戲項目】1小時教你用Java語言做經(jīng)典掃雷游戲_手把手教你開發(fā)游戲

【Java畢業(yè)設計】OA辦公系統(tǒng)項目實戰(zhàn)_OA員工管理系統(tǒng)項目_java開發(fā)

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容