Java MySQL ODBC PreparedStatement切断查询

时间:2014-03-21 06:12:24

标签: java mysql odbc prepared-statement

`我在为Java ODBC MySQL做一个PreparedStatement时遇到了问题。它似乎正在切断查询,并给出语法错误。我不知道如何继续,因为我现在只学习Java SQL。我不能真正做一个自包含的例子,因为问题涉及数据库,而且它会变得非常大。

有问题的代码是这个..

public void insertEntry(
            Hashtable<String, String> strings,
            Hashtable<String, Integer> integers,
            Date created, Date paid, boolean enabled)
            throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");

        String dburl = "jdbc:mysql://" + dbHost + "/" + dbName +
                        "?user=" + dbUser + "&password=" + dbPass;

        connect = DriverManager.getConnection(dburl);

        ps = connect.prepareStatement("INSERT INTO " + dbName + ".users INSERT " +
                "enabled=?, username=?, created=?, paid=?, alias=?, password=?, " +
                "email=?, bitmessage=?, torchat=?, reputation=?," +
                "privacy=?, fpmport=?, fpm-template=? ;");

        java.sql.Date SQLcreated = new java.sql.Date(created.getTime());
        java.sql.Date SQLpaid = new java.sql.Date(paid.getTime());
        System.out.println("Debug: SQLpaid = " + SQLpaid.toString());

        ps.setBoolean(1, enabled);
        ps.setString(2, strings.get("username"));
        ps.setDate(3, SQLcreated);
        ps.setDate(4, SQLpaid);
        ps.setString(5, strings.get("alias"));
        ps.setString(6, strings.get("password"));
        ps.setString(7, strings.get("email"));
        ps.setString(8, strings.get("bitmessage"));
        ps.setString(9, strings.get("torchat"));
        ps.setInt(10, integers.get("reputation"));
        ps.setInt(11, integers.get("privacy"));
        ps.setInt(12, integers.get("fpmport"));
        ps.setString(13, strings.get("fpm-template"));
        ps.executeUpdate();

        ps.close();
        connect.close();
        resultSet.close();
    }

尝试使用此方法时,我得到以下输出...

Debug: SQLpaid = 1990-03-21
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: 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 'INSERT enabled=0, username='default_username', created='2000-03-21', paid='1990-' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Construc tor.java:532)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:41 1)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLErro r.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:26 17)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionIm pl.java:2834)
at com.mysql.jdbc.PreparedStatement.executeInternal(P reparedStatement.java:2156)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:2441)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:2366)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:2350)
at database.Users.insertEntry(Users.java:297)
at test.dbUsers.main(dbUsers.java:95) 

2 个答案:

答案 0 :(得分:1)

我认为您在代码中犯了一些错误:

以下是:

1。您的提及INSERT,您应该更改为SET

2。您在SQL查询中添加;应该删除它。

您的代码:

 ps = connect.prepareStatement
 ("INSERT INTO " + dbName + ".users  INSERT " #look here error to change 'set' +
  "enabled=?, username=?, created=?, paid=?, alias=?, password=?, " +
  "email=?, bitmessage=?, torchat=?, reputation=?," +
  "privacy=?, fpmport=?, fpm-template=?  ; " #remove this semicolon(;));

您必须更改为:

 ps = connect.prepareStatement
           ("INSERT INTO " + dbName + ".users SET " +
           "enabled=?, username=?, created=?, paid=?, alias=?, password=?, " +
           "email=?, bitmessage=?, torchat=?, reputation=?," +
           "privacy=?, fpmport=?, fpm-template=? ");

答案 1 :(得分:0)

更改:

INSERT INTO " + dbName + ".users INSERT "

致:

INSERT INTO " + dbName + ".users SET "

请参阅

MySQL: INSERT Syntax

  

INSERT [LOW_PRIORITY |延迟| HIGH_PIORITY] [IGNORE]
      [INTO] tbl_name
      [PARTITION(partition_name,...)]
      SET col_name = {expr | DEFAULT},...
      [关于重复主题更新
        COL_NAME = EXPR
          [,col_name = expr] ...]