Package com.zss.www;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import com.mysql.jdbc.Connection;
Import com.mysql.jdbc.PreparedStatement;
public class Dbconn {
Private Connection Conn=null;
Private PreparedStatement Stmt=null;
Private ResultSet Rs=null;
Public Dbconn () {
try {
String driver= "Com.mysql.jdbc.Driver";
String url= "Jdbc:mysql://localhost:3306/school";
String user= "root";
String password= "711109";
Class.forName (driver);
conn = (Connection) drivermanager.getconnection (Url,user,password);
SYSTEM.OUT.PRINTLN ("-------Connection successfully------");
} catch (ClassNotFoundException classnotfoundexception) {
Classnotfoundexception.printstacktrace ();
System.err.println ("DB:" + classnotfoundexception.getmessage ());
} catch (SQLException SQLException) {
System.err.println ("Db.getconn ():" + sqlexception.getmessage ());
}
}
public void Dbconn (String driver,string url,string user,string password) {
try {
Driver= "Com.mysql.jdbc.Driver";
Url= "Jdbc:mysql://localhost:3306/school";
User= "Root";
Password= "711109";
Class.forName (driver);
conn = (Connection) drivermanager.getconnection (Url,user,password);
SYSTEM.OUT.PRINTLN ("-------Connection successfully------");
} catch (ClassNotFoundException classnotfoundexception) {
Classnotfoundexception.printstacktrace ();
System.err.println ("DB:" + classnotfoundexception.getmessage ());
} catch (SQLException SQLException) {
System.err.println ("Db.getconn ():" + sqlexception.getmessage ());
}
}
public void Doinsert (String sql) {
try {
stmt = (preparedstatement) conn.preparestatement (SQL);
int i = stmt.executeupdate (sql);
} catch (SQLException SQLException) {
System.err.println ("Db.executeinset:" + sqlexception.getmessage ());
}finally{
}
}
public void DoDelete (String sql) {
try {
stmt = (preparedstatement) conn.preparestatement (SQL);
int i = stmt.executeupdate (sql);
} catch (SQLException SQLException) {
System.err.println ("Db.executedelete:" + sqlexception.getmessage ());
}
}
public void DoUpdate (String sql) {
try {
stmt = (preparedstatement) conn.preparestatement (SQL);
int i = stmt.executeupdate (sql);
} catch (SQLException SQLException) {
System.err.println ("db.executeupdate:" + sqlexception.getmessage ());
}
}
Public ResultSet doselect (String sql) {
try {
stmt = (preparedstatement) conn.preparestatement (SQL);
rs = stmt.executequery (SQL);
SYSTEM.OUT.PRINTLN ("Get result set");
} catch (SQLException SQLException) {
System.err.println ("Db.executequery:" + sqlexception.getmessage ());
}
Return RS;
}
public void Close (ResultSet rs) throws SQLException, Exception {
if (rs! = null) {
Rs.close ();
rs = null;
}
if (stmt! = null) {
Stmt.close ();
stmt = null;
}
IF (conn! = null) {
Conn.close ();
conn = null;
}
}
public void Close () throws SQLException, Exception {
if (stmt! = null) {
Stmt.close ();
stmt = null;
}
IF (conn! = null) {
Conn.close ();
conn = null;
}
}
public static void Main (String []args) throws Exception {
Dbconn db=new dbconn ();
ResultSet rs=db.doselect ("select * from Dbuser where username= ' Chenjirong '");
try {
while (Rs.next ()) {
System.out.println (rs.getstring (1));
System.out.println (rs.getstring (2));
System.out.println (Rs.getstring (3));
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Rs.close ();
Db.close ();
}
}
JDBC Operations under the WEB (JSP)