【问题标题】:How to match field Value in from database in jsp?如何在jsp中匹配数据库中的字段值?
【发布时间】:2014-01-18 07:00:12
【问题描述】:

我在 jsp 中注册了两个字段用户名和密码,我将这些字段插入数据库。在插入之前我想检查用户名是否已经存在然后不要将其插入数据库并打印任何有意义的完整消息。

这是我的代码:

String user = request.getParameter("user");
   String pass = request.getParameter("pass");
try{
Class.forName("com.mysql.jdbc.Driver");
        Connection  cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Court","root","Dan@1234");
 PreparedStatement ps=cn.prepareStatement("insert into Councel  values(?,?)");
ps.setString(1,user);
                        ps.setString(2,pass);
 int i=ps.executeUpdate();
                        if(i==1)

}
catch(Exception e)
                    {

                        out.println(e.toString());
                    }

【问题讨论】:

    标签: jsp jdbc


    【解决方案1】:
    String user = request.getParameter("user");
    String pass = request.getParameter("pass");
    try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection  cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/Court","root","Dan@1234");
    PreparedStatement ps0=cn.prepareStatement("Select uname from Councel where uname=?");
    ps0.setString(1,user);
    ResultSet rs=ps0.executeQuery();
    if(rs.next())
    {
        //User already exists
    }
    else{
        PreparedStatement ps=cn.prepareStatement("insert into Councel  values(?,?)");
        ps.setString(1,user);
        ps.setString(2,pass);
        int i=ps.executeUpdate();
        if(i==1){}
    }
    
    
    }
    catch(Exception e)
    {
    out.println(e.toString());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-14
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      相关资源
      最近更新 更多