嗨,我想在MySQL数据库中插入值并在下面附上我的编码,我收到错误。
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(DB_URL,"root","root");
System.out.println("Remote DB connection established");
PreparedStatement statement=con.prepareStatement("INSERT INTO TBL_MONTHLY_EXPENSES_DETAILS"+
(AVG_COST, RWDS_INCENT,OTH_EXPENSES,TRAVELLING_EXPENSES,CLIENT_VISITS,REV_RECD)VALUES
(AVG_COST, RWDS_INCENT,OTH_EXPENSES,TRAVELLING_EXPENSES,CLIENT_VISITS,REV_RECD);
}catch(Exception e){
System.out.println("Remote DataBase connection establishment error.");
e.printStackTrace();
pn.setProjectName("Failed");
System.out.println(e.getMessage().toString());
}
答案 0 :(得分:1)
下面的更正 - 您缺少一些引号,并且您没有正确参数化。
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(DB_URL,"root","root");
System.out.println("Remote DB connection established");
PreparedStatement statement=con.prepareStatement("INSERT INTO TBL_MONTHLY_EXPENSES_DETAILS"+
"(AVG_COST, RWDS_INCENT,OTH_EXPENSES,TRAVELLING_EXPENSES,CLIENT_VISITS,REV_RECD)VALUES"+
"(?,?,?,?,?,?)");
}catch(Exception e)
{
System.out.println("Remote DataBase connection establishment error.");
e.printStackTrace();
pn.setProjectName("Failed");
System.out.println(e.getMessage().toString());
}
不要忘记使用statement.setX()函数来设置每个的值?在VALUES声明中。
答案 1 :(得分:0)
int AVG_COST, RWDS_INCENT, OTH_EXPENSES, TRAVELLING_EXPENSES,CLIENT_VISITS, REV_RECD;
PreparedStatement statement=conn.prepareStatement("INSERT INTO TBL_MONTHLY_EXPENSES_DETAILS" +
"(AVG_COST, RWDS_INCENT,OTH_EXPENSES,TRAVELLING_EXPENSES,CLIENT_VISITS,REV_RECD)" +
"VALUES(?,?,?,?,?,?)");
statement.setInt(1, AVG_COST);
statement.setInt(2, RWDS_INCENT);
statement.setInt(3, OTH_EXPENSES);
statement.setInt(4, TRAVELLING_EXPENSES);
statement.setInt(5, CLIENT_VISITS);
statement.setInt(6, REV_RECD);
statement.executeUpdate();