我正在创建简单的数据库,所以这是我的数据库模式:
create database studentapp_db ;
create table students_info
( regno int(10) not null,
firstname varchar (50) ,
middlename varchar (50),
lastname varchar (50),
primary key(regno) ) ;
create table gaurdian_info
( regno int(10) not null,
gfirstname varchar (50) ,
gmiddlename varchar (50),
glastname varchar (50),
primary key(regno) ) ;
create table students_otherinfo
( regno int(10) not null,
isadmin varchar (1) ,
passowrd varchar (50),
primary key(regno) ) ;
现在,我从运行配置中插入值,但这会给我这种错误;
unable to create the profile
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 '10,'AAYUSH','KUMAR','NA')' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at
sun.reflect.DelegatingConstructorAccessorImpl.newInstance(UnknownSource)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
at com.mysql.jdbc.Util.getInstance(Util.java:381)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1030)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3515)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3447)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2554)
at
com.mysql.jdbc.PreparedStatement.executeInternal
(PreparedStatement.java:1761)
at com.mysql.jdbc.PreparedStatement.executeUpdate
(PreparedStatement.java:2046)
at com.mysql.jdbc.PreparedStatement.executeUpdate
(PreparedStatement.java:1964)
at com.mysql.jdbc.PreparedStatement.executeUpdate
(PreparedStatement.java:1949)
at com.jspider.jdbc.common.Transactionplusassignmentexample.main
(Transactionplusassignmentexample.java:35)
这是我的java代码:
package com.jspider.jdbc.common;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.mysql.jdbc.Driver;
public class Transactionplusassignmentexample {
public static void main(String[] args) {
java.sql.Connection con = null;
PreparedStatement pstmt1 = null;
PreparedStatement pstmt2 = null;
PreparedStatement pstmt3 = null;
//1 load DB driver
try {
Driver driverRef = new Driver();
DriverManager.registerDriver(driverRef);
//2 Get DB connection via driver
String dbUrl = "jdbc:mysql://localhost:3306
/Studentapp_db?user=j2ee&password=j2ee";
con = DriverManager.getConnection(dbUrl);
//3. Issue sql queries via connection
String query1 = "insert into Students_info" + "values(?,?,?,?)" ;
pstmt1 = con.prepareStatement(query1);
pstmt1.setInt(1,Integer.parseInt(args[0]));
pstmt1.setString(2,args[1]);
pstmt1.setString(3,args[2]);
pstmt1.setString(4,args[3]);
int count1 =pstmt1.executeUpdate();
String query2 = "insert into guardian_info" + "values(?,?,?,?)" ;
pstmt2 = con.prepareStatement(query2);
pstmt2.setInt(1,Integer.parseInt(args[0]));
pstmt2.setString(2,args[4]);
pstmt2.setString(3,args[5]);
pstmt2.setString(4,args[6]);
int count2 =pstmt2.executeUpdate();
String query3 = "insert into Students_otherinfo" +
"values(?,?,?,)" ;
pstmt3 = con.prepareStatement(query3);
pstmt3.setInt(1,Integer.parseInt(args[0]));
pstmt3.setString(2,args[7]);
pstmt3.setString(3,args[8]);
int count3 =pstmt3.executeUpdate();
//4 Process the results
System.out.println("Profile created sucessfully");
System.out.println("row affected for S_I:"+count1);
System.out.println("row affected for G_I:"+count2);
System.out.println("row affected for S_OI:"+count3);
} catch (SQLException e) {
System.err.println("unable to create the profile");
e.printStackTrace();
}finally {
//close all jdbc object
//close all 3 preparedstatement object
try
{ if (con!= null){
con.close();
}
if (pstmt1!= null){
pstmt1.close();
}
if(pstmt2!= null){
pstmt2.close();
}
if(pstmt3!= null){
pstmt3.close();
}
}
catch( SQLException e) {
e.printStackTrace();
}//end of outside class try catch
}
}
}
请帮助我新的java;
答案 0 :(得分:2)
在“Students_otherinfo”和“values”之间添加一个空格
String query3 = "insert into Students_otherinfo " +
"values(?,?,?,)" ;
而不是
String query3 = "insert into Students_otherinfo" +
"values(?,?,?,)" ;
删除额外的“,”:
String query3 = "insert into Students_otherinfo " +
"values(?,?,?)" ;
编辑:评论后
用示意图中的gaurdian取代守护者。或者更改架构(更好)。