java - 无法创建配置文件 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : You have an error in your SQL syntax;

标签 java mysql jdbc

我正在制作简单的数据库,所以这是我的数据库架构:

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 ;

最佳答案

在“Students_otherinfo”和“值”之间添加空格

String query3 = "insert into Students_otherinfo " + 
    "values(?,?,?,)" ;

而不是

String query3 = "insert into Students_otherinfo" + 
    "values(?,?,?,)" ;

并删除多余的“,”:

 String query3 = "insert into Students_otherinfo " + 
    "values(?,?,?)" ;

编辑:评论后

并像方案中那样用守护者替换守护者。或者更改架构(更好)。

关于java - 无法创建配置文件 com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : You have an error in your SQL syntax;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31566913/

相关文章:

java - 如何使用 Java 中的 Calendar 对象获取特定日期信息?

php - MySQL JOIN 2个表并分别获取两个表的总和

php - 如何在mysql中管理并发?

java - 无法使用 DriverManager 连接到 mysql 数据库

java - 在 openshift 上连接到 jdbc mysql

java - 整个应用程序的静态方法内的连接静态变量的范围?

java - 构建 Java 树

java - Mesos 任务状态时间戳

java - 在 ImageView 上添加多个效果

PHP/MySQL 登录 -> 每个客户的模式