对于一个JDBC程序的解读

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=system

password=openlab

上面这些数据是单独放在一个叫db_oracle.properties的一个配置文件当中的。

这个类是ConnectionUtils。


import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
//获得连接数据库的连接 并且关闭数据库
public class ConnectionUtils {
	private static String url;
	private static String driver;
	private static String username;
	private static String password;
	
	static{//程序装载的时候,只执行一次
		Properties props = new Properties();
		try{
			props.load(ConnectionUtils.class.getClassLoader()
					.getResourceAsStream("配置文件的位置/db_oracle.properties"));
			
		}catch(IOException e){
			e.printStackTrace();
		}
		if(props!=null){
			url = props.getProperty("url");
			driver = props.getProperty("driver");
			username = props.getProperty("username");
			password = props.getProperty("password");
			try{
				Class.forName(driver);
			}catch(ClassNotFoundException e){
				
			}
		}
	}
	//获得连接
	public static Connection openConnection() throws SQLException{
		return DriverManager.getConnection(url, username, password);
	}
	//关闭连接
	public static void closeConnection(Connection conn){
		try{
			if(conn != null){
				conn.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public static void closeStatement(Statement stmt){
		try{
			if(stmt != null){
				stmt.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	public static void closeResultSet(ResultSet rs){
		try{
			if(rs != null){
				rs.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}
ConnectionUtils的作用主要是从配置文件中获得到数据库的连接。是一个工具类。为什么要不连接的相关信息写在配置文件中,而不写在程序中,这也是计算机中常用的一个方法。主要是为了在调试的时候,方便。修改的时候容易。定位错误的时候能够很快定位到。这算是一个技巧吧!


import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

public class TestBatch {
	static Connection conn = null;
	static Statement stmt = null;
	public static void main(String[] args){
		batch();
	}
	
	public static void batch(){
		String sql1 = "insert into student values(200, 'rose', 19, 'F')";
		String sql2 = "insert into student values(210, 'martha', 25, 'F')";
		try{
		    conn = ConnectionUtils.openConnection();
			conn.setAutoCommit(false);
			stmt = conn.createStatement();
			stmt.addBatch(sql1);
			stmt.addBatch(sql2);
			int [] result = stmt.executeBatch();//批量执行
			conn.commit(); 
			for(int i:result){
				System.out.println(i);
			}
			
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			ConnectionUtils.closeConnection(conn);
			ConnectionUtils.closeStatement(stmt);
			
		}
		
	}
}

这个类的主要作用就是对数据库进行操作。

步骤就是:实现数据库连接,获得语句对象,执行SQL语句。而这里用到了addBatch方法,这个方法是把SQL语句加入到缓冲区。利用executeBatch()方法,批量执行SQL语句,主要目的是为了提高效率。数据库很多的时间主要花在了I/O上,减少I/O的情况下,得到相应的结果,就能够提高效率。

单独把数据库的连接和资源的关闭写在一个类里边,而不业务逻辑写在另一个类中,这样在调试程序的时候,能够很快定位程序的bug,提高调试效率。








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值