对JDBC中常用到的一些函数进行了简单说明,写了一些实例来记录一下,里面涉及到查询,事务,插入数据的几种方式等。程序中连接的数据库为oracle,另外,如果涉及到大量数据插入的话,务必使用PreparedStatement+批处理的方式,可以最大可能的提高效率,这点比较有感受。主要还是通过程序来了解把。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
* @author xiaobaidsl@163.com
* 提供jdbc连接数据库中一些函数的用法实例,以oracle为操作对象
*/
public class DatabaseConnection {
public static void main(String[] args) {
DatabaseConnection d = new DatabaseConnection();
Connection conn = d.connectionTest();
Statement stmt = null;
ResultSet rs = null;
try {
stmt = conn.createStatement();
//**********事务操作
conn.setAutoCommit(false);
//**********插入操作
String sql1 = "insert into motorcycle (id,licence,device_num) values('11111111111','11111111111','11111111111') ";
stmt.execute(sql1);
String sql2 = "insert into motor_users(username,pwd,gender,register_time,phone_number) values('11111111111','123456','m',to_timestamp('2014-06-16 12:03:05','yyyy-mm-dd hh24:mi:ss'),'11111111111')";
stmt.execute(sql2);
//**********查询操作
String sql3 = "select id from motor_users where username = '11111111111'";
String userid = "";
rs = stmt.executeQuery(sql3);
if (rs.next()) {
userid = String.valueOf(rs.getInt("id"));
}
System.out.println("查找结果"+ userid);
conn.commit();
//******************下面是三种插入数据的方式,插入效率各有不同***********************
//**** 方式1 循环Statement插入
for (int i = 0; i < 100; i++) {
String sql6 = "insert into motorcycle (id,licence,device_num) values('11111111111','11111111111','11111111111') ";
stmt.execute(sql6);
}
//**** 方式2 PreparedStatement插入,该方式有以下几点好处:
/*
* 一.代码的可读性和可维护性.代码无论从可读性还是可维护性上来说.都比直接用Statement的代码高很多档次
* 二.PreparedStatement尽最大可能提高性能.
* 三.提高了安全性. 定好了参数可以防止恶意代码注入
*/
PreparedStatement pstmt = conn.prepareStatement("insert into user values(?)");
for (int i = 0; i < 100; i++) {
pstmt.setString(1, "username");
pstmt.execute();
}
//**** 方式3 PreparedStatement+批处理 这种方式处理比前两种快几十倍到上百倍应该是有的,1秒5000条应该没问题
//批量插入(提高插入效率)
PreparedStatement pstmt2 = conn.prepareStatement("insert into user values(?)");
for (int i = 0; i < 100; i++) {
pstmt2.setString(1, "username");
pstmt2.addBatch();
}
pstmt2.executeBatch();
pstmt2.clearBatch();
pstmt2.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
if (conn!=null) {
try {
conn.rollback();
conn.setAutoCommit(true);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}finally{
d.close(conn,stmt,rs);
}
}
/**
* 关闭数据库连接
* @param conn
* @param stmt
* @param rs
*/
private void close(Connection conn, Statement stmt, ResultSet rs) {
// TODO Auto-generated method stub
try {
if (rs!=null){
rs.close();
}
if (stmt!=null) {
stmt.close();
}
if (conn!=null) {
conn.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.toString());
}
}
/**
* 获取数据库连接
* @return
*/
public Connection connectionTest() {
// 定义一个连接对象
Connection conn = null;
// 定义连接数据库的URL资源
String url = "jdbc:oracle:thin:@localhost:1521:webgps3"; //这里指定的是sid,而不是service_name
// 定义连接数据库的用户名称与密码
String username = "scott";
String password = "Gpsserver12345";
// 加载数据库连接驱动
String className = "oracle.jdbc.driver.OracleDriver";
try {
Class.forName(className);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 获取数据库的连接对象
try {
conn = DriverManager.getConnection(url, username, password);
System.out.println("数据库连接建立成功...");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
}