一.curd操作
增删改查(curd):Create,Update,Retrieve,Delete
package com.openlab.test;
import com.openlab.util.JDBCUtils;
import java.sql.*;
public class Test02_curd {
public static void main(String agrs[]){
// create();
// delete();
// update();
// select01();
select02(804);
}
// INSERT INTO s_student VALUES(8,'root08','root08','男',8,'8559876234','西安市太白南路')
public static void create(){
JDBCUtils jdbcUtils = new JDBCUtils();
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName(JDBCUtils.DRIVER);
conn = DriverManager.getConnection(JDBCUtils.URL,JDBCUtils.USER,JDBCUtils.PASSWORD);
String sql = "INSERT INTO s_student VALUES(?,?,?,?,?,?,?)";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,8);
pstmt.setString(2,"root08");
pstmt.setString(3,"root08");
pstmt.setString(4,"男");
pstmt.setInt(5,8);
pstmt.setString(6,"8559876234");
pstmt.setString(7,"西安市太白南路");
int rows = pstmt.executeUpdate();
System.out.println(rows);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
jdbcUtils.close(null,pstmt,conn);
}
}
// DELETE FROM s_student WHERE s_no = 8
public static void delete(){
JDBCUtils jdbcUtils = new JDBCUtils();
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName(JDBCUtils.DRIVER);
conn = DriverManager.getConnection(JDBCUtils.URL,JDBCUtils.USER,JDBCUtils.PASSWORD);
String sql = "DELETE FROM s_student WHERE s_no = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1,8);
int rows = pstmt.executeUpdate();
System.out.println(rows);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
jdbcUtils.close(null,pstmt,conn);
}
}
//UPDATE s_student SET s_no = 9, s_user = 'root09' , s_password='root09' WHERE s_no = 6
public static void update() {
JDBCUtils jdbcUtils = new JDBCUtils();
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName(JDBCUtils.DRIVER);
conn = DriverManager.getConnection(JDBCUtils.URL, JDBCUtils.USER, JDBCUtils.PASSWORD);
String sql = "UPDATE s_student SET s_no = ?, s_user = ? , s_password=? WHERE s_no = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, 6);
pstmt.setString(2, "root06");
pstmt.setString(3, "root06");
pstmt.setInt(4, 9);
int rows = pstmt.executeUpdate();
System.out.println(rows);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
jdbcUtils.