JDBC 常用函数实例说明

        对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;
	}
	
}


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值