JDBC插入错误 [英] JDBC insert error

查看:161
本文介绍了JDBC插入错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to insert some data into my database with jdbc. I am using the correct table, db name, and parameters. Everything is checked.

代码

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();
    }

以下是我尝试运行时给出的错误:

and here are the errors that it gives me when I try to run it:

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)


推荐答案

此行中存在语法错误:

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

您缺少值子句的右括号。

You are missing the closing parenthesis for the values clause.

这篇关于JDBC插入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