激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - jdbc連接數據庫實例詳解

jdbc連接數據庫實例詳解

2021-07-17 12:17Java教程網 Java教程

在本篇內容里小編給大家分享了關于jdbc如何連接數據庫的相關知識點內容,需要的朋友們學習下。

jdbc簡介

jdbc全稱為:java data base connectivity (java數據庫連接),可以為多種數據庫提供填統一的訪問。jdbc是sun開發的一套數據庫訪問編程接口,是一種sql級的api。它是由java語言編寫完成,所以具有很好的跨平臺特性,使用jdbc編寫的數據庫應用程序可以在任何支持java的平臺上運行,而不必在不同的平臺上編寫不同的應用程序。

jdbc編程步驟

(1)加載驅動程序:

下載驅動包 : http://dev.mysql.com/downloads/connector/j/

解壓,得到 jar文件。將該文件復制到java工程目錄java resources/libraries/ 下,→ buildpath 。

(2)獲得數據庫連接

(3)創建statement對象:

(4)向數據庫發送sql命令

(5)處理數據庫的返回結果(resultset類)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package com.baidu.emp.jdbctest;
 
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.statement;
 
import com.mysql.jdbc.driver;
/**
 * 開始使用jdbc連接數據庫
 * @author admin
 *
 */
public class test001 {
 
  public static void main(string[] args) throws exception {
 
    /**
     * 加載驅動
     */
    // 方法一:
    /*
     * import java.sql.drivermanager; import com.mysql.jdbc.driver;
     */
    // driver driver = new driver();
    // drivermanager.registerdriver(driver);
 
    // 方法二:(推薦使用)
    class.forname("com.mysql.jdbc.driver");
 
    /**
     * 創建鏈接
     */
    string url = "jdbc:mysql://localhost:3306/testjdbc";
    string user = "root";
    string password = "root";
    connection connection = drivermanager.getconnection(url, user, password);
 
    // 創建statement對象
    statement statement = connection.createstatement();
 
    /**
     * 執行sql,獲取結果集
     */
    string sql = "select * from test01";
    resultset result = statement.executequery(sql);
 
    // 遍歷結果集
    while (result.next()) {
      string name = result.getstring("name");
      int id = result.getint("id");
      system.out.println(name + "\t" + id);
    }
 
    /**
     * 關閉鏈接,釋放資源
     */
    result.close();
    statement.close();
    connection.close();
  }
}

防止sql注入改用preparestatement

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.boya.emp.jdbctest;
 
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.preparedstatement;
import java.sql.resultset;
/**
 * sql注入,使用preparestatement對象進行預編譯
 * @author admin
 *
 */
public class test002 {
 
  public static void main(string[] args) throws exception {
 
    /**
     * 加載驅動
     */
    class.forname("com.mysql.jdbc.driver");
 
    /**
     * 創建鏈接
     */
    string url = "jdbc:mysql://localhost:3306/testjdbc";
    string user = "root";
    string password = "root";
    connection connection = drivermanager.getconnection(url, user, password);
 
    // 寫sql
    string sql = "select * from test01 where id = ?";
    //創建statement對象,預編譯
    preparedstatement statement = connection.preparestatement(sql);
    //設置參數
    statement.setint(1, 2);
    /**
     * 執行sql,獲取結果集
     */
    resultset result = statement.executequery();
 
    // 遍歷結果集
    while (result.next()) {
      string name = result.getstring("name");
      int id = result.getint("id");
      system.out.println(name + "\t" + id);
    }
 
    /**
     * 關閉鏈接,釋放資源
     */
    result.close();
    statement.close();
    connection.close();
  }
}

進行代碼優化,設置配置文件,工具類,實現增刪該查

增加配置文件方便修改數據庫,用戶登錄。。。

jdbc.properties(配置文件名)

?
1
2
3
4
drivername=com.mysql.jdbc.driver
url=jdbc:mysql://localhost:3306/testjdbc
username=root
password=root

注意寫配置文件時中間不可以有空格,引號之類的

工具類:增強了代碼的復用性

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.baidu.emp.utils;
 
import java.io.inputstream;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.preparedstatement;
import java.sql.resultset;
import java.sql.sqlexception;
import java.util.properties;
 
import org.junit.test;
 
 
 
public class jdbcutils {
 
  static string driverclassname;
  static string url;
  static string user;
  static string password;
 
  static {
    // 創建配置文件對象
    properties properties = new properties();
    // 加載配置文件輸入流
    inputstream inputstream = jdbcutils.class.getclassloader().getresourceasstream("jdbc.properties");
    // 重新加載配置文件
    try {
      properties.load(inputstream);
      // 獲取配置文件的值
      driverclassname = properties.getproperty("drivername");
      url = properties.getproperty("url");
      user = properties.getproperty("username");
      password = properties.getproperty("password");
      class.forname(driverclassname);
 
    } catch (exception e) {
      // 拋出異常
      throw new runtimeexception(e);
    }
  }
 
