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.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","scott","123");
DriverManager.gerConnection(URL,user,password);
(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:@localhost:1521:orcl","scott","123"); 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/libin?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("id")+"\t"+rs.getString("name")+"\t"+rs.getInt("age")); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); //3.finally语句块中释放资源 } finally{ try{ if(rs!=null){ rs.close(); } if(ps!=null) ps.close(); if(conn!=null) conn.close(); }catch(SQLException e){ e.printStackTrace(); } } } }
源代码可见:https://github.com/hglibin/mygame