日期:2014-05-16  浏览次数:20459 次

JDBC用PreparedStatement完成批量添加vs普通添加
public class WeatherDao {
	private static Connection conn = null;
	private static PreparedStatement pstmt = null;
	
	/**
	 * Author:Allison 
	 * Date:2013-04-02 
	 * Description:执行批量添加
	 */	
	public void addWeatherBatch(List<WeatherData> list) {
		conn = DBConn.getConnection();
		try {
			conn.setAutoCommit(false);
			String sql = "insert into weather_data(currentDate,highTemp,lowTemp,weather,windDirection,windPower,week,city)values(?,?,?,?,?,?,?,?)";
			pstmt = conn.prepareStatement(sql);
			for (WeatherData WeatherData : list) {
				pstmt.setString(1, WeatherData.getcurrentDate());
				pstmt.setDouble(2, WeatherData.getHighTemp());
				pstmt.setDouble(3, WeatherData.getLowTemp());
				pstmt.setString(4, WeatherData.getWeather());
				pstmt.setString(5, WeatherData.getWindDirection());
				pstmt.setString(6, WeatherData.getWindPower());
				pstmt.setString(7, WeatherData.getWeek());
				pstmt.setString(8, WeatherData.getCity());
				pstmt.addBatch();
			}
			pstmt.executeBatch();
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {

			try {
				DBConn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Author:Allison datatime:2013-03-28 introduction:添加weather详细信息
	 */
	public boolean addWeather(WeatherData weather) {
		// step1 定义sql语句
		String sqlText = "insert into weather_data(currentDate,highTemp,lowTemp,weather,windDirection,windPower,week,city)values(?,?,?,?,?,?,?,?)";
		Object[] oParams = { weather.getcurrentDate(), weather.getHighTemp(),
				weather.getLowTemp(), weather.getWeather(),
				weather.getWindDirection(), weather.getWindPower(),
				weather.getWeek(), weather.getCity() };
		// step2 执行sql语句
		// 执行sql命令
		int iRs = DBConn.exePreIUD(sqlText, oParams);
		// 关闭DBConn对象
		DBConn.close();
		// step3 返回结果
		return iRs > 0 ? true : false;
	}

	/*
	 * Author:Allison datatime:2011-10-19introduct:查询日志
	 */
	public ArrayList<WeatherData> findWeatherByDateCity(String date, String city){
		
		//step1 定义sql语句
		String sqlText="select currentDate,city from weather_data where currentDate='"+ date +"' and city ='"+city+"'";
		//step2 执行sql语句
		ResultSet rs = DBConn.exeR(sqlText);
		try {
			ArrayList<WeatherData> list = new ArrayList<WeatherData>();
			while(rs.next()){
				//定义一个WeatherData对象类型
				WeatherData cobject = new WeatherData();
				cobject.setcurrentDate(rs.getString(1));
				cobject.setCity(rs.getString(2));
				list.add(cobject);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally{
			DBConn.close();
		}
	}
}

?

public class DBConn {
	// 四个核心变量、六个方法
	private static Connection conn = null;// 连接数据库
	private static Statement stmt = null;// 发送SQL命令
	private static PreparedStatement pstmt = null;// 发送带参数的sql命令
	private static ResultSet rs = null;// 获得返回的数据集

	public static Connection getConnection() {
		// step1:找驱动
		try {
			Class.forName(Config.DRIVER);
			conn = DriverManager.getConnection(Config.URL + Config.DBNAME,
					Config.DBUSERNAME, Config.DBPASS);
			// DatabaseMetaData dbmd = conn.getMetaData();
			// System.out.println("db name: " + dbmd.getDatabaseProductName());
			// System.out.println("tx: " + dbmd.supportsTransactions());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	/**
	 * Author:Allison 
	 * Date:2011-10-12 
	 * Description:执行RUD操作
	 */
	public static int exeIUD(String sqlText) {
		// step1:创建连接
		getConnection();
		// step2:判断连接
		if (conn != null) {
			try {
				// step3:定义statement对象
				stmt = conn.createStatement();
				// step4:执行sql命令
				int iRs = stmt.executeUpdate(sqlText);
				retur