JDBC(1)

JDBC,全称Java Database Connectivity,Java数据库连接,包括了一组与数据库交互的api,还有与数据库进行通信的驱动程序。

  • 在项目中使用JDBC
  1. 导入JDBC驱动,比如mysql-connector-java-5.1.35.jar包。
  2. 打开连接:使用 DriverManager.getConnection() 方法创建Connection 对象,这代表与数据库的物理连接。
  3. 执行查询:使用类型声明的对象建立并提交一个 SQL 语句到数据库。
  4. 从结果集中提取数据:使用 ResultSet.getXXX() 方法来检索结果集的数据。
  5. 处理结果集:对得到的结果集进行相关的操作。
  6. 清理环境:需要明确地关闭所有的数据库资源,释放内存。

代码示例:

import java.sql.*;



public class JdbcTest {

   // JDBC 驱动器名称 

   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  

   //数据库地址 ,数据库的名称为 EXAMPLE

   static final String DB_URL = "jdbc:mysql://localhost/EXAMPLE";



   //  数据库用户和密码

   static final String USER = "root";



   static final String PASS = "";



   public static void main(String[] args) {

       Connection conn = null;

       Statement stmt = null;

       try{

           //注册JDBC 驱动程序

           Class.forName(JDBC_DRIVER);



           //打开连接

           System.out.println("Connecting to database...");

           conn = DriverManager.getConnection(DB_URL,USER,PASS);



           //执行查询

           System.out.println("Creating statement...");

           stmt = conn.createStatement();

           String sql;

           sql = "SELECT id, name, age FROM Students";

           ResultSet rs = stmt.executeQuery(sql);



           //得到和处理结果集

           while(rs.next()){

               //检索

               int id  = rs.getInt("id");

               int age = rs.getInt("age");

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



               //显示

               System.out.print("ID: " + id);

               System.out.print(", Age: " + age);

               System.out.print(", Name: " + name);

               System.out.println();

           }

        

           rs.close();

           stmt.close();

           conn.close();

       }catch(SQLException se){

           // JDBC 操作错误

           se.printStackTrace();

       }catch(Exception e){

           // Class.forName 错误

           e.printStackTrace();

       }finally{

           //这里一般用来关闭资源的

           try{

               if(stmt!=null)

                   stmt.close();

           }catch(SQLException se2){

           }

           try{

               if(conn!=null)

                   conn.close();

           }catch(SQLException se){

               se.printStackTrace();

           }

       }

   }

}
  • 注册JDBC驱动程序

使用驱动程序之前,必须先注册驱动程序,注册驱动程序的本质就是讲驱动类文件动态地加载到内存里面去。两种方法:

1、Class.forName();

   Class.forName("com.mysql.jdbc.Driver");

2、DriverManager.registerDriver();

   Driver driver = new com.mysql.jdbc.Driver();

   DriverManager.registerDriver(driver);
  • 指定连接数据库的URL

下面是流行的JDBC驱动程序名和数据库的URL。

RDBMS       JDBC驱动程序的名称                           URL

Mysql        com.mysql.jdbc.Driver                        jdbc:mysql://hostname/ databaseName

Oracle    oracle.jdbc.driver.OracleDriver   jdbc:oracle:thin:@hostname:port Number:databaseName

DB2        COM.ibm.db2.jdbc.net.DB2Driver       jdbc:db2:hostname:port Number/databaseName

Sybase    com.sybase.jdbc.SybDriver      jdbc:sybase:Tds:hostname: port Number/databaseName

  • 创建数据库连接对象

有三种方式:

1、getConnection(String url)

String URL = "jdbc:mysql://localhost/EXAMPLE?user=root&password=123456";

Connection conn = DriverManager.getConnection(URL);

2、 getConnection(String url, Properties prop)

import java.util.*;

String URL = "jdbc:mysql://localhost/EXAMPLE";

Properties pro = new Properties( );

//Properties对象,保存一组关键字-值对

pro.put( "user", "root" );

pro.put( "password", "123456" );

Connection conn = DriverManager.getConnection(URL, pro);

3、 getConnection(String url, String user, String password)

String URL = "jdbc:mysql://localhost/EXAMPLE";

String USER = "username";

String PASS = "password"

Connection conn = DriverManager.getConnection(URL, USER, PASS);

 

 

原文地址:https://www.cnblogs.com/lingqinyu/p/8877766.html