-1

嗨,我想在 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());
}
4

2 回答 2

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 语句中。

于 2013-09-30T17:56:52.630 回答
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();
于 2013-09-30T18:01:51.077 回答