【问题标题】:JDBC insert errorJDBC插入错误
【发布时间】:2014-10-09 17:55:16
【问题描述】:

我正在尝试使用 jdbc 将一些数据插入到我的数据库中。我正在使用正确的表、数据库名称和参数。一切都经过检查。

代码

public static void main(String[] args) throws IOException/*, ClassNotFoundException, SQLException*/ {
    DataInputStream d = new DataInputStream(System.in);
    System.out.println("Enter employee ID : ");

    @SuppressWarnings("deprecation")
    int empid = Integer.parseInt(d.readLine());

    System.out.println("Enter employee name : ");

    @SuppressWarnings("deprecation")
    String empname = d.readLine();

    System.out.println("Enter employee ID : ");
    @SuppressWarnings("deprecation")
    double empsalary = Double.parseDouble(d.readLine());

    try {
        Class.forName("com.mysql.jdbc.Driver");

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    Connection con;
    try {
        con = DriverManager.getConnection("jdbc:mysql://localhost:3306/testDB","root","");
        PreparedStatement  pst = con.prepareStatement("insert into `employee` values(?,?,?");
        pst.setInt(1, empid);
        pst.setString(2, empname);
        pst.setDouble(3, empsalary);
        int rowcount = pst.executeUpdate();
        System.out.println(rowcount + " row has been instered");
    } catch (SQLException e) {

        e.printStackTrace();
    }

这是我尝试运行它时给我的错误:

com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1604)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1519)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1504)
at com.test.jdbcExample.main(jdbcExample.java:47)

【问题讨论】:

    标签: java mysql sql jdbc insert


    【解决方案1】:

    values 子句中缺少右括号。

    PreparedStatement  pst = con.prepareStatement("insert into employee values(?,?,?)");
    

    【讨论】:

    • 我是从 mysql 论坛上读到的,没有引号我也有同样的问题
    • @Seekerakos 还有缺少的右括号。
    • @Seekerakos 主要问题是缺少右括号,但是您通常还指定要插入的列。如果添加列或表中列的顺序发生变化,您当前的插入将中断。
    • 是的,我解决了 gmarintes 说同样感谢你们的时间!!
    【解决方案2】:

    您在此行中有语法错误:

    PreparedStatement  pst = con.prepareStatement("insert into `employee` values(?,?,?");
    

    您缺少 values 子句的右括号。

    【讨论】:

      猜你喜欢
      • 2018-07-29
      • 1970-01-01
      • 2017-08-03
      • 2014-10-05
      • 1970-01-01
      • 2016-12-26
      • 2014-04-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多