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了。
但是查询语句又没有报错,真不知道这是什么鬼。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值