在不使用准备好的语句的情况下插入ddbb

问题描述 投票:-2回答:1

首先,这是我第一次使用JDBC。我有一个称为insertQuery的方法和另一个称为updateQuery的方法,它们都接收一个String以便在称为Movies的数据库中更新或插入新电影。因此,基于用户将通过基本UI输入的一些值,我的代码将执行一个查询或另一个查询。问题是我在更新时遇到语法错误,在插入时出现错误23000,我不知道为什么

try {
  ResultSet rs = conn.selectQuery("SELECT title FROM movie WHERE title LIKE '" + title + "';");
  if (rs != null) {
    if (rs.next()) {
      System.out.println("Updating db...");
      conn.updateQuery("UPDATE movie SET title ='" + title 
                       + "', id_director = " + idDirector 
                       + ", year = " + year 
                       + ", duration = " + duration 
                       + ", country = '" + country 
                       + "', movie_facebook_likes = " + facebookLikes  
                       + ", imdb_score = "+ imdbScore 
                       + ", gross = " + gross 
                       + ", budget = " + budget 
                       + " WHERE title LIKE '" + title + "';");
    } else {
      System.out.println("Inserting into db...");
      conn.insertQuery("INSERT INTO movie (title, id_director, year, duration,
                       country, movie_facebook_likes, imdb_score, gross, budget) " +
                       "VALUES ('" + title + "', " + idDirector + "," + year + ", " 
                       + duration + ", " + country + ", " + facebookLikes + ", "
                       + ", " + imdbScore + ", " + gross + ", " + budget + ");");
    }
  }
} catch (SQLException e1) {
  JOptionPane.showMessageDialog(mainView, "MySQL error", "ERROR", .ERROR_MESSAGE);
}
java sql jdbc
1个回答
0
投票
This is how to i used to insert in db with no prepared statement




public int adduser(String id, String nm, String pwd,String cy)
    {
        boolean result=false;
        int i=0;
        try
        {
       Connection con=Util.getConnection();
       Statement stmt =con.createStatement();

            i =stmt.executeUpdate("INSERT INTO userdata (userid,name,password,city)\n" +
"VALUES ("+id+",'"+nm+"','"+pwd+"','"+cy+"')");


       }catch(Exception ex)
       {
           System.out.println(ex);
       }
        return i ; 
    }
© www.soinside.com 2019 - 2024. All rights reserved.