JDBC 连接数据库
一、什么是 JDBC
JDBC(Java DataBase Connectivity,Java 数据库连接)是一种用于执行 SQL 语句的 Java API,可以为多种关系型数据库提供统一访问,它由一组用 Java 语言编写的类和接口组成。JDBC 提供了一种基准,据此可以构建更高级的工具和接口,使数据库开发人员能够编写数据库应用程序,同时,JDBC 也是个商标名。
二、JDBC 连接数据库的步骤
(1)利用类的反射来获取驱动
Class.forName(driverClass) 加载 mysql 驱动:Class.forName("com.mysql.jdbc.Driver"); 加载 oracle 驱动:Class.forName("oracle.jdbc.driver.OracleDriver");
(2)获得数据库连接对象 Connection
获取 mysql 连接
DriverManager.getConnection("jdbc:mysql://localhost:3306/libin?useUnicode=true&characterEncoding=UTF8",user,password);
获取 oracle 连接
DriverManager.gerConnection(URL,user,password); DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","123");
(3)创建语句
PreparedStatement ps = conn.prepareStatement(sql);
(4)执行语句得到结果集 ResultSet
ResultSet rs = ps.executeQuery();
(5)遍历结果集 ResultSet
(6)关闭连接、释放资源
三、连接数据的实例
创建项目、导入架包
1、连接 oracle 数据库
package com.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class TestOracleJDBC { public static void main(String[] args) { Connection conn = null; try { // 1.加载驱动 Class.forName("oracle.jdbc.driver.OracleDriver"); // 2.获取连接Connection对象 conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "123456"); String sql = "select * from emp"; // 3.得到语句PreparedStatement PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); // 4.遍历结果集 while (rs.next()) { System.out.println(rs.getString("EMPNO") + "\t" + rs.getString("ENAME") + "\t" + rs.getString("MGR") + "\t" + rs.getString("HIREDATE") + "\t" + rs.getString("SAL") + "\t" + rs.getString("COMM") + "\t" + rs.getString("DEPTNO")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { if (conn != null) { try { // 5.关闭连接,释放资源 conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
2、连接 mysql 数据库
package com.jdbc; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class TestMysqlJDBC { private static final String url = "jdbc:mysql://localhost:3306/springmvc?useUnicode=true&" + "characterEncoding=UTF8"; private static final String username = "root"; private static final String password = "123456"; public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { // 1.加载mysql驱动程序 Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接对象 conn = DriverManager.getConnection(url, username, password); String sql = "select * from student"; // 3.通过连接对象获得ps语句 ps = conn.prepareStatement(sql); rs = ps.executeQuery(); // 4.遍历结果集 while (rs.next()) { System.out.println(rs.getInt("sno") + "\t" + rs.getString("sname") + "\t" + rs.getInt("gender") + "\t" + rs.getInt("grade") + "\t" + rs.getInt("age")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); // 5.finally语句块中释放资源 } finally { try { if (rs != null) { rs.close(); } if (ps != null) ps.close(); if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
四、其它链接
源代码
参考资料
来源:https://www.cnblogs.com/hglibin/p/9556465.html