  /**
   * 獲取連接
   */
  @test
  public void testname() throws exception {
     
    system.out.println(driverclassname);
  }
  public static connection getconnection() {
    connection connection = null;
    try {
      connection = drivermanager.getconnection(url, user, password);
    } catch (sqlexception e) {
      // 拋出異常
      throw new runtimeexception(e);
    }
    return connection;
  }
 
  /**
   * 關閉鏈接,釋放資源
   */
  public static void close(connection connection, preparedstatement statement, resultset resultset) {
 
    try {
      if (resultset != null) {
        resultset.close();
      }
      resultset = null; // 垃圾及時清除
      //注意,不要弄成死循環
      close(connection, statement);
    } catch (sqlexception e) {
      throw new runtimeexception(e);
    }
 
  }
 
  /**
   * 增刪改釋放資源
   */
  public static void close(connection connection, preparedstatement statement) {
 
    try {
      if (connection != null) {
        connection.close();
      }
         
      connection = null;
      if (statement != null) {
        statement.close();
      }
      statement = null;
 
    } catch (sqlexception e) {
      throw new runtimeexception(e);
    }
 
  }
 
}

測試增刪改查:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.baidu.emp.jdbctest;
 
import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;
 
import org.junit.after;
import org.junit.before;
import org.junit.test;
 
import com.baidu.emp.utils.jdbcutils;
 
/**
 * 使用jdbcutils連接數據庫進行增刪改查
 *
 * @author admin
 *
 */
public class test003 {
 
  // 初始化值
  connection connection = null;
  preparedstatement statement = null;
  resultset result = null;
 
  @before
  public void start() throws exception {
    // 創建鏈接
    connection = jdbcutils.getconnection();
    system.out.println("創建鏈接");
  }
 
  @after
  public void end() throws exception {
    // 關閉鏈接
    jdbcutils.close(connection, statement, result);
    system.out.println("關閉鏈接");
  }
   
  /**
   *插入數據
   * @throws exception
   */
  @test
  public void add() throws exception {
    string sql = "insert into test01 values(null,?)";
    statement = connection.preparestatement(sql);
    statement.setstring(1, "李四");
    int result = statement.executeupdate();
    if (result!=0) {
      system.out.println("添加成功");
    }
  }
  /**
   * 刪除數據
   * @throws exception
   */
  @test
  public void del() throws exception {
    string sql = "delete from test01 where id =?";
    statement = connection.preparestatement(sql);
    statement.setint(1,3);
    int result = statement.executeupdate();
    if (result!=0) {
      system.out.println("刪除成功");
    }
  }
  /**
   * 修改數據
   * @throws exception
   */
  @test
  public void change() throws exception {
    string sql = "update test01 set name = ? where id = ?";
    statement = connection.preparestatement(sql);
    statement.setstring(1, "張飛");
    statement.setint(2, 2);
    int result = statement.executeupdate();
    if (result!=0) {
      system.out.println("修改成功");
    }
  }
   
  /**
   * 查詢全部數據
   * @throws exception
   */
  @test
  public void findall() throws exception {
    string sql = "select id , name from test01";
    statement = connection.preparestatement(sql);
    result = statement.executequery();
    if (result.next()) {
      system.out.println("查詢成功");
    }
  }
   
  /**
   * 條件查詢數據
   * @throws exception
   */
  @test
  public void findone() throws exception {
    string sql = "select id , name from test01 where id = ?";
    statement = connection.preparestatement(sql);
    statement.setint(1, 2);
    result = statement.executequery();
    if (result.next()) {
      system.out.println("查詢成功");
    }
  }
 
}

以上就是相關知識以及相關代碼,感謝大家對服務器之家的支持。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久久久一本一区二区青青蜜月 | 深夜小视频在线观看 | 99精品视频在线观看免费播放 | 91精品观看91久久久久久国产 | 999视频网| h视频免费在线观看 | 美女视频大全网站免费 | 久久精品国产99久久6动漫亮点 | 成人毛片免费在线 | 夏目友人帐第七季第一集 | 亚洲成人自拍电影 | 在线播放一区二区三区 | 曰韩精品 | 亚洲性在线视频 | 久久综合婷婷香五月 | 亚洲午夜电影 | 一区二区国产在线 | 欧美一级做 | 免费黄色一级网站 | 精品国产一区三区 | 日本中文字幕网址 | 久久网国产| 精品小视频 | 成人三级免费电影 | www.17c亚洲蜜桃 | 日日草夜夜| 亚洲精品欧美二区三区中文字幕 | 最近免费观看高清韩国日本大全 | 5xx免费看| 国产精品一区二区在线 | 在线播放黄色网址 | 国产伦久视频免费观看视频 | 黄片毛片一级 | 欧美色爱综合 | 久久久成人精品视频 | 日韩电影av在线 | 少妇一级淫片免费放正片 | 亚洲免费观看视频 | 成人免费久久 | 斗破苍穹在线观看免费完整观看 | av成人免费观看 |