将数据插入数据库时​​出错

时间:2015-04-06 14:26:44

标签: sql

我试图将记录插入数据库,所以我创建了一个方法插入。问题是我一直收到这个错误

java.sql.SQLException: No value specified for parameter 7

这是我的代码

public void InsertDataInDB (Connection conn, Statement stmt, event MyNewEvent,String TableName){

   try {
       conn = DriverManager.getConnection(DB_URL, USER, PASS);
       stmt = conn.createStatement();

       //String Values = MyNewEvent.EventName+"', '"+MyNewEvent.Location+"', '"+MyNewEvent.Description+"', '"+MyNewEvent.Creator+"', '"+MyNewEvent.startDate+"', '"+MyNewEvent.endDate;
       //Values = "'sjsjs','sss',''";
       String sql = "INSERT INTO " + TableName+ " (EventName, Location, Description, Creater, StartDate, EndDate) VALUES (?, ?, ?, ?, ?, ?)" ;
       PreparedStatement statement = conn.prepareStatement(sql);

       statement.setString(1, MyNewEvent.EventName);
       statement.setString(2, MyNewEvent.Location);
       statement.setString(3, MyNewEvent.Description);
       statement.setString(4, MyNewEvent.Creator);
       statement.setString(5, MyNewEvent.startDate);
       statement.setString(6, MyNewEvent.endDate);
       statement.execute();
       System.out.println("Successfully inserted record into database");
   }
   catch(SQLException se){
           se.printStackTrace();

   }//end finally try


}

1 个答案:

答案 0 :(得分:0)

在main中的函数调用中,表名是错误的。谢谢你指出来。