JDBC, Java Database Connecive, Java 数据库连接,是一组专门负责连接并操作数据库的标准,在整个JDBC 中实际上大量的提供的是接口。针对于各个不同的数据库生产商 ,只要想使用JAVA 进行数据库的开发,则对这些标准有所支持。
JDBC 在使用中常见的有以下三类:
JDBC-ODBC 桥连接:是SUN 在JDK的开发包中提供的最标准的一套JDBC 操作类库,使用的时候将JDBC-ODB-数据库,中间要经过一个ODBC 的连接,那么就意味着整体的性能将会降低,所以在开发中是绝对不会去使用JDBC-ODBC的连接方式的。
JDBC 连接,使用各个数据库提供商给定的数据库驱动程序,完成JDBC的开发,使用的时候需要在classpath中配置数据库的驱动程序
JDBC 网络连接:主要使用通过网络连接数据库
JDBC 的操作步骤
在进行JDBC 操作的时候可以按照以下的步骤完成:
1、加载数据库驱动程序,加载的时候需要将驱动程序配置到classpath之中
2、连接数据库,通过Connection 接口和 DriverManager 类完成
3、操作数据库,通过Statement、PreparedStatement、ResultSet 三个接口完成
4、关闭数据库,在实际开发中数据库资源非常有限,操作完之后必须关闭
数据库连接操作
在JDBC 的操作中,如果要想进行数据库的连接,则必须按照以上的几步完成
1、通过Class.forName()加载数据库的驱动程序
2、通过DriverManager 类进行数据库的连接,连接的时候要输入数据库的连接地址、用户名、密码
3、通过Connection 接口接收连接
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 package org.connectiondemo;import java.sql.Connection;import java.sql.DriverManager;public class ConnectionJDBC { * @param args */ public static final String DBDRIVER = "com.mysql.jdbc.Driver" ; public static final String DBURL = "jdbc:mysql://localhost:3306/test" ; public static final String DBUSER = "root" ; public static final String DBPASS = "" ; public static void main(String [] args) throws Exception { Connection con = null ; Class.forName(DBDRIVER); con = DriverManager.getConnection(DBURL,DBUSER,DBPASS); System.out.println (con); con.close(); } }
数据库更新操作
如果要想执行数据库的更新操作,则可以使用Statement接口,数据库更新就是增加、修改、删除
增加、更新、删除操作
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 package org.updatedemo;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class UpdateDemo { * @param args */ public static final String DBDRIVER = "com.mysql.jdbc.Driver" ; public static final String DBURL = "jdbc:mysql://localhost:3306/test" ; public static final String DBUSER = "root" ; public static final String DBPASS = "" ; public static void main(String [] args) throws Exception { Connection con = null ; Statement stmt = null ; Class.forName(DBDRIVER); con = DriverManager.getConnection(DBURL,DBUSER,DBPASS); stmt = con.createStatement(); stmt.executeUpdate("insert into java_study.person values(\'Tom\',20,\'SH\')" ); stmt.executeUpdate("update java_study.person set name='Jery' where age = 20" ); stmt.executeUpdate("delete from java_study.person where age = 20" ); con.close(); } }
数据库查询操作
通过SELECT 语句,可以查询数据中的内容,在mysql 中直接查询的时候可以发现将返回全部的查询结果给用户看,那么对于程序的操作中也是一样的,所有的查询结果要返回到程序处进行输出查看,那么程序通过ResultSet 接口保存全部的查询结果,通过Statement 接口中的executeQuery()方法查询。
查询之后的数据需要分别的取出。通过 next ()方法找到返回的每一行数据,每一行中各个列的数据需要通过getXxx()方法取得,例如:
取得整型:getInt()
取得字符串:getString()
取得日期:getDate()
取得小数:getFloat()
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 package org.querydemo; import java.sql.Connection ;import java.sql.DriverManager ;import java.sql.ResultSet ;import java.sql.Statement ;public class QueryDemo { /** * @param args */ //驱动程序就是之前在classpath中配置的JDBC 的驱动程序的JAR 包中 public static final String DBDRIVER = "com.mysql.jdbc.Driver" ; //连接地址是由各个数据库生产商单独提供的,所以需要单独记住 public static final String DBURL = "jdbc:mysql://localhost:3306/test" ; //连接数据库的用户名 public static final String DBUSER = "root" ; //连接数据库的密码 public static final String DBPASS = "" ; public static void main(String [] args) throws Exception { Connection con = null; //表示数据库的连接对象 Statement stmt = null; //表示数据库的更新操作 ResultSet result = null; //表示接收数据库的查询结果 Class .forName(DBDRIVER ); //1 、使用CLASS 类加载驱动程序 con = DriverManager .getConnection(DBURL ,DBUSER ,DBPASS ); //2 、连接数据库 stmt = con.createStatement(); //3 、Statement 接口需要通过Connection 接口进行实例化操作 result = stmt .executeQuery("select name,age,address from java_study.person" ); //执行SQL 语句,查询数据库 while (result .next()){ String name = result .getString("name" ); int age = result .getInt("age" ); String address = result .getString("address" ); System .out .println(name+age+address); } result .close(); con.close(); // 4 、关闭数据库 } }
PreparedStatement
此接口是在实际的开发中使用最广泛的一个操作接口,此接口是Statement接口的子接口,采用预处理的方式完成
完成增加操作
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 package org.preparedstatementdemo;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.util.Date;public class PreparedStatementDemo { * @param args */ public static final String DBDRIVER = "com.mysql.jdbc.Driver" ; public static final String DBURL = "jdbc:mysql://localhost:3306/test" ; public static final String DBUSER = "root" ; public static final String DBPASS = "" ; public static void main(String [] args) throws Exception { Connection con = null ; PreparedStatement pstmt = null ; String name = "Tom" ; int age = 20 ; Date date = new Date(); String sql = "insert into java_study.person values(?,?,?)" ; Class.forName(DBDRIVER); System.out.println (sql); con = DriverManager.getConnection(DBURL,DBUSER,DBPASS); pstmt = con.prepareStatement(sql); pstmt.setString(1 , name); pstmt.setInt(2 , age); pstmt.setDate(3 , new java.sql.Date(date.getTime())); pstmt.executeUpdate(); pstmt.close(); con.close(); } }
完成模糊操作
模糊查询要使用LIKE 语句,那么LIKE 语句需要使用%进行匹配
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 package org.preparedstatementquery; import java.sql.Connection ;import java.sql.DriverManager ;import java.sql.PreparedStatement ;import java.sql.ResultSet ;import java.util.Date ;public class PreparedStatementQuery { /** * @param args */ // TODO Auto -generated method stub public static final String DBDRIVER = "com.mysql.jdbc.Driver" ; //连接地址是由各个数据库生产商单独提供的,所以需要单独记住 public static final String DBURL = "jdbc:mysql://localhost:3306/test" ; //连接数据库的用户名 public static final String DBUSER = "root" ; //连接数据库的密码 public static final String DBPASS = "" ; public static void main(String [] args) throws Exception { Connection con = null; //表示数据库的连接对象 PreparedStatement pstmt = null; //表示数据库更新操作 ResultSet result = null; String like_name ="Tom1" ; int like_age = 12 ; String sql = "select name,age,birthday from java_study.person where name like ? or age = ?" ; Class .forName(DBDRIVER ); //1 、使用CLASS 类加载驱动程序 System .out .println(sql); con = DriverManager .getConnection(DBURL ,DBUSER ,DBPASS ); //2 、连接数据库 pstmt = con.prepareStatement(sql); //使用预处理的方式创建对象 pstmt.setString(1 , "%" +like_name+"%" ); pstmt.setInt(2 , like_age); result = pstmt.executeQuery(); //执行SQL 语句,更新数据库 while (result .next()){ String name = result .getString("name" ); int age = result .getInt("age" ); Date date = result .getDate("birthday" ); System .out .println(name+"," +age+"," +date); } result .close(); pstmt.close(); con.close(); // 4 、关闭数据库 } }
批处理
批处理,多条SQL 语句可以一次性执行完毕,称为批处理操作
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 package org.addbetchdemo;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.util.Date;public class AddDetchDemo { * @param args */ public static final String DBDRIVER = "com.mysql.jdbc.Driver" ; public static final String DBURL = "jdbc:mysql://localhost:3306/test" ; public static final String DBUSER = "root" ; public static final String DBPASS = "" ; public static void main(String [] args) throws Exception { Connection con = null ; PreparedStatement pstmt = null ; String sql = "insert into java_study.person values(?,?,?)" ; Class.forName(DBDRIVER); System.out.println (sql); con = DriverManager.getConnection(DBURL,DBUSER,DBPASS); pstmt = con.prepareStatement(sql); for (int i = 0 ;i<10 ;i++){ pstmt.setString(1 , "org" +i); pstmt.setInt(2 , i); pstmt.setDate(3 , new java.sql.Date(new java.util.Date().getTime())); pstmt.addBatch(); } int [] i = pstmt.executeBatch(); System.out.println (i); pstmt.close(); con.close(); } }
事务处理
通过设置 connection 的autocommit(false) 提交事务 con.commit(); 回滚事务 con.rollback()
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 package org.transactiondemo;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Statement;public class TransactionDemo { * @param args */ public static final String DBDRIVER = "com.mysql.jdbc.Driver" ; public static final String DBURL = "jdbc:mysql://localhost:3306/test" ; public static final String DBUSER = "root" ; public static final String DBPASS = "" ; public static void main(String [] args) throws Exception { Connection con = null ; Statement stmt = null ; Class.forName(DBDRIVER); con = DriverManager.getConnection(DBURL,DBUSER,DBPASS); con.setAutoCommit(false ); stmt = con.createStatement(); try { stmt.addBatch("insert into java_study.person values(\'Tom\',20,\'2012-01-01\')" ); stmt.addBatch("insert into java_study.person values(\'Tom\',20,\'2012-01-01\')" ); stmt.addBatch("insert into java_study.person values(\''Tom\',20,\'2012-01-01\')" ); stmt.executeBatch(); con.commit(); } catch (Exception e){ con.rollback(); } stmt.close(); con.close(); } }
原文:JAVA JDBC - IQuicksandI的专栏 - 博客频道 - CSDN.NET