JAVA学习笔记(十一)连接数据库

添加好jar驱动包,例如Oracle下的"D:\app\junjun\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6_g.jar",下载并导入“javax.servlet.jsp.jstl-api-1.2.1.jar”。

import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.jsp.jstl.sql.*;

public class BaseDAO {
	/**
	* 用于存放数据库驱动的变量
	*/
	public static final String DRIVER="oracle.jdbc.driver.OracleDriver";
	/**
	 * 用于存放数据库链接字符串的变量
	 */
	public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
	//public static final String URL="jdbc:oracle:thin:@192.168.254.5:1521:orcl";
	/**
	 * 用于存放数据库链接用户的变量
	 */
	public static final String USER="system ";
	/**
	 * 用于存放数据库链接密码的变量
	 */
	public static final String PASSWORD="oracle";
	
	static Connection conn = null;
	static PreparedStatement pstmt = null;
	static ResultSet rs = null;
	
	static {
		try {
			Class.forName(DRIVER);
		}catch(ClassNotFoundException e) {
			/**
			 * 处理ClassNotFoundException无法获得驱动的异常
			 */
			e.printStackTrace();
		}
	}

	/**
	 * 获得数据库连接
	 * @return返回数据库连接
	 * @SQLException SQL的异常
	 */
	public static Connection getConnection(){
		try {
			/**
			 * 注册驱动
			 */
			//Class.forName(DRIVER);
			/**
			 * 获得数据库连接
			 */
			conn=DriverManager.getConnection(URL,USER,PASSWORD);
		} catch(SQLException e) {
			/**
			 * 处理SQLException异常
			 */
			e.printStackTrace();
		}
		/**
		 * 返回数据库连接
		 */
		return conn;
	}
	
	/**
	 * 释放资源
	 * @param conn
	 * @param pstmt
	 * @param rs
	 */
	public static void closeAll(Connection conn,PreparedStatement pst,ResultSet rs) {
		try {
		if(rs != null) {
				rs.close();
			}
			if(pst != null) {
				pst.close();
			}
			if(conn != null) {
				conn.close();
			}
		}catch(SQLException e) {
			/**
			 * 处理SQLException异常
			 */
			e.printStackTrace();
		}
	}
	
	public static ResultSet getResult(String preparedSql) {
		/**
		 * 得到数据库连接
		 */
		conn = getConnection();
		try {
			/**
			 * 得到PreparedStatement对象
			 */
			pstmt = conn.prepareStatement(preparedSql);
			rs = pstmt.executeQuery();
		}catch(SQLException e) {
			/**
			 * 处理SQLException异常
			 */
			e.printStackTrace();
		}
		return rs;
	}
	
	/**
	 * 执行SQL语句,可以进行增、删、改的操作,不能执行查询
	 * @param preparedSql  预编译的 SQL 语句
	 * @param param  预编译的 SQL 语句中的‘?’参数的字符串数组
	 * @return  返回影响行数
	 */
	public static int executeUpdate(String preparedSql, String[] param) {
		int result = 0;
		/**
		 * 得到数据库连接
		 */
		conn = getConnection();
		try {
			/**
			 * 得到PreparedStatement对象
			 */
			pstmt = conn.prepareStatement(preparedSql);
			if( param != null ) {
				for( int i=0;i<param.length;i++) {
					/**
					 * 为已预编译的sql设置参数
					 */
					pstmt.setString(i+1, param[i]);
				}
			}
			result = pstmt.executeUpdate();
		}catch(SQLException e) {
			/**
			 * 处理SQLException异常
			 */
			e.printStackTrace();
		}finally {
			/**
			 * 释放资源
			 */
			closeAll(conn,pstmt,null);
		}
		return result;
	}

