简体   繁体   中英

Insert into a table with a foreign key in mysql using java

I'm being unable to insert into a table with a foreign key

String querystate = " insert into state (country_idcountry, State, short, Km2, Capital, Largest City)"
                    + " values (?, ?, ?, ?, ?, ?)";
preparedStmt = conn.prepareStatement(querystate);
preparedStmt.setInt(1, cID);
preparedStmt.setString(2, sName);
preparedStmt.setString(3,sShort);
preparedStmt.setInt(4, sArea);
preparedStmt.setString(5, sCapital);
preparedStmt.setString(6, sLargest_city);
preparedStmt.execute();

cID its the foreign key, I'm sure that I'm doing something wrong, but i don't know what is it.

Exception in thread "main" java.lang.IllegalStateException: Cannot connect the database!
at DB_Connect.TryConn(DB_Connect.java:67)
at Main.main(Main.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
Caused by: 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 'mas grande) values (1, 'Córdoba', 'CBA', 165421, 'Córdoba', 'Córdoba')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3970)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3906)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2524)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2677)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2549)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
at DB_Connect.TryConn(DB_Connect.java:46)
... 6 more

If you have a column with a space in it then the column will need to be quoted

eg

insert into state (country_idcountry, State, short, Km2, Capital, 'Largest City')"
                + " values (?, ?, ?, ?, ?, ?)";

Your SQL syntax is incorrect.

In your prepared statement you should mention only column names in bracket and question mark for each corresponding column. And then you can set value for each column using prepared Statement set methods.

Suppose your table is like below,

state (country_id int, country text, state text, distance int, capital text, largest_city text)

Then your insert query syntax should be like

String query= " insert into state (country_id , country , state , distance , capital , largest_city) values (?, ?, ?, ?, ?, ?)";
preparedStmt = conn.prepareStatement(query);
preparedStmt.setInt(1, cID);
preparedStmt.setString(2, cName);
preparedStmt.setString(3,sName);
preparedStmt.setInt(4, distance);
preparedStmt.setString(5, sCapital);
preparedStmt.setString(6, sLargest_city);
preparedStmt.execute();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM