下面是我的代码,我得到一个异常索引超出范围,我无法弄清楚为什么我使用netbeans IDE。是因为"#"字符,我没有发现任何麻烦创建一个名称为"a#"或"B"的表。
class DBS{
public boolean insertAt(String tableName){
String query ;
String insertAt;
java.sql.TimeStamp sysTime;
boolean result
Switch(tableName){
case "A":
sysTime =
insertAt = "A";
String query = "INSERT IGNORE INTO "+ insertAt +"(Name,Location,Time) VALUES(?,?,?)";
preparedStmt.setString(1,"xName");
preparedStmt.setString(2,"xLocation");
preparedStmt.setTimeStamp(3,)
preparedStmt = conn.execute(query);
result = true;
preparedStmt = null;
break;
case "B":
insertAt = "B #";
String query = "INSERT IGNORE INTO "+ insertAt +"(Name,Location,Time,Address) VALUES(?,?,?,?)";
preparedStmt.setString(1,"yName");
preparedStmt.setString(2,"yLocation");
preparedStmt.setTimeStamp(3,sysTime);
preparedStmt.setString(4,"yAddress")
preparedStmt = conn.execute(query);
preparedStmt = null;
result = true;
break;
default:
break;
}
return result;
}
public static void main(String[] args){
String[] tableNames = {"A #"," b"};
if(new DBS().insertAt(tableNames[1]) && new DBS().insertAt(tableNames[0])){
System.out.println("insertion success");
}else{
System.out.println("insertion failed");
}
}
}
你不能这样使用PreparedStatements
首先你必须创建语句:
preparedStmt = conn.prepareStatement(query);
然后执行它(设置参数后):
preparedStmt.executeUpdate ();
例如:
String query = "INSERT IGNORE INTO "+ insertAt +"(Name,Location,Time) VALUES(?,?,?)";
preparedStmt = conn.prepareStatement(query);
preparedStmt.setString(1,"xName");
preparedStmt.setString(2,"xLocation");
preparedStmt.setTimeStamp(3,...)
preparedStmt.executeUpdate ();