auth="Container"
type="javax.sql.DataSource"/>
factory
org.apache.commons.dbcp.BasicDataSourceFactory
maxActive
100
maxIdle
100
maxWait
10000
username
password
driverClassName
sun.jdbc.odbc.JdbcOdbcDriver
url
jdbc:odbc:newslist
=================
===============================================================
DBConnection .java
package news;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
//import com.AlertClass.Alert;
public class DBConnection {
private DataSource ds = null;
private Connection con = null;
private ResultSet rs = null;
public DBConnection() {
try {
Context ctx = new InitialContext();
//搜索数据库的资源 封装了DriverManager,它的底层还是通过DriverManager来获取连接对象的
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/news2");
} catch (NamingException e) {
e.printStackTrace();
}
}
public Connection getConnection() {
try {
con = ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public void closeConnection() {
try {
if (this.con != null) {
con.close();
}
} catch (SQLException sqlex) {
sqlex.printStackTrace();
}
}
public void closePrepStmt(PreparedStatement prepStmt) {
try {
if (prepStmt != null)
prepStmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void closeResultSet(ResultSet rs) {
try {
if (rs != null)
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public ResultSet executeQuery(String sql) {
try {
//connect = DriverManager.getConnection("jdbc:odbc:newslist"); //??b????????????l??
this.getConnection();
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);//执行指定的数据查询语句
}
catch(SQLException ex) {
ex.printStackTrace();
}
return rs;
}
/*public static Connection getConnection(){
Connection con=null;
ResourceBundle rb=ResourceBundle.getBundle("dataBaseProperties");
String driverClassName=rb.getString("classForName");
String url=rb.getString("url");
String username=rb.getString("username");
String password=rb.getString("password");
try{
Class.forName(driverClassName);
con=DriverManager.getConnection(url,username,password);
}catch(ClassNotFoundException cnfe){
new Alert(cnfe.getMessage());
System.out.println(cnfe.getMessage());
} catch (SQLException sqle) {
new Alert(sqle.getMessage());
System.out.println(sqle.getMessage());
}
return con;
}*/
}
阅读(1286) | 评论(0) | 转发(0) |