The example in this article describes the JDBC operation method for MySQL database. Share to everyone for your reference. as follows:
Import java.sql.*;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;
Class conn{String url= "Jdbc:mysql://127.0.0.1/naridata";
Final String user = "Nari";
Final String pass = "NARIACC";
Connection conn = null;
Statement st = null;
ResultSet rs = null;
Conn () {try{class.forname ("Com.mysql.jdbc.Driver");
}catch (Exception e) {e.printstacktrace ();
} try{System.out.println ("in Dbmanager");
conn = drivermanager.getconnection (URL, user, pass);
String sql = "CREATE TABLE test (ID int, uid int)";
st = Conn.createstatement ();
St.execute (SQL);
}catch (Exception e) {e.printstacktrace ();
} void Insert () {try{for (int i=0;i<10;i++) {String sql = ' INSERT into test values (' +i+ ', ' +i+ ') ';
St.execute (SQL);
}}catch (Exception e) {e.printstacktrace ();
} void Query () {try{String sql = ' select * from Test ';
rs = st.executequery (SQL);
while (Rs.next ()) { System.out.print ("Res1:" +rs.getint (1) + "Res2:" +rs.getint (2) + "\ n");
}}catch (Exception e) {e.printstacktrace ();
} void Close () {try{rs.close ();
St.close ();
Conn.close ();
}catch (Exception e) {e.printstacktrace (); }} public class Mysqltest {public static void main (string[] args) {//TODO auto-generated method Stub conn =
New Conn ();
A1.insert ();
A1.query ();
A1.close ();
}
}
I hope this article will help you with your JSP programming.