The following is a simple database connection tool class written in SQL Server, encapsulated in a WEB project (web-inf/classes/) directory, and introduced in Java or JSP pages.
ODBC name: MySQL;
User name: Dxaw;
Password: 123;
Method Executeupdate (): Update the database;
Method ExecuteQuery (): Returns a recordset through an SQL statement;
The code is as follows:
Class Conn.db.conndb.java
package conn.db;
import java.sql.*;
public class conndb {
Connection conn;
ResultSet rs;
private int count;
public conndb() {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception ex) {}
}
public ResultSet executeQuery(String sql){
try {
conn=DriverManager.getConnection("Jdbc:Odbc:mysql","dxaw","123");
Statement stat=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs=stat.executeQuery(sql);
} catch (Exception ex1) {
}
return rs;
}
public void executeUpdate(String sql){
try {
conn = DriverManager.getConnection("Jdbc:Odbc:mysql", "dxaw","123");
Statement stat=conn.createStatement();
stat.executeQuery(sql);
} catch (Exception ex2) {
}
}
}