package com.gg.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;public class BaseDao {
private final String DRIVER = "org.h2.Driver";
private final String URL = "jdbc:h2:~/test";
private final String USER = "sa";
private final String PASS = "";
protected Connection conn = null;// 数据库连接对象
@SuppressWarnings("unused")
protected PreparedStatement ps = null;// 数据库执行对象
@SuppressWarnings("unused")
protected ResultSet rs = null;// 数据库临时结果集 /**
* 获取数据库连接Connection对象
*
* @return
*/
@SuppressWarnings("unused")
protected Connection getConn() {
try {
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USER, PASS);
if (conn != null)
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
} /**
* 关闭动态连接
*
* @param conn
* @param ps
*/
@SuppressWarnings("unused")
protected void closeLink(Connection conn, PreparedStatement ps) {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
} /**
* 关闭动态连接(重载方法)
*
* @param conn
* @param ps
* @param rs
*/
@SuppressWarnings("unused")
protected void closeLink(Connection conn, PreparedStatement ps, ResultSet rs) {
if (conn != null)
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (ps != null)
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
if (rs != null)
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
} // protected boolean insertDelete(String sql) {
// try {
// ps = this.getConn().prepareStatement(sql);
// if (ps != null)
// return ps.execute();
// } catch (SQLException e) {
// e.printStackTrace();
// } finally {
// this.closeLink(conn, ps);
// }
// return false;
// }
//
// static public void main(String[] args) {
// BaseDao b = new BaseDao();
// System.out.println(b.insertDelete("create table AAA(Id int primary key,name varchar,sex varchar age int)"));
// }}