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;
Import com.mysql.jdbc.Statement;
public class Tablecrud {
public static void Main (string[] args) throws Exception {
/*string Driver = "Com.mysql.jdbc.Driver";
String url = "Jdbc:mysql://localhost:3306/student";
String username = "root";
String password = "711109";
Connection conn = null;
Class.forName (driver);
conn = (Connection) drivermanager.getconnection (URL, username, password);
Statement Stmt=null;
ResultSet Rs=null;
stmt= (Statement) conn.createstatement ();
Rs=stmt.executequery ("SELECT * from information");
while (Rs.next ()) {
System.out.println ("Name=" +rs.getstring (1));
System.out.println ("Name=" +rs.getstring (2));
}
Rs.close ();
Stmt.close ();
Conn.close (); */
/*student one=new Student ("Mary", 10);
SelectAll ();
Update (one);
SelectAll ();
Delete (one);
SelectAll ();
Insert (one);
SelectAll (); */
String username01 = "Chenjirong";
int age01 = 20;
ResultSet rs = myquery (
"SELECT * from information where name =?" and age =? ",
USERNAME01, AGE01);
while (Rs.next ()) {
System.out.println (rs.getstring (1));
System.out.println (rs.getstring (2));
}
USERNAME01 = "Myfatpig";
AGE01 = 45;
Myinsert (
Insert into information (name,age) values (?,?),
USERNAME01, AGE01);
}
private static Connection Getconn () {
String Driver = "Com.mysql.jdbc.Driver";
String url = "Jdbc:mysql://localhost:3306/student";
String username = "root";
String password = "711109";
Connection conn = null;
try {
Class.forName (driver);
conn = (Connection) drivermanager.getconnection (URL, username, password);
} catch (ClassNotFoundException e) {
E.printstacktrace ();
} catch (SQLException e) {
E.printstacktrace ();
}
Return conn;
}
private static int Insert (Student Student) {
Connection conn = Getconn ();
int i = 0;
String sql = "INSERT into information (name,age) VALUES (?,?)";
PreparedStatement pstmt;
try {
pstmt = (preparedstatement) conn.preparestatement (SQL);
Pstmt.setstring (1, Student.getname ());
Pstmt.setint (2, Student.getage ());
i = Pstmt.executeupdate ();
Pstmt.close ();
Conn.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
return i;
}
private static int update (Student Student) {
Connection conn = Getconn ();
int i = 0;
String sql = "Update information set age=" +string.valueof (Student.getage () +20) + "where name= '" + student.getname () + "'";
PreparedStatement pstmt;
try {
pstmt = (preparedstatement) conn.preparestatement (SQL);
i = Pstmt.executeupdate ();
System.out.println ("Resutl:" + i);
Pstmt.close ();
Conn.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
return i;
}
private static Integer SelectAll () {
Connection conn = Getconn ();
String sql = "SELECT * from Information";
System.out.println ("************");
PreparedStatement pstmt;
try {
pstmt = (preparedstatement) conn.preparestatement (SQL);
ResultSet rs = Pstmt.executequery ();
int col = Rs.getmetadata (). getColumnCount ();//number of Fields
System.out.println ("************");
while (Rs.next ()) {
for (int i = 1; I <= col; i++) {
System.out.print (rs.getstring (i) + "T");
if ((i = = 2) && (rs.getstring (i). Length () < 8)) {
System.out.print ("\ t");
}
}
System.out.println ("");
}
System.out.println ("============================");
} catch (SQLException e) {
E.printstacktrace ();
}
return null;
}
private static int Delete (Student Student) {
Connection conn = Getconn ();
int i = 0;
String sql = "Delete from information where name= '" + student.getname () + "'";
PreparedStatement pstmt;
try {
pstmt = (preparedstatement) conn.preparestatement (SQL);
i = Pstmt.executeupdate ();
System.out.println ("Resutl:" + i);
Pstmt.close ();
Conn.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
return i;
}
private static ResultSet myquery (String sql, Object ... args) throws Exception {
Connection conn = Getconn ();
PreparedStatement pstmt = (preparedstatement) conn.preparestatement (SQL);
for (int i = 0; i < args.length; i++) {
Pstmt.setobject (i+1, args[i]);
}
return Pstmt.executequery ();
}
private static void Myinsert (String sql, Object ... args) throws Exception {
Connection conn = Getconn ();
PreparedStatement pstmt = (preparedstatement) conn.preparestatement (SQL);
for (int i = 0; i < args.length; i++) {
Pstmt.setobject (i+1, args[i]);
}
Pstmt.executeupdate ();
Pstmt.close ();
}
}
JDBC Operation under Application