检查mysql中的用户名...如果存在,则插入另一个表,否则为基表

时间:2013-05-17 04:23:24

标签: java mysql database jdbc mysql-workbench

我在MySQL工作台中有下表Table1 -

user_id       int            not null,autoincrement
movie _id     int            not null 
movie_name    varchar        
user_name     varchar
rating        int
genre         varchar

以下是插入数据库 -

try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:XXXX/Recommendation1", "root",
                    "XXXXXXXXXXX");
            Statement st = con.createStatement();
            int i = st
                    .executeUpdate("insert into Table1(movie_Id,movie_name,user_name,rating,genre) values('"
                            + movieId
                            + "','"
                            + mname
                            + "','"
                            + pname
                            + "','" + ratingr + "','" + genre + "')");
            out.println("Data is successfully inserted!");
        } catch (Exception e) {
            System.out.print(e);

现在我想要的是,如果已存在带有该值的用户名(pnmae),我输入它不应该进入我的表1或更新,但它应该进入另一个表表2,模式表2如下 -

movie _id     int            not null 
movie_name    varchar        
user_name     varchar
genre         varchar

对于那个用什么。如何使用if -else condition

以下是我在检查用户名时正在进行的操作

Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:XXXX/Recommendation1", "root",
                    "wXXXXX");
            Statement st = con.createStatement();
            //Statement st2 = con.createStatement();
            String SQL = "SELECT * from Table1 WHERE user_name ='" + pname
                    + "' ";
            ResultSet rs = st.executeQuery(SQL);

            --if (rs.) {   //What to put here

                int j = st
                        .executeUpdate("insert into Table2(movie_Id,movie_name,user_name,genre) values('"
                                + movieId
                                + "','"
                                + mname
                                + "','"
                                + pname
                                + "','" + genre + "') ");

            }

            int i = st
                    .executeUpdate("insert into Table1(movie_Id,movie_name,user_name,rating,genre) values('"
                            + movieId
                            + "','"
                            + mname
                            + "','"
                            + pname
                            + "','" + ratingr + "','" + genre + "') ");

            /*  ResultSet rs = st1.executeQuery();
                out.println("name is already there");
                out.println("Data is successfully inserted!"); */

        /*  final String Query = "SELECT t1.user_Id from Table1 t1 JOIN Table2 t2 on t2.movie_Id = t1.movie_id WHERE t2.user_name = 'vishal'";

            PreparedStatement st11 = con.prepareStatement(Query);
            ResultSet rw = st11.executeQuery();
            rw.last();

            int id = rw.getInt("user_Id");

            System.out.print("ID: " + id);*/

由于

1 个答案:

答案 0 :(得分:0)

试试这个: -

        boolean isUser=false;
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(
                "jdbc:mysql://localhost:XXXX/Recommendation1", "root",
                "wXXXXX");
        Statement st = con.createStatement();           
        String SQL = "SELECT 1 from Table1 WHERE user_name ='"+ pname
                + "' ";
        ResultSet rs = st.executeQuery(SQL);
        isUser=rs.next();
        Statement st2 = con.createStatement();
        if (isUser) {   //check the user is exist or not

            int j = st2.executeUpdate("insert into Table2(movie_Id,movie_name,user_name,genre) values('"
                            + movieId
                            + "','"
                            + mname
                            + "','"
                            + pname
                            + "','" + genre + "') ");

     }else{

     int i = st2.executeUpdate("insert into Table1(movie_Id,movie_name,user_name,rating,genre) values('"
                        + movieId
                        + "','"
                        + mname
                        + "','"
                        + pname
                        + "','" + ratingr + "','" + genre + "') ");

       }

希望这会对你有所帮助。