java 没有错误,但数据没有插入数据库

h43kikqp  于 10个月前  发布在  Java
关注(0)|答案(2)|浏览(149)

我有一个SQL数据库,我想插入数据。下面是我的代码。执行java程序后,数据不会插入数据库,也不会发生错误。

Connection conn=null;
    Statement stmt=null;
    Scanner sc=new Scanner (System.in);
    System.out.println("Enter Username");
    String username=sc.next();
    System.out.println("Enter Email");
    String email=sc.next();
    System.out.println("Enter Phone Number");
    int phno=sc.nextInt();
    System.out.println("Create a Password");
    String password=sc.next();
    
    
    try {
        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","abc");
        stmt=conn.createStatement();
        String query1="select * from login_details";
        String query2="insert into login_details(user_name,user_email,user_phone,user_password)values('"+username+"','"+email+"','"+phno+"','"+password+"')";
        ResultSet rs=stmt.executeQuery(query1);
        boolean status=false;
        
        while(rs.next()) {
            if(status=false) {
                stmt.executeUpdate(query2);
                System.out.println("Details Entered Successfully");
            }
        }
        
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
brjng4g3

brjng4g31#

代码中存在逻辑和安全问题。
你给条件中的变量给予,而不是检查它。您应该用途:

if (!status){...}

另一件事是,如果数据来自用户,你应该使用PreparedStatement而不是Statement。这是针对SQL注入攻击。

4nkexdtk

4nkexdtk2#

当我看这段代码时,它会插入更多的加倍条目,表中的条目越多。
我不知道,当循环表的内容并插入输入的值时,你想实现什么。
当我假设你想检查一个用户是否已经存在时,在插入它之前,检查用户是否存在是值得的。
我同意所有人使用准备好的声明。试试这样的方法:

Connection conn=null;
PreparedStatement reader=null;
PreparedStatement writer=null
ResultSet rs=null;

boolean userExists = false;
Scanner sc=new Scanner (System.in);
System.out.println("Enter Username");
String username=sc.next();
System.out.println("Enter Email");
String email=sc.next();
System.out.println("Enter Phone Number");
int phno=sc.nextInt();
System.out.println("Create a Password");
String password=sc.next();

try {
    conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sample","root","abc");
    reader=conn.prepareStatement("select count(*) NumberOfUsers from login_details where user_name = ?" );      
    reader.setString(1,username);
    rs = reder.executeQuery();
    if ( rs.next() ) {
       if ( rs.getInt(1) > 0 ) {
         userExists = true;
         System.out.println("Username already exists");
       }     
    }
    
    if ( ! userExists ) {
        writer = conn.prepareStatement("insert into login_details(user_name,user_email,user_phone,user_password)values(?,?,?,?)");
        writer.setString(1,username);
        writer.setString(2,email);
        writer.setInt(3,phno);  // is it really integer?
        writer.setString(4,password);
        
        writer.executeUpdate();
            
    }
    
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} finally {
    if ( reader ! = null ) {
       try {
          reader.close();
       } catch ( SQLException s ) {
          System.out.println("Error while closing reader " + s.getMessage());
       }
    }  
    if ( writer ! = null ) {
       try {
          writer.close();
       } catch ( SQLException s ) {
          System.out.println("Error while closing writer " + s.getMessage());
       }
    } 
    if ( rs ! = null ) {
       try {
          rs.close();
       } catch ( SQLException s ) {
          System.out.println("Error while closing ResultSet " + s.getMessage());
       }
    }       
    if ( conn ! = null ) {
       try {
          conn.close();
       } catch ( SQLException s ) {
          System.out.println("Error while closing Connection " + s.getMessage());
       }
    }
    

}

相关问题