尽管正确,但我的JAVA程序中的SQL查询还是使用Prepared语句引发了错误

时间:2018-10-19 02:01:02

标签: java mysql jdbc xampp

在我的Java程序中,我试图使用准备好的语句在MySQL表中进行插入操作。

这是我的SQL查询:

PreparedStatement stmt = null;

String sql="INSERT INTO registration"+"(phone,name,address,city,destination,date) VALUES"+ "(?,?,?,?,?,?)";

try{
    //STEP 2: Register JDBC driver
    //Class.forName("com.mysql.jdbc.Driver");

    //STEP 3: Open a connection
    System.out.println("Connecting to a selected database...");
    conn = DriverManager.getConnection(DB_URL, USER, PASS);
    System.out.println("Connected database successfully...");
    stmt=conn.prepareStatement(sql);

    //STEP 4: Execute a query
    System.out.println("Inserting records into the table...");
   // stmt = conn.createStatement();
    stmt.setInt(1, phone[i]);
    stmt.setString(2, name[i]);
    stmt.setString(3, address[i]);
    stmt.setString(4, city[i]);
    stmt.setString(5, destination[i]);
    stmt.setString(6, date[i]);

    stmt.executeUpdate(sql);
}catch(SQLException se){
    //Handle errors for JDBC
    se.printStackTrace();
}catch(Exception e){
    //Handle errors for Class.forName
    e.printStackTrace();
}finally{
    //finally block used to close resources
   try{
        if(stmt!=null)
            conn.close();
    }catch(SQLException se){
    }// do nothing
    try{
        if(conn!=null)
            conn.close();
    }catch(SQLException se){
        se.printStackTrace();
    }//end finally try
}//end try
System.out.println("Goodbye!");
break;

我似乎在SQL查询中找不到任何错误,但是程序抛出语法错误。如果需要,这是上下文的完整程序;

import java.util.*;
import java.sql.*;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

    public class Airplane {
        // JDBC driver name and database URL
        static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
        static final String DB_URL = "jdbc:mysql://localhost:3306/airplane";

        //  Database credentials
        static final String USER = "root";
        static final String PASS = "";

    public static void main(String args[])
    {   int i=0;int choice;
        Scanner sc=new Scanner(System.in);
        String name[]=new String[1000];
        String address[]=new String[1000];
        int phone[]=new int[1000];
        String city[]=new String[1000];
        String destination[]=new String[1000];
        String date[]=new String[1000];

        do {

            System.out.println("WELCOME TO AIRPLANE MANAGEMENT SYSTEM");
            System.out.println("Select your operation :");
            System.out.println("1:New Booking");
            System.out.println("2:List");
            System.out.println("3:Search");
            System.out.println("4:Edit");
            System.out.println("5:Delete");
            System.out.println("6:Bill");

            choice = sc.nextInt();
            switch (choice) {
                case 1: {
                    System.out.println("Enter your Phone number:");
                    phone[i] = sc.nextInt();
                    sc.nextLine();

                    System.out.println("Enter your name:");
                    name[i] = sc.nextLine();


                    System.out.println("Enter your address:");
                    address[i] = sc.nextLine();

                    System.out.println("Enter your Pick up city:");
                    city[i] = sc.nextLine();


                    System.out.println("Enter your Destination:");
                    destination[i] = sc.nextLine();


                    System.out.println("Enter your Date:");
                    date[i] = sc.nextLine();
                    ++i;
                    Connection conn = null;
                    PreparedStatement stmt = null;

                    String sql="INSERT INTO registration"+"(phone,name,address,city,destination,date) VALUES"+ "(?,?,?,?,?,?)";

                    try{
                        //STEP 2: Register JDBC driver
                        //Class.forName("com.mysql.jdbc.Driver");

                        //STEP 3: Open a connection
                        System.out.println("Connecting to a selected database...");
                        conn = DriverManager.getConnection(DB_URL, USER, PASS);
                        System.out.println("Connected database successfully...");
                        stmt=conn.prepareStatement(sql);

                        //STEP 4: Execute a query
                        System.out.println("Inserting records into the table...");
                       // stmt = conn.createStatement();
                        stmt.setInt(1, phone[i]);
                        stmt.setString(2, name[i]);
                        stmt.setString(3, address[i]);
                        stmt.setString(4, city[i]);
                        stmt.setString(5, destination[i]);
                        stmt.setString(6, date[i]);

                        stmt.executeUpdate(sql);
                    }catch(SQLException se){
                        //Handle errors for JDBC
                        se.printStackTrace();
                    }catch(Exception e){
                        //Handle errors for Class.forName
                        e.printStackTrace();
                    }finally{
                        //finally block used to close resources
                       try{
                            if(stmt!=null)
                                conn.close();
                        }catch(SQLException se){
                        }// do nothing
                        try{
                            if(conn!=null)
                                conn.close();
                        }catch(SQLException se){
                            se.printStackTrace();
                        }//end finally try
                    }//end try
                    System.out.println("Goodbye!");
                    break;
                }


                case 2:
                    System.out.println("Name\tAddress\tContactNo\tPickUp City\tDestination\tDate");
                    for (int m = 0; m < i; m++) {
                        System.out.println(name[m] + "\t" + address[m] + "\t" + phone[m] + "\t" + city[m] + "\t\t" + destination[m] + "\t" + date[m]);

                    }
                    break;

                case 3:
                    System.out.println("enter the contact number to see details");
                    int search=sc.nextInt();
                    for (int m = 0; m < i; m++) {
                        if(search==phone[m])
                        System.out.println(name[m] + "\t" + address[m] + "\t" + phone[m] + "\t" + city[m] + "\t" + destination[m] + "\t" + date[m]);

                    }
                    break;
                case 4:
                    break;

                case 5:
                    break;
                case 6:
                    break;

            }
        }while(choice!=0);



    }//end main
    }//end JDBCExample

我做错了什么?

程序显示此错误:

  

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您有一个   您的SQL语法错误;检查与您的手册相对应的手册   MariaDB服务器版本可在'?,?,?,?,?,?,?)'附近使用正确的语法   在第1行   sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)     在   sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)     在   sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)     在java.lang.reflect.Constructor.newInstance(Constructor.java:423)     在com.mysql.jdbc.Util.handleNewInstance(Util.java:404)处   com.mysql.jdbc.Util.getInstance(Util.java:387)在   com.mysql.jdbc.SQLError.createSQLException(SQLError.java:942)在   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)在   com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)在   com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)在   com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)在   com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)在   com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1540)     在   com.mysql.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2595)     在   com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1468)     在Airplane.main(Airplane.java:90)

1 个答案:

答案 0 :(得分:1)

您的问题似乎在这里:

stmt.executeUpdate(sql);

您已经对以下语句进行了原始字符串查询:

stmt=conn.prepareStatement(sql);

PreparedStatement的{​​{1}}版本没有参数。看起来您正在executeUpdate()继承的Statement界面中找到版本,该界面运行作为参数传入的字符串。

我认为,如果您从通话中删除sql字符串,它将起作用:

PreparedStatement