好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java 如何使用JDBC连接数据库

一、使用 JDBC 连接数据库

1.使用JDBC-ODBC桥驱动程序连接数据库

基本步骤:
(1)加载并注册数据库驱动
(2)通过DriverManager获取数据库连接
(3)通过Connection对象获取Statement对象
(4)使用Statement接口执行SQL语句
(5)操作ResultSet结果集
(6)关闭连接,释放资源

2.下面进行代码演示

1.注册数据库驱动程序的语法格式如下:

?

1

DriverManager.registerDriver(Driver driver)

或者

?

1

Class.forName( "DriverName" );

2.创建数据库连接

?

1

2

3

4

5

6

String url = "jdbc:odbc:student" ;

//student是在数据源管理器中创建的数据源名字

Connection con = DriverManager.getConnection(url);

//一下语句是采用了一种无数据源连接数据库的方式

con=DriverManager.getConnection("jdbc:odbc:driver={Microsoft Access Driver(*.mdb)};

DBQ=d:\\xsgl.mdb")

3.获取Statement对象
可见之前连载的三种成员方法创建Statement对象、PreparedStatement对象、CallableStatement对象

4.执行SQL语句
所有的Statement都有以下三种执行SQL语句的方法
(1)execute():可以执行任何SQL语句
(2)executeQuery():执行查询语句,返回ResultSet对象
(3)executeUpate():执行增删改操作

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.bjpowernode.java_learning;

 

import java.sql.Statement;

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.SQLException;

 

public class D134_1_JDBCConnection {

     public static void main(String[] args) {

         Statement stmt = null ;

         ResultSet rs = null ;

         Connection conn = null ;

        

         try {

             //1.注册数据库的驱动程序

             Class.forName( "com.hxtt.sql.access.AccessDriver" );

             //2.通过DriverManager获取数据库连接

             conn = DriverManager.getConnection( "jbdc:Access:///e:xsgl.mdb" );

             //3.通过Connection对象获取Statement对象

             stmt = conn.createStatement();

             //4.使用Statement执行SQL语句

             String sql = "select * from studentInfo" ;

             rs = stmt.executeQuery(sql);

             //5.操作ResultSet结果集

             System.out.println( "studentID | studentName | studentSEX" );

             while (rs.next()) {

                 int id = rs.getInt( "studentID" ); //通过列名获取指定字段的值

                 String name = rs.getString( "studentName" );

                 String psw = rs.getString( "studentSEX" );

                 System.out.println(id + " | " + name + " | " + psw);

             }

         } catch (Exception e) {

             e.printStackTrace();

         } finally {

             //6.回收数据库资源

             if (rs != null ) {

                 try {

                     rs.close();

                 } catch (SQLException e) {

                     e.printStackTrace();

                 }

                 rs = null ;

             }

             if (stmt != null ) {

                 try {

                     stmt.close();

                 } catch (SQLException e) {

                     e.printStackTrace();

                 }

                 stmt = null ;

             }

             if (conn != null ) {

                 try {

                     conn.close();

                 } catch (SQLException e) {

                     e.printStackTrace();

                 }

                 conn = null ;

             }

         }

     }

}

3.注意点

JDK1.7以后的版本不再包含Access桥接驱动程序,因此不再支持JDBC-ODBC桥接方式,需要下载Access驱动程序的jar包(Access_JDBC30.jar),而JDK1.1到JDK1.6都自带jar包,不需要下载。 下载完成后把Access_JDBC30.jar包放到JDK的lib文件夹里,之后修改环境变量CLASSPATH,在其中加上这个jar包,路径为jar包的绝对路径,例如:C:\ProgramFiles\ Java \jre1.8.0_65\lib\Access_JDBC30.jar。如果CLASSPATH中已经有了其他的值,最后添加该包就可以了。然后再工程里面设置好,至此就可以正常连接数据库了,但是驱动的名称就不是sun.jdbc.odbc.JdbcOdbcDriver,而是com.hxtt.sql.access.AccessDriver,数据库路径也可以采用直连,URL可以设置为jdbc:Access:///d:MYDB.accdb。

二、源码:

D134_1_JDBCConnection.java
https://github测试数据/ruigege66/Java/blob/master/D134_1_JDBCConnection.java

以上就是Java 如何使用JDBC连接数据库的详细内容,更多关于Java 使用JDBC连接数据库的资料请关注其它相关文章!

原文链接:https://HdhCmsTestcnblogs测试数据/ruigege0000/p/13460865.html

查看更多关于Java 如何使用JDBC连接数据库的详细内容...

  阅读:23次