Error inserting data into table

Asked

Viewed 37 times

0

Opening Connection:

private static final String USUARIO = "root";
private static final String SENHA = "123456";
private static final String URL = "jdbc:mysql://127.0.0.1:3306/escolabd";
private static final String DRIVER = "com.mysql.jdbc.Driver";

public static Connection abrir() throws Exception
{   
    Class.forName(DRIVER);

    conn = DriverManager.getConnection(URL, USUARIO, SENHA);

    return conn;
}

public Main()
{
    try {
        abrir();
    } catch (Exception e1) {
        e1.printStackTrace();
        return;
    }

Code:

String sql = "INSERT INTO 303(Id, Aula) VALUES(?,?)";

    try {
        PreparedStatement stmt = conn.prepareStatement(sql);

        stmt.setString(1, "0");
        stmt.setString(2, "Portugues");

        stmt.execute(); //linha 464
        stmt.close();           

    } catch (SQLException e1) {
        e1.printStackTrace();
    }

Error:

com.mysql.jdbc.exceptions.jdbc4.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 '303(Id, Aula) VALUES('0','Portugues')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:942)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5098)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
at Default.Main.adicionar(Main.java:464)
at Default.Main$2.actionPerformed(Main.java:413)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

inserir a descrição da imagem aqui

Note: On line 413 I just call the method.

  • Where you’re opening the connection?

  • @Diegoaugusto Editei now, I put in question.

  • Which your Stacktrace completes?

  • And just to test, change the stmt.execute() for stmt.executeUpdate()

  • I switched to stmt.executeUpdate() and kept giving error, I put the complete error in the question.

  • Well, the problem is in your Query. I’ll take a look

  • Your field Id probably is the type int in your table. If it is, you should not use stmt.setString(1, "0"); and yes, stmt.setInt(1, 0);

  • @Lucascarezia Try to change the name of your table, when you are trying to do Insert it recognizes your table name (303) as an integer

  • @Diegoaugusto Yes, he changes the name funciono, vlw.

  • @Diegof I made that mistake, vlw by the tip :)

Show 5 more comments

2 answers

2

Hello, a table name can start with numbers, but not just numbers. Try to change the name of your table, a tip is that whenever your table has special characters or is a reserved word, put the name of the table among the crases, for example: select.

1


The problem is in the name of your table. Note that you are putting an integer value as a name. If you try to run the query directly into a SGBD may come the error message with more details.

Try changing the name of your table 303 for tbl_303, refactor your code and take a test.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.