数据库操作可以中WEB开发中最常用到的,很多Java开发工具都提供了自动的Data bean WinZard.只要数据库建立好,相应的操作数据库的Bean就基本可以自动完成,本人使用Jcreator开发bean,手工录入觉得也不是很麻烦的事情,下面我常用的数据库操作bean,完全可以对付访问量不是很大的系统 :
privateConnectionconn=null;
privateStatementstmt=null;
privatePreparedStatementprepstmt=null;
//这是一个全局类,里面放置数据库的参数,如数据库主机访问用户名密码等
privatestaticBeansConstantsCONST=BeansConstants.getInstance();
/***//**
*构造数据库的连接和访问类
*/
publicMysql()throwsException{
Class.forName(CONST.dbdriver);
conn=DriverManager.getConnection(CONST.dburl);
stmt=conn.createStatement();
}
publicMysql(Stringsql)throwsException{
Class.forName(CONST.dbdriver);
conn=DriverManager.getConnection(CONST.dburl);
this.prepareStatement(sql);
}
/***//**
*返回连接
*@returnConnection连接
*/
publicConnectiongetConnection(){
returnconn;
}
/***//**
*PreparedStatement
*@returnsql预设SQL语句
*/
publicvoidprepareStatement(Stringsql)throwsSQLException{
prepstmt=conn.prepareStatement(sql);
}
/***//**
*设置对应值
*@paramindex参数索引
*@paramvalue对应值
*/
publicvoidsetString(intindex,Stringvalue)throwsSQLException{
prepstmt.setString(index,value);
}
publicvoidsetInt(intindex,intvalue)throwsSQLException{
prepstmt.setInt(index,value);
}
publicvoidsetBoolean(intindex,booleanvalue)throwsSQLException{
prepstmt.setBoolean(index,value);
}
publicvoidsetDate(intindex,Datevalue)throwsSQLException{
prepstmt.setDate(index,value);
}
publicvoidsetLong(intindex,longvalue)throwsSQLException{
prepstmt.setLong(index,value);
}
publicvoidsetFloat(intindex,floatvalue)throwsSQLException{
prepstmt.setFloat(index,value);
}
//Filefile=newFile("test/data.txt");
//intfileLength=file.length();
//InputStreamfin=newjava.io.FileInputStream(file);
//mysql.setBinaryStream(5,fin,fileLength);
publicvoidsetBinaryStream(intindex,InputStreamin,intlength)throwsSQLException{
prepstmt.setBinaryStream(index,in,length);
}
publicvoidclearParameters()
throwsSQLException
{
prepstmt.clearParameters();
}
/***//**
*返回预设状态
*/
publicPreparedStatementgetPreparedStatement(){
returnprepstmt;
}
/***//**
*返回状态
*@returnStatement状态
*/
publicStatementgetStatement(){
returnstmt;
}
/***//**
*执行SQL语句返回字段集
*@paramsqlSQL语句
*@returnResultSet字段集
*/
publicResultSetexecuteQuery(Stringsql)throwsSQLException{
if(stmt!=null){
returnstmt.executeQuery(sql);
}
elsereturnnull;
}
publicResultSetexecuteQuery()throwsSQLException{
if(prepstmt!=null){
returnprepstmt.executeQuery();
}
elsereturnnull;
}
/***//**
*执行SQL语句
*@paramsqlSQL语句
*/
publicvoidexecuteUpdate(Stringsql)throwsSQLException{
if(stmt!=null)
stmt.executeUpdate(sql);
}
publicvoidexecuteUpdate()throwsSQLException{
if(prepstmt!=null)
prepstmt.executeUpdate();
}
/***//**
*关闭连接
*/
publicvoidclose()throwsException{
if(stmt!=null){
stmt.close();
stmt=null;
}
if(prepstmt!=null){
prepstmt.close();
prepstmt=null;
}
conn.close();
conn=null;
}
}
Mysql建立好后,以后涉及数据库的操作,只要对象化Mysql就可以:
{
ResultSetrs=null;
try{
Mysqlmysql=newMysql(page_navlink_insert);
mysql.setInt(1,this.siteid);
mysql.setInt(2,this.pageid);
mysql.setString(3,this.navlinkname);
mysql.setString(4,this.pagefile);
mysql.executeUpdate();
mysql.close();
mysql=null;
}catch(Exceptionex){
thrownewException("insertnavlink()"+ex.getMessage());
}
}
在Jsp中,就可以直接使用一句语句使用insertnavlink()了:
<jsp:useBean scope="session" />
<jsp:setProperty name="NAV" property="*" />
........
NAV.insertnavlink();
......
频繁访问数据库,需要使用连接池,在tomcat 4.0中配置JNDI,稍微修改一下上面程序就可使用连接池.Tomcat的连接池配置和J2EE类似,因此程序不用修改,也可直接运行在J2EE上.
也可以使用第三方连接池, 如很有名的Poolman.