仔细阅读了代码发现
import java.sql.*;
public class DataBase {
public static void main(String[] args) throws SQLException {
Connection con;
PreparedStatement sql;
ResultSet res;
Gradation c = new Gradation();
con = c.getConnection();
// sql=con.prepareStatement("insert into test.ts value(?,?,?);");
// sql.setInt(1,174314);
// sql.setString(2,"胡彩月");
// sql.setString(3,"2");
// sql.executeUpdate();
c.insert();
c.delete();
c.update();
try {
sql = con.prepareStatement("select *from test.ts");
// res=sql.executeQuery("select *from test.ts"+"where number =?");
res = sql.executeQuery();
while (res.next()) {
int numbers = res.getInt(1);
String name = res.getString("name");
String sex = res.getString("sex");
System.out.println(numbers + " :" + name + " :" + sex);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
class Gradation {
Connection connection;
public Connection getConnection() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8", "root", "123456");
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}
public void insert() throws SQLException {
Connection con = this.getConnection(); //不写就空指针异常
PreparedStatement sql;
ResultSet res;
try {
assert false;
sql = con.prepareStatement("insert into test.ts value(?,?,?);");
sql.setInt(1, 3222);
sql.setString(2, "胡彩月");
sql.setString(3, "2");
// sql.executeUpdate();
sql.executeUpdate("delete from test.ts where number =1744");
sql.setInt(1, 3222);
sql.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void delete() throws SQLException {
Connection con = this.getConnection(); //不写就空指针异常
String sqlDelete = "delete from test.ts where number =(?)";
PreparedStatement sql;
ResultSet res;
try {
assert false;
// sql.executeUpdate("delete *from test.ts where number =123");
sql = con.prepareStatement(sqlDelete);
sql.setInt(1, 3222);
sql.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void update(){
Connection con = this.getConnection(); //不写就空指针异常
String sqlUpdate = "update test.ts set name=? where number=1707004314";
PreparedStatement sql;
try {
sql=con.prepareStatement(sqlUpdate);
sql.setString(1,"黄洪飞");
sql.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
}
}
在执行删除语句时
"delete *from test.ts where number =(?)"
把*号去掉就ok了。
但是查询语句又没有报错,真不知道这是什么鬼。