JDBC Operations under the WEB (JSP)

Source: Internet
Author: User
Tags getmessage stmt

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.