环境
xp + jdk1.5 + tomcat5.5.23 + eclipse3.2.2 + myeclipse5.1.1GA + sqlserver2000
使用连接池方式连接数据库,至于这方面的问题可参考 http://zbsdg.com/?p=39
写一个DBConnection.java封装对数据库的一些操作,实现SqlTestDS的getConnection()方法连接数据库
DBConnection.java
package
database
;
import java . sql .*;
public class DBConnection {
private Connection conn = null ;
private Statement stmt = null ;
ResultSet rs = null ;
public ResultSet executeQuery ( String sql ) {
try {
conn = SqlTestDS . getConnection () ;
stmt = conn . createStatement
( ResultSet . TYPE_SCROLL_INSENSITIVE , ResultSet . CONCUR_READ_ONLY ) ;
rs = stmt . executeQuery ( sql ) ;
} catch ( Exception e ) {
e . printStackTrace () ;
}
return rs ;
}
public void executeUpdate ( String sql ) {
try {
conn = SqlTestDS . getConnection () ;
stmt = conn . createStatement
( ResultSet . TYPE_SCROLL_INSENSITIVE , ResultSet . CONCUR_READ_ONLY ) ;
stmt . executeUpdate ( sql ) ;
} catch ( Exception e ) {
e . printStackTrace () ;
}
}
public void closeStmt () {
try {
stmt . close () ;
} catch ( Exception e ) {
e . printStackTrace () ;
}
}
public void closeConn () {
try {
conn . close () ;
} catch ( Exception e ) {
e . printStackTrace () ;
}
}
}
import java . sql .*;
public class DBConnection {
private Connection conn = null ;
private Statement stmt = null ;
ResultSet rs = null ;
public ResultSet executeQuery ( String sql ) {
try {
conn = SqlTestDS . getConnection () ;
stmt = conn . createStatement
( ResultSet . TYPE_SCROLL_INSENSITIVE , ResultSet . CONCUR_READ_ONLY ) ;
rs = stmt . executeQuery ( sql ) ;
} catch ( Exception e ) {
e . printStackTrace () ;
}
return rs ;
}
public void executeUpdate ( String sql ) {
try {
conn = SqlTestDS . getConnection () ;
stmt = conn . createStatement
( ResultSet . TYPE_SCROLL_INSENSITIVE , ResultSet . CONCUR_READ_ONLY ) ;
stmt . executeUpdate ( sql ) ;
} catch ( Exception e ) {
e . printStackTrace () ;
}
}
public void closeStmt () {
try {
stmt . close () ;
} catch ( Exception e ) {
e . printStackTrace () ;
}
}
public void closeConn () {
try {
conn . close () ;
} catch ( Exception e ) {
e . printStackTrace () ;
}
}
}