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

来源:互联网 发布:sql 字段名为变量 编辑:程序博客网 时间:2024/06/06 11:00

添加好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类型。
原创粉丝点击