在Javaweb开发中连接数据库的方法有JDBC,ODBC数据源,连接池...等。
本人比较喜欢连接池,这里推荐一个实例给大家看看.
首先在tomcat里面找到context.xml
这里以mysql为例子
<Resource name="jdbc/mysqlS2"
auth="Containar"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="wei"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/struts2"/>
在上面代码中mysqlS2名字可以任意,但是不能重复.其它连接大家都应该明白这里就不多做解释.
然后在封装一个DButils代码如下:
package com.wei.dang.util;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
public class DBUtils {
private static DataSource ds = null;
private static Context context;
private static QueryRunner qr;
public static QueryRunner geQueryRunner(){
try {
context = new InitialContext();
ds = (DataSource)context.lookup("java:/comp/env/jdbc/mysqlds");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
qr = new QueryRunner(ds);
return qr;
}
public static int update(String sql, String[] param) {
int result = 0;
qr = DBUtils.geQueryRunner();
try {
result = qr.update(sql, param);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public static Object query(String sql, String[] param, ResultSetHandler rsh) {
qr = DBUtils.geQueryRunner();
Object result = null;
try {
result = qr.query(sql, param, rsh);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
上面忘记说了还需要加个DButils的一个jar包.这个包我已经上传了,供大家下载.
补充下面的是oracle请大家动手将上面的改成下面的模式.
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
public class OracleUtil {
private String dataSourceName;
private DataSource ds;
public OracleUtil(String dataSourceName) {
this.dataSourceName = dataSourceName;
}
public OracleUtil() {
}
public void setDataSourceName(String dataSourceName) {
this.dataSourceName = dataSourceName;
}
public void init() {
Context initContext;
try {
initContext = new InitialContext();
ds = (DataSource) initContext.lookup(dataSourceName);
} catch (NamingException e) {
e.printStackTrace();
}
}
public int update(String sql, String[] param) {
int result = 0;
QueryRunner qr = new QueryRunner(ds);
try {
result = qr.update(sql, param);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public Object query(String sql, String[] param, ResultSetHandler rsh) {
QueryRunner qr = new QueryRunner(ds);
Object result = null;
try {
result = qr.query(sql, param, rsh);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
本人比较喜欢连接池,这里推荐一个实例给大家看看.
首先在tomcat里面找到context.xml
这里以mysql为例子
<Resource name="jdbc/mysqlS2"
auth="Containar"
type="javax.sql.DataSource"
maxActive="100"
maxIdle="30"
maxWait="10000"
username="root"
password="wei"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/struts2"/>
在上面代码中mysqlS2名字可以任意,但是不能重复.其它连接大家都应该明白这里就不多做解释.
然后在封装一个DButils代码如下:
package com.wei.dang.util;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
public class DBUtils {
private static DataSource ds = null;
private static Context context;
private static QueryRunner qr;
public static QueryRunner geQueryRunner(){
try {
context = new InitialContext();
ds = (DataSource)context.lookup("java:/comp/env/jdbc/mysqlds");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
qr = new QueryRunner(ds);
return qr;
}
public static int update(String sql, String[] param) {
int result = 0;
qr = DBUtils.geQueryRunner();
try {
result = qr.update(sql, param);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public static Object query(String sql, String[] param, ResultSetHandler rsh) {
qr = DBUtils.geQueryRunner();
Object result = null;
try {
result = qr.query(sql, param, rsh);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
上面忘记说了还需要加个DButils的一个jar包.这个包我已经上传了,供大家下载.
补充下面的是oracle请大家动手将上面的改成下面的模式.
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
public class OracleUtil {
private String dataSourceName;
private DataSource ds;
public OracleUtil(String dataSourceName) {
this.dataSourceName = dataSourceName;
}
public OracleUtil() {
}
public void setDataSourceName(String dataSourceName) {
this.dataSourceName = dataSourceName;
}
public void init() {
Context initContext;
try {
initContext = new InitialContext();
ds = (DataSource) initContext.lookup(dataSourceName);
} catch (NamingException e) {
e.printStackTrace();
}
}
public int update(String sql, String[] param) {
int result = 0;
QueryRunner qr = new QueryRunner(ds);
try {
result = qr.update(sql, param);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public Object query(String sql, String[] param, ResultSetHandler rsh) {
QueryRunner qr = new QueryRunner(ds);
Object result = null;
try {
result = qr.query(sql, param, rsh);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}