前言
Java数据库连接,(Java Database Connectivity,简称JDBC)是Java语言中用来规范客户端程序如何来访问数据库的应用程序接口,提供了诸如查询和更新数据库中数据的方法。
JDBC API主要位于JDK中的java.sql包中(之后扩展的内容位于javax.sql包中),主要包括(斜体代表接口,需驱动程序提供者来具体实现):
DriverManager:负责加载各种不同驱动程序(Driver),并根据不同的请求,向调用者返回相应的数据库连接(Connection)。Driver:驱动程序,会将自身加载到DriverManager中去,并处理相应的请求并返回相应的数据库连接(Connection)。Connection:数据库连接,负责与进行数据库间通讯,SQL执行以及事务处理都是在某个特定Connection环境中进行的。可以产生用以执行SQL的Statement。Statement:用以执行SQL查询和更新(针对静态SQL语句和单次执行)。PreparedStatement:用以执行包含动态参数的SQL查询和更新(在服务器端编译,允许重复执行以提高效率)。CallableStatement:用以调用数据库中的存储过程。SQLException:代表在数据库连接的建立和关闭和SQL语句的执行过程中发生了例外情况(即错误)。
准备工作
以mysql为例,需要引入数据库驱动
1 <!--mysql驱动坐标--> 2 <dependency> 3 <groupId>mysql</groupId> 4 <artifactId>mysql-connector-java</artifactId> 5 <version>5.1.6</version> 6 <scope>runtime</scope> 7 </dependency>
1.查询
1 public static void main(String[] args) throws SQLException { 2 Connection connection = null; 3 PreparedStatement preparedStatement = null; 4 ResultSet resultSet = null; 5 try { 6 //1.加载数据库驱动 7 Class.forName("com.mysql.jdbc.Driver"); 8 //2.通过驱动管理类获取数据库链接 9 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_jdbc","root","123456"); 10 //3.定义sql语句,?表示占位符 11 String sql = "select * from user where username = ?"; 12 //4.获取预处理statement 13 preparedStatement = connection.prepareStatement(sql); 14 //5.设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值 15 preparedStatement.setString(1, "zed"); 16 //6.向数据库发出sql,执行查询,查询出结果集 17 resultSet = preparedStatement.executeQuery(); 18 //7.遍历查询结果集 19 while (resultSet.next()) { 20 int id = resultSet.getInt("id"); 21 String username = resultSet.getString("username"); 22 User user = new User(); 23 user.setId(id); 24 user.setUsername(username); 25 System.out.println(user); 26 } 27 } catch (ClassNotFoundException e) { 28 e.printStackTrace(); 29 } finally { 30 //8.释放资源 31 if (resultSet != null) { 32 resultSet.close(); 33 } 34 if (preparedStatement != null) { 35 preparedStatement.close(); 36 } 37 if (connection != null) { 38 connection.close(); 39 } 40 } 41 }
2.插入
1 public static void main(String[] args) throws SQLException { 2 Connection connection = null; 3 PreparedStatement preparedStatement = null; 4 try { 5 //1.加载数据库驱动 6 Class.forName("com.mysql.jdbc.Driver"); 7 //2.通过驱动管理类获取数据库链接 8 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_jdbc","root","123456"); 9 //3.定义sql语句,?表示占位符 10 String sql = "insert into user (id, username) values (?, ?)"; 11 //4.获取预处理statement 12 preparedStatement = connection.prepareStatement(sql); 13 //5.设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值 14 preparedStatement.setInt(1, 7); 15 preparedStatement.setString(2, "lux"); 16 //6.向数据库发出sql,执行插入,获取受影响的行数 17 int rows = preparedStatement.executeUpdate(); 18 System.out.println("受影响的行数:" + rows); 19 } catch (ClassNotFoundException e) { 20 e.printStackTrace(); 21 } finally { 22 //7.释放资源 23 if (preparedStatement != null) { 24 preparedStatement.close(); 25 } 26 if (connection != null) { 27 connection.close(); 28 } 29 } 30 }
3.更新
1 public static void main(String[] args) throws SQLException { 2 Connection connection = null; 3 PreparedStatement preparedStatement = null; 4 try { 5 //1.加载数据库驱动 6 Class.forName("com.mysql.jdbc.Driver"); 7 //2.通过驱动管理类获取数据库链接 8 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_jdbc","root","123456"); 9 //3.定义sql语句,?表示占位符 10 String sql = "update user set username = ? where id = ?"; 11 //4.获取预处理statement 12 preparedStatement = connection.prepareStatement(sql); 13 //5.设置参数,第一个参数为sql语句中参数的序号(从1开始),第二个参数为设置的参数值 14 preparedStatement.setString(1, "jax"); 15 preparedStatement.setInt(2, 7); 16 //6.向数据库发出sql,执行更新,获取受影响的行数 17 int rows = preparedStatement.executeUpdate(); 18 System.out.println("受影响的行数:" + rows); 19 } catch (ClassNotFoundException e) { 20 e.printStackTrace(); 21 } finally { 22 //7.释放资源 23 if (preparedStatement != null) { 24 preparedStatement.close(); 25 } 26 if (connection != null) { 27 connection.close(); 28 } 29 } 30 }
4.删除
1 public static void main(String[] args) throws SQLException { 2 Connection connection = null; 3 PreparedStatement preparedStatement = null; 4 try { 5 //1.加载数据库驱动 6 Class.forName("com.mysql.jdbc.Driver"); 7 //2.通过驱动管理类获取数据库链接 8 connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test_jdbc","root","123456"); 9 //3.定义sql语句,?表示占位符 10 String sql = "delete from user where id = ?"; 11 //4.获取预处理statement 12 preparedStatement = connection.prepareStatement(sql); 13 //5.设置参数,第一个参数为sql语句中参数的序号(从1开始) 14 preparedStatement.setInt(1, 7); 15 //6.向数据库发出sql,执行删除,获取受影响的行数 16 int rows = preparedStatement.executeUpdate(); 17 System.out.println("受影响的行数:" + rows); 18 } catch (ClassNotFoundException e) { 19 e.printStackTrace(); 20 } finally { 21 //7.释放资源 22 if (preparedStatement != null) { 23 preparedStatement.close(); 24 } 25 if (connection != null) { 26 connection.close(); 27 } 28 } 29 }
总结
1)加载数据库驱动
2)建立链接
3)执行SQL语句
4)处理结果
5)关闭链接