	public static ResultSet executeQueryRS(String preparedSql,String[] param) {
		/**
		 * 处理SQL,执行SQL
		 */
		try {
			/**
			 * 得到数据库连接
			 */
			conn = getConnection();
			/**
			 * 得到prepareStatement对象
			 */
			pstmt = conn.prepareStatement(preparedSql);
			if(param!=null){
				for(int i=0;i<param.length;i++) {
					/**
					 * 为已预编译的sql设置参数
					 */
					pstmt.setString(i+1, param[i]); 
				}
			}
			/**
			 * 执行SQL语句
			 */
			rs = pstmt.executeQuery();
			return rs;
		}catch (SQLException e) {
			/**
			 * 处理SQLException异常
			 */
			e.printStackTrace();
		}
		return rs;
	}
	
	public static Result executeQuery(String preparedSql,String[] param) {
		Result result = null;
		/**
		 * 处理SQL,执行SQL
		 */
		try {
			/**
			 * 得到数据库连接
			 */
			conn = getConnection();
			/**
			 * 得到prepareStatement对象
			 */
			pstmt = conn.prepareStatement(preparedSql);
			if(param!=null){
				for(int i=0;i<param.length;i++) {
					/**
					 * 为已预编译的sql设置参数
					 */
					pstmt.setString(i+1, param[i]); 
				}
			}
			/**
			 * 执行SQL语句
			 */
			rs=pstmt.executeQuery();
			/**
			 * 将resultset中的数据转移到result中,为了关闭resultset
			 */
			result = ResultSupport.toResult(rs);
		}catch (SQLException e) {
			/**
			 * 处理SQLException异常
			 */
			e.printStackTrace();
		}finally{
			/**
			 * 释放资源
			 */
			closeAll(conn,pstmt,rs);
		}
		return result;
	}
}


但是实际中要考虑到要存储Date类型的数据,所以可以改进executeUpdate方法,如下:

首先可以导入java.sql.Date包,注意区别java.util.Date包。

import java.sql.Date;

executeUpdate方法如下

/***
 * 执行SQL语句,可以进行增、删、改的操作,不能执行查询
 * @param preparedSql  预编译的 SQL 语句
 * @param param  预编译的 SQL 语句中的‘?’参数的字符串数组
 * @return  返回影响行数
 */
public static int executeUpdate(String preparedSql, String[] param, int site) {
	int result = 0;
	/**
	 * 得到数据库连接
	 */
	conn = getConnection();
	try {
		/**
		 * 得到PreparedStatement对象
		 */
		pstmt = conn.prepareStatement(preparedSql);
		if( param != null ) {
			for( int i=0;i<param.length;i++) {
				/**
				 * 为已预编译的sql设置参数
				 */
				if(site==0) {
					pstmt.setString(i+1, param[i]);
				}else {
					if(site==i + 1) {
						java.text. SimpleDateFormat  sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
						Long l1 = 0l;
						try {
							l1 =sdf.parse(param[i]).getTime();
						}catch(java.text.ParseException e) {
							e.printStackTrace();
						}
						pstmt.setDate(i+1, new Date(l1));
					}else {
						pstmt.setString(i+1, param[i]);
					}
				}
			}
		}
		result = pstmt.executeUpdate();
	}catch(SQLException e) {
		/**
		 * 处理SQLException异常
		 */
		e.printStackTrace();
	}finally {
		/**
		 * 释放资源
		 */
		closeAll(conn,pstmt,null);
	}
	return result;
}

其中形参site为0时表示数据库中没有Date类型的列。
更好的解决方案是结合使用sysdate和.getTimestamp()等处理日期,如下所示:

sql语句:

String sqlStr = "INSERT INTO MESSAGE VALUES(?,?,?,sysdate)";

使用BaseDAO方法:

int i = BaseDAO.executeUpdate(sqlStr, param, 0);

查询:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");//hh:12小时制;HH:24小时制
message.setTime(sdf.format(rs.getTimestamp("time")).toString());
补充:
在开发web应用中,针对不同的数据库日期类型,我们需要在我们的程序中对日期类型做各种不同的转换。
若对应数据库数据是oracle的Date类型,即只需要年月日的,可以选择使用java.sql.Date类型;
若对应的是MSsqlserver数据库的DateTime类型,即需要年月日时分秒的,应选择java.sql.Timestamp类型。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值