自从弄了框架之后,以前的一些东西有点点生疏了。先总结下。
java连接数据库有4中方式
1.JDBC-ODBC桥+ODBC驱动
2.本地API
3.JDBC网络纯JAVA驱动
4.本地协议纯java驱动程序
首先从最早接触到的纯JBDC的那种DAO层写法开始。
一开始是这样写DAO层的数据库连接的。
1
首先从最早接触到的纯JBDC的那种DAO层写法开始。
一开始是这样写DAO层的数据库连接的。
package com.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 static Connection con = null;
private final static String url = "jdbc:mysql://localhost:3306/test";// 数据库连接名
private final static String usrName = "root";// 用户名
final static private String pwd = "lohas";// 密码
final static private String driver = "com.mysql.jdbc.Driver";
public static Connection getConnection() {
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con = DriverManager.getConnection(url, usrName, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
public void closeAll(ResultSet set, PreparedStatement ps, Connection con) {
try {
if (set != null) {
set.close();
}
if (ps != null) {
ps.close();
}
if (con != null) {
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int executeSql(String sql, Object[] param) {//执行SQL语句
Connection con = getConnection();
PreparedStatement ps = null;
int count = 0;
try {
ps = con.prepareStatement(sql);
if (param != null) {
for (int i = 0; i < param.length; i++) {
ps.setObject(i + 1, param[i]);
}
}
count = ps.executeUpdate();// 执行sql语句
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
closeAll(null, ps, con);
return count;
}
public ResultSet querySql(String sql){//查询
con = getConnection();
ResultSet set = null;
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
set = ps.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//closeAll(set, ps, con);
return set;
}
public ResultSet querySql(String sql,Object[] str){//查询
con = getConnection();
ResultSet set = null;
PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);
if (str != null) {
for (int i = 0; i < str.length; i++) {
ps.setObject(i + 1, str[i]);
}
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
set = ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//closeAll(set, ps, con);
return set;
}
public ResultSet getOneIntence(String sql,Object obj){
Connection con = getConnection();
PreparedStatement ps = null;
ResultSet set = null;
try {
ps = con.prepareStatement(sql);
ps.setObject(1, obj);
set = ps.executeQuery();// 执行sql语句
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//closeAll(null, ps, con);
return set;
}
}
这种写法没有写连接管理类,并且都是硬编码的方式,传统的操作ResultSet PreparedStatement Connection 这几个接口,方法还是不够通过。
那个时候的JNDI是这样写的
Web.xml
<resource-ref>
<res-ref-name> jdbc/JNDI6 </res-ref-name>
<res-type> javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>
package com.dao;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class BaseDao {
/**
* 采用JNDI数据库连接池
*/
private static Connection con;
public static Connection getConnection() {
/**
* 通过JNDI获得连接对象
*/
Context ic=null;
try {
ic = new InitialContext();
DataSource source = (DataSource) ic.lookup("java:comp/env/jdbc/JNDI6");
con = source.getConnection();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
ic.close();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}return con;
}
/**
* 采用JNDI方式connection 不用关 容器会自动回收
* @param set
* @param sta
*/
public static void closeAll(ResultSet set ,Statement sta){
if(set!=null){
try {
set.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(sta!=null){
try {
sta.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
再后来有了配置文件,读取配置文件的代码如下:
url = jdbc:mysql://localhost:3306/shopdb
usrName = admin
pwd = admin
driver = com.mysql.jdbc.Driver
package com.Properties;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class Env extends Properties {
/**
*
*/
private static final long serialVersionUID = 1L;
private static Env instance;
public static Env getInstance() {
if (instance != null) {
return instance;
} else {
mackInstance();
return instance;
}
}
private static synchronized void mackInstance() {
// TODO Auto-generated method stub
if (instance == null) {
instance = new Env();
}
}
private Env() {
/**
* 从输入流中读取属性列表(键和元素对)。输入流按 load(Reader) 中所指定的、简单的面向行的格式,并假定使用 ISO 8859-1
* 字符编码;即每个字节都是 Latin1 字符。对于非 Latin1 的字符和某些特殊字符,可以使用 Unicode
* 转义以键和元素的形式来表示它们。
*/
InputStream in = this.getClass().getResourceAsStream("/db.properties");
try {
load(in);// 父类方法
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.dao;
import com.Properties.Env;
public class TestProperties {
public static void main(String[] args) {
String name = Env.getInstance().getProperty("url");//读取配置文件
System.out.println(name);
}
}
再后来
package com.oh.util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.jsp.jstl.sql.Result;
import javax.servlet.jsp.jstl.sql.ResultSupport;
public class SqlCommentBean {
/**
* 操纵数据库工具类
*/
private Connection connection = ConnectionManager.getConnection();// 连接类
private String sql;// sql语句
private Object[] sqlParams;// sql语句参数数组
public SqlCommentBean(String sql, Object[] sqlParams) {
/**
* 带有sql参数构造器
*/
super();
this.sql = sql;
this.sqlParams = sqlParams;
}
public SqlCommentBean(String sql) {
/**
* 不带有sql参数构造器
*/
super();
this.sql = sql;
}
/**
* 执行查询
*/
public Result executeQuery() {
Result result = null;
ResultSet set = null;
PreparedStatement ps = null;
try {
ps = connection.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (sqlParams != null && sqlParams.length > 0) {
try {
setSqlParams(ps, sqlParams);
set = ps.executeQuery();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
try {
set = ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
result = ResultSupport.toResult(set);// 将resultset转化为result
ConnectionManager.closeAll(ps, set);
return result;
}
/**
* 执行增删改
*
* @return
*/
public int executeUpdate() {
PreparedStatement ps = null;
int rows = 0;
try {
ps = connection.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (sqlParams != null && sqlParams.length > 0) {
try {
setSqlParams(ps, sqlParams);
rows = ps.executeUpdate();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
try {
rows = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rows;
}
/**
* 设置sql语句
*
* @param sql
*/
public void setSql(String sql) {
this.sql = sql;
}
/**
* 设置sql语句参数 内部方法
*
* @param sqlParams
*/
private void setSqlParams(PreparedStatement ps, Object[] sqlParams) {
if (sqlParams != null) {
for (int i = 0; i < sqlParams.length; i++) {
try {
ps.setObject(i + 1, sqlParams[i]);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
private void setDarkSqlParams(PreparedStatement ps, Object[] sqlParams){
/**
* 设置模糊参数
*/
if (sqlParams != null) {
for (int i = 0; i < sqlParams.length; i++) {
try {
if(sqlParams[i] instanceof Integer){//如果是具体的数值类型
ps.setObject(i + 1, sqlParams[i]);
}else{
if(!"".equals(sqlParams[i])){
ps.setObject(i + 1, "%"+sqlParams[i]+"%");
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public Result darkQuery(){
/**
* 执行模糊查询
*/
Result result = null;
ResultSet set = null;
PreparedStatement ps = null;
try {
ps = connection.prepareStatement(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (sqlParams != null && sqlParams.length > 0) {
try {
setDarkSqlParams(ps, sqlParams);
set = ps.executeQuery();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else {
try {
set = ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
result = ResultSupport.toResult(set);// 将resultset转化为result
ConnectionManager.closeAll(ps, set);
return result;
}
}