This is the class I use after I have changed on the Code found on the Internet.
Divided into three parts: parameter settings, database connections, and applied examples later.
If you can't apply directly, please contact me: Fajaven@vip.sina.com
/ **************** * Connect to the parameter setting of the database *************************** ******* /
Package Net.asales.mysql;
public class BeansConstants {public static String dbdriver; public static String dburl; private String database = "asales"; private String usr = "root"; private String passwd = ""; private BeansConstants (String dbms) {// set currently only MySQL driver IF (dbms == "mysql") {this.dbdriver = "org.gjt.mm.mysql.driver"; this.dburl = "JDBC: mysql: // localhost /" this.database " ? useUnicode = true & characterEncoding = gb2312 " " & user = " this.usr " & password = " this.passwd;} else {this.dbdriver =" none ";}} public static BeansConstants getInstance (String dbms) {BeansConstants bc = new BeansConstants (dbms); return bc;} public String getDatabase () {return this.database;} public String getUsr () {return this.usr;} public String getPasswd () {return this.passwd;} public String getDbdriver () {Return this.dbdriver; public string getdburl () {return this.dburl;} public voi d setDatabase (String database) {this.database = database;} public void setUsr (String usr) {this.usr = usr;} public void setPasswd (String passwd) {this.passwd = passwd;} public static void main (String [] args) {beansconStants constant = beansconstants.getinstance ("mysql"); system.out.println ("driver =" const.dbdriver "/ nurl =" const.dburl);}} / **** *********************** * @Author javen * @version 0.98 ********* ****************** /
Package net.asales.mysql; import java.sql. *; import java.io. *;
Public class mysql {private connection conn = null; private preparedStatement prepstmt = null;
// This is a full class, placed the parameters of the database, such as database host access username password, etc. Private beansconstants constance ("mysql");
/ ** * Construct the connection and access class * / public mysql () throws exception {class.forname (const.dbdriver); conn = drivermanager.getConnection (const.dburl); stmt = conn.createstatement ();} PUBLIC Mysql (String SQL) throws Exception {class.Forname (const.dbdriver); conn = drivermanager.getConnection (const.dburl); this.preparestatement (SQL);}
/ ** * prepaaredStatement * @RETURN SQL presets SQL statement * / public void prepareStatement (String SQL) THROWS SQLEXCEPTION {prepstMt = conn.preparestatement (SQL);
public Connection getConn () {return this.conn;} public Statement getStmt () {return this.stmt;} public PreparedStatement getPrepstmt () {return this.prepstmt;} / ** * returns the set of fields execute SQL statements * @param sql SQL statement * @Return ResultSet field set * / public resultSet ExecuteQuery (String SQL) THROWS SQLEXCEPTION {f (stmt! = Null) {Return Stmt.executeQuery (SQL);} else return null;}
public ResultSet executeQuery () throws SQLException {if (prepstmt = null!) {return prepstmt.executeQuery ();} else return null;} / ** * execute SQL statements * @param sql SQL statement * / public void executeUpdate (String sql ) throws sqlexception {if (stmt! = null) stmt.executeUpdate (SQL);
public void executeUpdate () throws SQLException {if (prepstmt = null!) prepstmt.executeUpdate ();} / ** * close connection * / public void close () throws Exception {if (! stmt = null) {stmt.close ( ); Stmt = null;} if (prepStmt! = Null) {prepstmt.close (); prepstmt = null;} conn.close (); conn = null;}}
// Connection case
<% @ page import = "java.sql. *"%>
ResultSet RS = Sql.executeQuery (SQL);
SQL = "INSERT INTO TABLE (ID, NAME) VALUES ('', '')"
Sql.executeUpdate (SQL);