Java Swing,Mysql 来自 PreparedStatement 的错误
Java Swing, Mysql error from PreparedStatement
这是我在 mysql 上的 table:
这是我的 Java 代码:
public static final String username = "root";
public static final String password = "sundayman";
public static final String connect = "jdbc:mysql://localhost:3306/lives";
Connection sqlConn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
sqlConn = DriverManager.getConnection(connect, username, password);
pst = (PreparedStatement)sqlConn.prepareStatement("insert into lives_table(Name,Latin Name,Live's Class,Cell Structure,Nutrition,Respiratory,Move,Reproductive)"+"values(?,?,?,?,?,?,?,?)");
pst.setString(1, l.getName());
pst.setString(2, l.getLatin());
pst.setString(3, l.getLive_class());
pst.setString(4, l.getCell_type());
pst.setString(5, l.getFeed());
pst.setString(6, l.getO2());
pst.setString(7, l.getMove());
pst.setString(8, l.getReproductive());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Live Recorded!");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
}
}
我收到这个错误:
java.sql.SQLException:参数索引超出范围(1 > 参数个数,即0)。
怎么了?
如果需要列名有特殊字符,必须在back-ticks:
中分隔
错误:
insert into lives_table(Name,Latin Name,Live's Class,Cell Structure,Nutrition,Respiratory,Move,Reproductive)...
右:
insert into lives_table(Name,`Latin Name`,`Live's Class`,`Cell Structure`,Nutrition,Respiratory,Move,Reproductive)...
大多数人认为避免在列名称中使用空格和标点符号更简单。
这是我在 mysql 上的 table:
这是我的 Java 代码:
public static final String username = "root";
public static final String password = "sundayman";
public static final String connect = "jdbc:mysql://localhost:3306/lives";
Connection sqlConn = null;
PreparedStatement pst = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
sqlConn = DriverManager.getConnection(connect, username, password);
pst = (PreparedStatement)sqlConn.prepareStatement("insert into lives_table(Name,Latin Name,Live's Class,Cell Structure,Nutrition,Respiratory,Move,Reproductive)"+"values(?,?,?,?,?,?,?,?)");
pst.setString(1, l.getName());
pst.setString(2, l.getLatin());
pst.setString(3, l.getLive_class());
pst.setString(4, l.getCell_type());
pst.setString(5, l.getFeed());
pst.setString(6, l.getO2());
pst.setString(7, l.getMove());
pst.setString(8, l.getReproductive());
pst.executeUpdate();
JOptionPane.showMessageDialog(null, "Live Recorded!");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(Register.class.getName()).log(Level.SEVERE, null, ex);
}
}
我收到这个错误: java.sql.SQLException:参数索引超出范围(1 > 参数个数,即0)。
怎么了?
如果需要列名有特殊字符,必须在back-ticks:
中分隔错误:
insert into lives_table(Name,Latin Name,Live's Class,Cell Structure,Nutrition,Respiratory,Move,Reproductive)...
右:
insert into lives_table(Name,`Latin Name`,`Live's Class`,`Cell Structure`,Nutrition,Respiratory,Move,Reproductive)...
大多数人认为避免在列名称中使用空格和标点符号更简单。