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)
Note: On line 413 I just call the method.
Where you’re opening the connection?
– DiegoAugusto
@Diegoaugusto Editei now, I put in question.
– Lucas Caresia
Which your Stacktrace completes?
– DiegoAugusto
And just to test, change the
stmt.execute()
forstmt.executeUpdate()
– DiegoAugusto
I switched to
stmt.executeUpdate()
and kept giving error, I put the complete error in the question.– Lucas Caresia
Well, the problem is in your Query. I’ll take a look
– DiegoAugusto
Your field
Id
probably is the typeint
in your table. If it is, you should not usestmt.setString(1, "0");
and yes,stmt.setInt(1, 0);
– user28595
@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
@Diegoaugusto Yes, he changes the name funciono, vlw.
– Lucas Caresia
@Diegof I made that mistake, vlw by the tip :)
– Lucas Caresia