Question

`I am having a problem with doing a PreparedStatement for Java ODBC MySQL. It seems to be cutting off the query, and giving a syntax error. I am not sure how to proceed, as I am only learning Java SQL at this point. I can't really do a self contained example because the problem involves databases, and it would get quite big.

The code with the problem is this..

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

I get the following output when trying to use this method...

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) 
Was it helpful?

Solution 2

Change:

INSERT INTO " + dbName + ".users INSERT "

To:

INSERT INTO " + dbName + ".users SET "

Refer to:

MySQL: INSERT Syntax

INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
[INTO] tbl_name
[PARTITION (partition_name,...)]
SET col_name={expr | DEFAULT}, ...
[ ON DUPLICATE KEY UPDATE
col_name=expr
[, col_name=expr] ... ]

OTHER TIPS

i think your doing some mistake in your code:

these are following as:

1. your mention INSERT and you should must change like SET

2. your adding the ;in your SQL Query you should remove that.

your code:

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

you should must change like as:

 ps = connect.prepareStatement
           ("INSERT INTO " + dbName + ".users SET " +
           "enabled=?, username=?, created=?, paid=?, alias=?, password=?, " +
           "email=?, bitmessage=?, torchat=?, reputation=?," +
           "privacy=?, fpmport=?, fpm-template=? ");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top