我是JDBC的初学者,我正试图在表格中插入一条记录,就像我创建raja
一样:
所以这是我的计划:
import java.sql.*;
public class raj{
public static void main(String[] args){
try{
Connection con=DriverManager.getConnection(
"jdbc:mysql://localhost:9999/raj1", "root", "1234"
);
Statement stmt=con.createStatement();
String sql="insert into raja"+
"values(1, 'Rajendra Arora', 22)";
stmt.executeUpdate(sql);
sql="insert into raja"+
"values(2, 'Reena arora', 40)";
stmt.executeUpdate(sql);
sql="insert into raja"+
"values(3, 'Manohar lal arora', 45)";
stmt.executeUpdate(sql);
sql="insert into raja"+
"values(4, 'Suman and sudha arora', 20)";
stmt.executeUpdate(sql);
System.out.println("table records inserted succesfully....");
}catch(Exception e){e.printStackTrace();}
}
}
但在执行期间,显示的错误如下:
"C:\Program Files\Java\jdk1.8.0\bin\java" -Didea.launcher.port=7539 "-Didea.launcher.bin.path=C:\Program Files\JetBrains\IntelliJ IDEA 13.1.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\rt.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\access-bridge.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0\jre\lib\ext\zipfs.jar;C:\Users\Raj Arora\IdeaProjects\untitled\out\production\untitled;C:\Users\Raj Arora\.IntelliJIdea13\config\jdbc-drivers\mysql-connector-java-5.1.29-bin.jar;C:\Program Files\JetBrains\IntelliJ IDEA 13.1.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain raj
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 '1, 'Rajendra Arora', 22)' 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:408)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1054)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4237)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4169)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2617)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2778)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2828)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1842)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1764)
at raj.main(raj.java:13)
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:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Process finished with exit code 0
答案 0 :(得分:3)
永远不要忘记空格:
String sql="insert into raja(col1,col2,col3) "+
"values(1, 'Rajendra Arora', 22)";
答案 1 :(得分:0)
查看您的SQL请求! :
sql="insert into raja"+" values(3, 'Manohar lal arora', 45)";
添加空格!
答案 2 :(得分:0)
我认为你需要添加一个"白色空间"介于" raja"和"价值观"。
此外,调试这些问题的最佳方法是在控制台中打印最终的sql并在任何数据库编辑器中使用它。
答案 3 :(得分:0)
在你的问题中你提到你的sql如
String sql="insert into raja"+
"values(1, 'Rajendra Arora', 22)";
但在
raja"+ "values
是 rajavalues。
必须 raja值
所以把它作为
String sql="insert into raja "+
"values(1, 'Rajendra Arora', 22)";