JDBC

JDBC在使用中常见的有以下三类:

1,JDBC-ODBC桥连接:是SUN在jdk的开发包中提供的最标准的一套jdbc操作类库,是i用的时候是将JDBC-ODBC-数据库,中间要经过一个ODBC的连接,那么就i意味着整体的性能将会降低,所以在开发中是不会去使用这种方式连接的。

2,JDBC连接,使用各个数据库提供商给定的数据库驱动程序,完成JDBC的开发,使用的时候需要在classpath中配置数据库的驱动程序。

3,JDBC网络连接:主要使用通过网络连接数据库。

JDBC的操作步骤:

1,加载数据库驱动程序,加载的时候需要将驱动程序配置到classpath之中。

2,连接数据库,通过Connection接口和DriverManager类完成

3,操作数据库,通过Statement,PreparedStatement,ResultSet三个接口完成

4,关闭数据库,在实际开发中数据库资源非常有限,操作完之后必须关闭

使用JDBC的方式连接Oracle数据库,首先oracle中提供了专门的JDBC驱动程序,驱动程序路径:\oracle\product\10.1.0\db_1\lib\classes12.jar

一,数据库连接操作

1,通过Class.forName()加载数据库的驱动程序

2,通过DriverManager类进行数据库的连接,连接的时候要输入数据库的连接地址,用户名,密码

3,通过Connection接口接收连接

import java.sql.*;
public class ConnectJDBC {
 public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
 public static final String DBURL = "jdbc:oracle:thin:@192.168.1.101:1521:mldn";
 // 连接数据库的用户名
 public static final String DBUSER = "scott";
 // 连接数据库的密码
 public static final String DBPASS = "tiger";
 public static void main(String[] args) throws Exception {
  // 1、使用Class类加载驱动程序
  Class.forName(DBDRIVER).newInstance();
  // 2、连接数据库
  Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
  // 3、Statement接口实例化
  Statement stmt=conn.createStatement();
  
  //执行更新语句
  //String sql="insert into person(pid,name,age,birthday,salary) values('111','jin',30,to_date('2009年10月20日','yyyy-mm-dd'),9000)";
  //stmt.executeUpdate(sql);
  
  //执行查询语句
  String sql="select * from emp";
  ResultSet result=stmt.executeQuery(sql);
  while(result.next())
  {
   //int pid=result.getInt("empno");
            int pid=result.getInt(1);
   System.out.println(pid);
  }
  // 4、关闭数据库
  result.close();
  stmt.close();
  conn.close();
 }
}
 

使用Statement的子接口PreparedStatement开发。

 import java.sql.*;
public class ConnectJDBC {
 public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
 public static final String DBURL = "jdbc:oracle:thin:@192.168.1.101:1521:mldn";
 // 连接数据库的用户名
 public static final String DBUSER = "scott";
 // 连接数据库的密码
 public static final String DBPASS = "tiger";
 public static void main(String[] args) throws Exception {
  // 1、使用Class类加载驱动程序
  Class.forName(DBDRIVER).newInstance();
  // 2、连接数据库
  Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
  // 3、Statement接口实例化
  String sql="insert into person(pid,name,age,birthday,salary) values('111','jin',?,?,?)";
  PreparedStatement stmt=conn.prepareStatement(sql);
  //执行更新语句
  stmt.setInt(1, 10);
  Date date=new Date(20110210);
  //将util中的date转换为slq中的date
     stmt.setDate(2, new java.sql.Date(date.getTime()));
  stmt.executeUpdate();
  // 4、关闭数据库
  stmt.close();
  conn.close();
 }
}

使用PreparedStatement也可以执行查询语句

模糊查询:
stmt.setString(1,"%"+keyword+"%");

批处理

在Statement接口上定义了一个addBatch()方法,此方法可以用于加入批处理,之后使用executeBatch()方法执行批处理的操作。

import java.sql.*;
public class ConnectJDBC {
 public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
 public static final String DBURL = "jdbc:oracle:thin:@192.168.1.101:1521:mldn";
 // 连接数据库的用户名
 public static final String DBUSER = "scott";
 // 连接数据库的密码
 public static final String DBPASS = "tiger";
 public static void main(String[] args) throws Exception {
  // 1、使用Class类加载驱动程序
  Class.forName(DBDRIVER).newInstance();
  // 2、连接数据库
  Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
  // 3、Statement接口实例化
  String sql="insert into person(pid,name,age,birthday,salary) values('111','jin',?,?,?)";
  PreparedStatement stmt=conn.prepareStatement(sql);
  for(int i=0;i<10;i++){
   //执行更新语句
   stmt.setInt(1, 10);
   Date date=new Date(20110210);
   //将util中的date转换为slq中的date
      stmt.setDate(2, new java.sql.Date(date.getTime()));
      //增加批处理
      stmt.addBatch();
  }
  //执行批处理
  int i[]=stmt.executeBatch();
  System.out.println(i.length);
  // 4、关闭数据库
  stmt.close();
  conn.close();
 }
}

事务处理

在Connection操作中所有的数据库更新属于立即更新的,如果要想进行事务的操作,则首先应该停止自动更新操作,之后所有的更新通过commit()进行提交,如果有问题,则回滚。

import java.sql.*;
public class ConnectJDBC {
 public static final String DBDRIVER = "oracle.jdbc.driver.OracleDriver";
 public static final String DBURL = "jdbc:oracle:thin:@192.168.1.101:1521:mldn";
 // 连接数据库的用户名
 public static final String DBUSER = "scott";
 // 连接数据库的密码
 public static final String DBPASS = "tiger";
 public static void main(String[] args) throws Exception {
  // 1、使用Class类加载驱动程序
  Class.forName(DBDRIVER).newInstance();
  // 2、连接数据库
  Connection conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
  // 3、Statement接口实例化
  String sql="insert into person(pid,name,age,birthday,salary) values('111','jin',?,?,?)";
  //关掉自动提交
  conn.setAutoCommit(false);
  Statement stmt=conn.createStatement();
  try {
   stmt.addBatch(sql);
   stmt.addBatch(sql);
   stmt.addBatch(sql);
   stmt.addBatch(sql);
   //执行批处理
   int i[]=stmt.executeBatch();
   //事务处理
   conn.commit();
  } catch (Exception e) {
   //事务出错回滚
   conn.rollback();
  }  
  // 4、关闭数据库
  stmt.close();
  conn.close();
 }
}

原文地址:https://www.cnblogs.com/jinzhengquan/p/1949056.html