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();
}
}