【发布时间】:2019-03-23 21:40:30
【问题描述】:
在我的 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)
【问题讨论】:
-
我确实看到您的 JDBC 代码存在一些潜在问题,但该错误似乎与与 MySQL 本身的连接不正确有关。你确定你的连接字符串是对的吗?
-
不相关,但您需要在执行 SQL 查询后调用
i++,否则您将使用空值,因为您从用户那里获得的索引有i -
@TimBiegeleisen 我已经更新了我的问题。请看。我现在只有一个错误。我没有通过 XAMPP 启动 sql 服务器,这就是为什么我收到连接错误。现在我只有错误。你能看到吗?
-
您可能需要在插入查询中添加一些空格,即使用:
INSERT INTO registration (phone, name, address, city, destination, date) VALUES (?,?,?,?,?,?)。 -
@HovercraftFullOfEels 我没有拒绝您的任何编辑