java报错java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that
生活随笔
收集整理的這篇文章主要介紹了
java报错java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
仔細閱讀了代碼發現
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了。
但是查詢語句又沒有報錯,真不知道這是什么鬼。
總結
以上是生活随笔為你收集整理的java报错java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: HomeAssistant和Node-R
- 下一篇: javaSwing+MySQL实现图书管