gpt4 book ai didi

java - 尝试使用 JAVA 将输入保存到 SQL 时出错

转载 作者:行者123 更新时间:2023-11-30 05:23:41 24 4
gpt4 key购买 nike

我正在尝试使用 Java 在 MySQL 中输入我的详细信息。但我仍然出现以下错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '?, ?)' at line 1

这是我的代码:

Vehicle vehicle = new Vehicle();
int vType;
System.out.println("Please enter Vehicle Type\n1 = Car\n2 = Van\n3 = Motorbike\n4 = Motorbike");
vType = input.nextInt();
if (vType==1){
System.out.println("Car Brand: ");
vehicle.setvBrand(input.next());
System.out.println("Car License Plate: ");
vehicle.setvLicense(input.next());
try {
Connection dbConn = DriverManager.getConnection(url,user,pass);
String parkCar = "INSERT INTO car_park_details(vehicle_brand, vehicle_license) values( ?, ?)";
PreparedStatement park = dbConn.prepareStatement(parkCar);
park.executeUpdate(parkCar);
park.setString(2,vehicle.getvBrand());
park.setString(3, vehicle.getvLicense());
park.execute();
System.out.println("Try daw check sa DB MYONG!");
}
catch (Exception ex){
System.out.println("Error" + ex);
}
}

我做错了吗?我是一名 Java 开发新手。感谢您的帮助。

最佳答案

PreparedStatement park =       dbConn.prepareStatement(parkCar);   
park.setString(1, vehicle.getvBrand());
park.setString(2, vehicle.getvLicense());
park.executeUpdate();

PreparedStatement设置参数索引从1开始。

关于java - 尝试使用 JAVA 将输入保存到 SQL 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59084492/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com