在上一篇已经说明了所有关于tomcat6.0+Mysql+jdbc建立连接池
参考网上写了个JavaBean用于数据库连接的所有操作
package database;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
//import database.DBConnectionManager;
public class DBConnect
{
Connection conn=null; //数据库连接变量
Statement stmt=null;//statement语句变量
private PreparedStatement prepstmt=null;//预定义statement 变量
// private DBConnectionManager dcm=null;//连接池管理变量
DataSource ds = null;
void init() throws Exception{
InitialContext ctx=new InitialContext();
ds=(DataSource)ctx.lookup("java:comp/env/jdbc/mysql"); //连接池名,具体看上一篇
conn = ds.getConnection();
}
//构造数据库的连接和访问类
public DBConnect() throws Exception{
init();
stmt=conn.createStatement();
}
public DBConnect(int resultSetType,int resultSetConcurrency) throws Exception{
init();
stmt=conn.createStatement(resultSetType,resultSetConcurrency);
}
//执行sql语句,返回结果集
public ResultSet executeQuery(String sql) throws SQLException{
if(stmt!=null){
return stmt.executeQuery(sql);
}
else return null;
}
public ResultSet executeQuery() throws SQLException{
if(prepstmt!=null){
return prepstmt.executeQuery();
}
else return null;
}
public int executeUpdate(String sql) throws SQLException{
if(stmt!=null){
return stmt.executeUpdate(sql);
}
else return -1;
}
public int executeUpdate() throws SQLException{
if(prepstmt!=null){
return prepstmt.executeUpdate();
}
else return -1;
}
//够找数据库的连接和访问类
//预编译SQL语句
public DBConnect(String sql) throws Exception{
init();
this.prepareStatement(sql);
}
public DBConnect(String sql,int resultSetType,int resultSetConcurrency) throws Exception{
init();
this.prepareStatement(sql,resultSetType,resultSetConcurrency);
}
//返回连接
public Connection getConnection(){
return conn;
}
//关闭连接
public void close() throws Exception{
if(stmt!=null){
stmt.close();
stmt=null;
}
if(prepstmt!=null){
prepstmt.close();
prepstmt=null;
}
if(conn!=null){
conn.close();
}
}
//preparedStatement 创建一个对象
public void prepareStatement(String sql) throws SQLException{
prepstmt=conn.prepareStatement(sql);
}
public void prepareStatement(String sql,int resultSetType,int resultSetConcurrency) throws SQLException{
prepstmt=conn.prepareStatement(sql,resultSetType,resultSetConcurrency);
}
//设置对应的值
//index参数索引value对应值
public void setString(int index,String value) throws SQLException{
prepstmt.setString(index,value);
}
public void setInt(int index,int value) throws SQLException{
prepstmt.setInt(index,value);
}
public void setBoolean(int index,boolean value) throws SQLException{
prepstmt.setBoolean(index,value);
}
public void setDate(int index,Date value) throws SQLException{
prepstmt.setDate(index,value);
}
public void setLong(int index,long value) throws SQLException{
prepstmt.setLong(index,value);
}
public void setFloat(int index,float value) throws SQLException{
prepstmt.setFloat(index,value);
}
public void setBytes(int index,byte[] value) throws SQLException{
prepstmt.setBytes(index,value);
}
//清除参数及返回状态
public void clearParameters() throws SQLException{
prepstmt.clearParameters();
prepstmt=null;
}
public PreparedStatement getPreparedStatement(){
return prepstmt;
}
public Statement getStatement(){
return stmt;
}
}
具体使用有两种:
1.选择性,操作成功返回1,失败返回0
int flag=0;
String result=null;//如何比对用户名密码而不出错,的SQL语句~~~~就不用在异常里捕捉了,我晕
String strSql = "select userPwd from users where userName='"+userName+"'";
try{
DBConnect dbc=new DBConnect();
ResultSet rs=dbc.executeQuery(strSql);
while(rs.next()){
result=rs.getString(1);
}
if(result.equals(password))
flag=1;
rs.close();
dbc.close();
}catch(Exception e)
{
return flag;
}
2.更新性质的:更新成功1
int flag=0;
//存在自动id增长的SQL语句还没找到,我晕
String strSql = "insert into users(userName,userPwd) values(?,?)";
try{
DBConnect dbc=new DBConnect(strSql);
dbc.setString(1,userName);
dbc.setString(2,password);
flag = dbc.executeUpdate();
dbc.close();
}catch(Exception e)
{
flag=0;
return flag;
}
return flag;
基本就这两种了,用起来蛮方便的
自己读读代码咯
不多说,有问题发信http://blog.sina.com.cn/programbus