本例需要的软件和运行环境:
1、Windows2000 Server操作系统
2、jdk1.4
3、JCreator2.5(java源码编辑调试器,吐血推荐!)
4、Macromedia JRun MX
5、Macromedia Dreamweaver MX(非必需)
6、MySQL数据库(最好安装MySQL Control Center)
一、数据库设计
用MySQL Control Center打开MySQL数据库,新建数据库shopping,在其下新建表tbl_user,其中各字段设置如下:
二、编写连接数据库bean:DBConn.java
//DBConn.java
//include required classes
import java.sql.*;
//==========================================
// Define Class DBConn
//==========================================
public class DBConn
{
public String sql_driver = "org.gjt.mm.mysql.Driver";
public String sql_url = "jdbc:mysql://localhost:3306";
public String sql_DBName = "shopping";
public String user = "sa";
public String pwd = ";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
public boolean setDriver(String drv)
{
this.sql_driver = drv;
return true;
}
public String getDriver()
{
return this.sql_driver;
}
public boolean setUrl(String url)
{
this.sql_url = url;
return true;
}
public boolean setDBName(String dbname)
{
this.sql_DBName = dbname;
return true;
}
public String getDBName()
{
return this.sql_DBName;
}
public boolean setUser(String user)
{
this.user = user;
return true;
}
public String getUser()
{
return this.user;
}
public boolean setPwd(String pwd)
{
this.pwd = pwd;
return true;
}
public String getPwd()
{
return this.pwd;
}
public DBConn()
{
try{
Class.forName(sql_driver);//加载数据库驱动程序
this.conn = DriverManager.getConnection(sql_url + "https://www.jb51.net/" + sql_DBName + "?user=" + user + "&password=" + pwd + "&useUnicode=true&characterEncoding=gb2312");
this.stmt = this.conn.createStatement();
}catch(Exception e){
System.out.println(e.toString());
}
}
//执行查询操作
public ResultSet executeQuery(String strSql)
{
try{
this.rs = stmt.executeQuery(strSql);
return this.rs;
}catch(SQLException e){
System.out.println(e.toString());
return null;
}catch(NullPointerException e){
System.out.println(e.toString());
return null;
}
}
//执行数据的插入、删除、修改操作
public boolean execute(String strSql)
{
try{
if(this.stmt.executeUpdate(strSql) == 0)
return false;
else
return true;
}catch(SQLException e){
System.out.println(e.toString());
return false;
}catch(NullPointerException e){
System.out.println(e.toString());
return false;
}
}
//结果集指针跳转到某一行
public boolean rs_absolute(int row)
{
try{
this.rs.absolute(row);
return true;
}catch(SQLException e){
System.out.println(e.toString());
return false;
}
}
public void rs_afterLast()
{
try{
this.rs.afterLast();
}catch(SQLException e){
System.out.println(e.toString());
}
}
public void rs_beforeFirst()
{
try{
this.rs.beforeFirst();
}catch(SQLException e){
System.out.print(e.toString());
}
}
public void rs_close()
{
try{
this.rs.close();
}catch(SQLException e){
System.out.print(e.toString());
}
}
public void rs_deleteRow()
{
try{
this.rs.deleteRow();
}catch(SQLException e){
System.out.print(e.toString());
}
}
public boolean rs_first()
{
try{
this.rs.first();
return true;
}catch(SQLException e){
System.out.print(e.toString());
return false;
}
}
public String rs_getString(String column)
{
try{
return this.rs.getString(column);
}catch(SQLException e){
System.out.println(e.toString());
return null;
}
}