Java 为什么我会收到这个sql错误?
我在NetBeans中启动java代码,我想在数据库Java 为什么我会收到这个sql错误?,java,mysql,sql-insert,sqlexception,Java,Mysql,Sql Insert,Sqlexception,我在NetBeans中启动java代码,我想在数据库firstdb中的numbersdb表中插入一些数据。我运行程序时遇到问题 我的代码是: public static void main(String [] arg){ try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("Driver loaded!"); Connection dbc = DriverMana
firstdb
中的numbersdb
表中插入一些数据。我运行程序时遇到问题
我的代码是:
public static void main(String [] arg){
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded!");
Connection dbc = DriverManager.getConnection("jdbc:mysql://localhost:3306/firstdb", "root","root");
System.out.println("Connection to database succeded!");
boolean evening = false;
int n100 = 1;
int n10 = 2;
int n1 = 3;
int wn = 123;
int month = 0;
int day = 0;
int year = 0;
java.sql.Date date_released = new java.sql.Date(Calendar.getInstance().getTime().getTime());
String query = "insert into numbersdb (hundreds_place, tens_place, ones_place, whole_number, evening, date_released, day, month, year) "+
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement preparedStatement = dbc.prepareStatement(query);
preparedStatement.setInt(1, n100);
preparedStatement.setInt(2, n10);
preparedStatement.setInt(3, n1);
preparedStatement.setInt(4, wn);
preparedStatement.setBoolean(5, evening);
preparedStatement.setDate(6, date_released);
preparedStatement.setInt(7, day);
preparedStatement.setInt(8, month);
preparedStatement.setInt(9, year);
preparedStatement.execute(query);
System.out.println("Query executed!!!");
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot find the driver in the classpath!", e);
}
}
我在phpmyadmin中使用以下sql代码创建了数据库:
create table numbersdb (
hundreds_place int unsigned not null,
tens_place int unsigned not null,
ones_place int unsigned not null,
whole_number int unsigned not null,
evening boolean not null,
date_released date not null,
day int unsigned not null,
month int unsigned not null,
year int unsigned not null,
primary key (date_released)
);
我的程序输出如下:
public static void main(String [] arg){
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded!");
Connection dbc = DriverManager.getConnection("jdbc:mysql://localhost:3306/firstdb", "root","root");
System.out.println("Connection to database succeded!");
boolean evening = false;
int n100 = 1;
int n10 = 2;
int n1 = 3;
int wn = 123;
int month = 0;
int day = 0;
int year = 0;
java.sql.Date date_released = new java.sql.Date(Calendar.getInstance().getTime().getTime());
String query = "insert into numbersdb (hundreds_place, tens_place, ones_place, whole_number, evening, date_released, day, month, year) "+
" VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
PreparedStatement preparedStatement = dbc.prepareStatement(query);
preparedStatement.setInt(1, n100);
preparedStatement.setInt(2, n10);
preparedStatement.setInt(3, n1);
preparedStatement.setInt(4, wn);
preparedStatement.setBoolean(5, evening);
preparedStatement.setDate(6, date_released);
preparedStatement.setInt(7, day);
preparedStatement.setInt(8, month);
preparedStatement.setInt(9, year);
preparedStatement.execute(query);
System.out.println("Query executed!!!");
} catch (SQLException ex) {
System.out.println("SQLException: " + ex.getMessage());
System.out.println("SQLState: " + ex.getSQLState());
System.out.println("VendorError: " + ex.getErrorCode());
} catch (ClassNotFoundException e) {
throw new RuntimeException("Cannot find the driver in the classpath!", e);
}
}
司机上膛了!连接到数据库成功!SQLException:您的SQL语法有错误;检查手册
对应于您的MySQL服务器版本,以便使用正确的语法
在第1行的“?、?、?、?、?、?、?、?、?)”附近,SQLState:42000供应商错误:1064 为什么我会得到这个例外
preparedStatement.execute(query);
此行使用从语句继承的.execute(String)
。所述方法仅执行现在包含?
的给定查询
删除参数以使用正确的方法,它就会工作
preparedStatement.execute();
此行使用从语句继承的.execute(String)
。所述方法仅执行现在包含?
的给定查询
删除参数以使用正确的方法,它就会工作
preparedStatement.execute();
此行使用从语句继承的.execute(String)
。所述方法仅执行现在包含?
的给定查询
删除参数以使用正确的方法,它就会工作
preparedStatement.execute();
此行使用从语句继承的.execute(String)
。所述方法仅执行现在包含?
的给定查询
删除参数以使用正确的方法,它就会工作
preparedStatement.execute();
+1.但是preparedStatement.executeUpdate()
可能更合适。+1。但是preparedStatement.executeUpdate()
可能更合适。+1。但是preparedStatement.executeUpdate()
可能更合适。+1。但是preparedStatement.executeUpdate()
可能更合适。