数据库操作可以中WEB开发中最常用到的,很多Java开发工具都提供了自动的Data bean WinZard.只要数据库建立好,相应的操作数据库的Bean就基本可以自动完成,本人使用Jcreator开发bean,手工录入觉得也不是很麻烦的事情,下面我常用的数据库操作bean,完全可以对付访问量不是很大的系统 :
Mysql类:
![](/Images/OutliningIndicators/None.gif)
import java.sql.*;
import java.io.*;
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
/** *//**
* 处理数据库的连接和访问
* @author sanware bqlr
* @version 1.01
*/
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
public class Mysql
{
![](/Images/OutliningIndicators/InBlock.gif)
private Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
![](/Images/OutliningIndicators/InBlock.gif)
//这是一个全局类,里面放置数据库的参数,如数据库主机 访问用户名 密码等
private static BeansConstants CONST = BeansConstants.getInstance();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 构造数据库的连接和访问类
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Mysql() throws Exception
{
Class.forName(CONST.dbdriver);
conn = DriverManager.getConnection(CONST.dburl);
stmt = conn.createStatement();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Mysql(String sql) throws Exception
{
Class.forName(CONST.dbdriver);
conn = DriverManager.getConnection(CONST.dburl);
this.prepareStatement(sql);
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 返回连接
* @return Connection 连接
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Connection getConnection()
{
return conn;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* PreparedStatement
* @return sql 预设SQL语句
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void prepareStatement(String sql) throws SQLException
{
prepstmt = conn.prepareStatement(sql);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 设置对应值
* @param index 参数索引
* @param value 对应值
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setString(int index,String value) throws SQLException
{
prepstmt.setString(index,value);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setInt(int index,int value) throws SQLException
{
prepstmt.setInt(index,value);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setBoolean(int index,boolean value) throws SQLException
{
prepstmt.setBoolean(index,value);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setDate(int index,Date value) throws SQLException
{
prepstmt.setDate(index,value);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setLong(int index,long value) throws SQLException
{
prepstmt.setLong(index,value);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setFloat(int index,float value) throws SQLException
{
prepstmt.setFloat(index,value);
}
//File file = new File("test/data.txt");
//int fileLength = file.length();
//InputStream fin = new java.io.FileInputStream(file);
//mysql.setBinaryStream(5,fin,fileLength);
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setBinaryStream(int index,InputStream in,int length) throws SQLException
{
prepstmt.setBinaryStream(index,in,length);
}
![](/Images/OutliningIndicators/InBlock.gif)
public void clearParameters()
throws SQLException
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
{
prepstmt.clearParameters();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 返回预设状态
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public PreparedStatement getPreparedStatement()
{
return prepstmt;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 返回状态
* @return Statement 状态
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public Statement getStatement()
{
return stmt;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 执行SQL语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public ResultSet executeQuery(String sql) throws SQLException
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (stmt != null)
{
return stmt.executeQuery(sql);
}
else return null;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public ResultSet executeQuery() throws SQLException
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (prepstmt != null)
{
return prepstmt.executeQuery();
}
else return null;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 执行SQL语句
* @param sql SQL语句
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void executeUpdate(String sql) throws SQLException
{
if (stmt != null)
stmt.executeUpdate(sql);
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void executeUpdate() throws SQLException
{
if (prepstmt != null)
prepstmt.executeUpdate();
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/** *//**
* 关闭连接
*/
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public void close() throws Exception
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (stmt != null)
{
stmt.close();
stmt = null;
}
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (prepstmt != null)
{
prepstmt.close();
prepstmt = null;
}
conn.close();
conn = null;
}
}
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
Mysql建立好后,以后涉及数据库的操作,只要对象化Mysql就可以:
private String page_navlink_insert="insert into page_navlink values (?,?,?,?)";
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
public void insertnavlink() throws Exception
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
ResultSet rs=null;
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
Mysql mysql = new Mysql(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();
![](/Images/OutliningIndicators/InBlock.gif)
mysql.close();
mysql = null;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
} catch (Exception ex)
{
throw new Exception("insertnavlink()"+ex.getMessage());
}
![](/Images/OutliningIndicators/InBlock.gif)
}
![](/Images/OutliningIndicators/None.gif)
在Jsp中,就可以直接使用一句语句使用insertnavlink()了:
<jsp:useBean id="NAV" scope="session" class="mysite.Navlink" />
<jsp:setProperty name="NAV" property="*" />
........
NAV.insertnavlink();
......
频繁访问数据库,需要使用连接池,在tomcat 4.0中配置JNDI,稍微修改一下上面程序就可使用连接池.Tomcat的连接池配置和J2EE类似,因此程序不用修改,也可直接运行在J2EE上.
也可以使用第三方连接池, 如很有名的Poolman.