public interface DbConst{
public final static String JDBC_DRIVER="com.mysql.jdbc.Driver";
public final static String JDBC_URL="jdbc:mysql://localhost:3306/web_exam?"
+"useUnicode=true&characterEncoding=GB2312";
public final static String JDBC_USER="root";
public final static String JDBC_PASS="chenfu";
}
9,DbConnection.java
package com.cf.model;
import java.sql.*;
public class DbConnection implements DbConst{
private String jdbcDriver=JDBC_DRIVER;
private String databaseURL=JDBC_URL;
private String dbUsername=JDBC_USER;
private String dbPassword=JDBC_PASS;
private Connection con=null;
public String getJdbcDriver(){
return this.jdbcDriver;
}
public void setJdbcDriver(String d){
this.jdbcDriver=d;
}
public String getDatabaseURL(){
return this.databaseURL;
}
public void setDatabaseURL(String url){
this.databaseURL=url;
}
public String getDbUsername(){
return this.dbUsername;
}
public void setDbUsername(String u){
this.dbUsername=u;
}
public String getDbPassword(){
return this.dbPassword;
}
public void setDbPassword(String p){
this.dbPassword=p;
}
public boolean isConnected(){
return (con!=null);
}
public void disconnected(){
if(con!=null){
try
{
con.close();
}
catch (SQLException ignore)
{
}
finally {
con=null;
}
}
}
public void setCon(Connection c){
this.con=c;
}
public Connection getCon() throws SQLException{
if(isConnected()) throw new SQLException("Already connected");
if(jdbcDriver==null) throw new SQLException("No jdbcDriver property");
if(databaseURL==null) throw new SQLException("No jdbcURL property");
try
{
Class.forName(jdbcDriver);
//log.info(jdbcDriver);
}
catch (ClassNotFoundException e)
{
throw new SQLException(jdbcDriver+"class could not loaded");
}
con=DriverManager.getConnection(databaseURL,dbUsername,dbPassword);
return con;
}
}
10,DbManipulate.java
package com.cf.model;
import java.sql.*;
public class DbManipulate{
private Connection con=null;
private PreparedStatement pstmt=null;