本例采用Access做为实例,所有还有很多要改进的地方。
请读者自已改进
package prodUCt;
import Java.sql.*;
public class ConnectDB{
String strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
String strConnstr="jdbc:odbc:product";
Connection conn=null;
ResultSet rs=null;
Statement stmt=null;
// Statement stmt=null;
//构造函数
public ConnectDB(){
try{
Class.forName(strDBDriver); conn=DriverManager.getConnection(strConnstr);
stmt=conn.createStatement();
}
catch(Exception e){
System.out.println(e);
}
}
public ResultSet execute(String sql){
rs=null;
try{
rs=stmt.executeQuery(sql);
}
catch(SQLException e){
System.out.println(e.getMessage());
}
return rs;
}
public Connection getConnection(){
return conn;
}
public boolean close(){
try{
if(this.rs!=null){
this.rs.close();
}
if(stmt!=null){
this.stmt.close();
}
if(conn!=null){
this.conn.close();
}
return true;
}catch(Exception err){
return false;
}
}
}
//=======================================
package product;
import java.sql.*;
public class catalog
{
ConnectDB conn=new ConnectDB();
String str=null;
String sql=null;
ResultSet rs=null;
String AncestorID,LinkStr;
public String getCatalog(){
str="<select name="FatherID" class="p9">
"+
"<option value="-1" selected>请选择父类</option>
";
String sql=null;
sql="select * from catalog order by AncestorID,Linkstr";
ResultSet rs=conn.execute(sql);
try{
while (rs.next()) {
str=str+"<option value="+rs.getString("Productid")+">";
int nbspCount=rs.getString("LinkStr").length()-1;