本文目录一览:
- 1、java链接mysql数据库实现登陆如何验证?
- 2、用Java编写了一个登录类,现在我想用MYSQL数据库的信息来验证登录该怎么做?
- 3、使用Mysql数据库验证用户登录怎么取到的集合竟然不是null
java链接mysql数据库实现登陆如何验证?
//这是我以前写的核对数据库实现登陆的方法,你只看jdbc部分就好,我还特地给你加了点注释\x0d\x0aString sql = "select username,password from account";\x0d\x0aString user = request.getParameter("user");\x0d\x0aString pass = request.getParameter("password");\x0d\x0aint j = 0;\x0d\x0aConnection conn = null;\x0d\x0aPreparedStatement ps = null;\x0d\x0aResultSet rs = null;\x0d\x0atry {\x0d\x0aconn = JDBCTools1.getConnection();\x0d\x0aps = conn.prepareStatement(sql);\x0d\x0ars = ps.executeQuery();\x0d\x0a//从表中查询获取所有账户的用户名密码的ResultSet 对象\x0d\x0awhile(rs.next()){\x0d\x0aint i = 0;\x0d\x0a\x0d\x0aString username[] = new String[10];//用户名数组\x0d\x0aString password[] = new String[10];//密码数组\x0d\x0ausername[i] = rs.getString(1);\x0d\x0apassword[i] = rs.getString(2);\x0d\x0aif(user.equals(username[i])pass.equals(password[i])){//比对\x0d\x0aresponse.getWriter().print("you are welcome!");\x0d\x0aj++;\x0d\x0a}else if(user.equals(username[i])!pass.equals(password[i])){\x0d\x0aresponse.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n");\x0d\x0aresponse.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right");\x0d\x0aj++;\x0d\x0a}else{\x0d\x0acontinue;\x0d\x0a}\x0d\x0ai++;\x0d\x0a}\x0d\x0aif(j == 0){\x0d\x0aresponse.getWriter().println("Your username may not be properly");\x0d\x0a}\x0d\x0a} catch (Exception e) {\x0d\x0ae.printStackTrace();\x0d\x0a}finally{\x0d\x0aJDBCTools1.release(rs, ps, conn);\x0d\x0a}\x0d\x0a//这是我JDBCTools的getConnection方法\x0d\x0agetConnection{\x0d\x0aString driverClass = oracle.jdbc.driver.OracleDriver;\x0d\x0aString jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl;\x0d\x0a//你的数据库的用户名密码\x0d\x0aString user = null;\x0d\x0aString password = null;\x0d\x0a// 通过反射创建Driver对象\x0d\x0aClass.forName(driverClass);\x0d\x0areturn DriverManager.getConnection(jdbcUrl, user, password);}\x0d\x0a//这是我JDBCTools的release方法\x0d\x0apublic static void release(ResultSet rs, Statement statement,\x0d\x0aConnection conn) {\x0d\x0aif (rs != null) {\x0d\x0atry {\x0d\x0ars.close();\x0d\x0a} catch (SQLException e) {\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aif (statement != null) {\x0d\x0atry {\x0d\x0astatement.close();\x0d\x0a} catch (Exception e2) {\x0d\x0ae2.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a\x0d\x0aif (conn != null) {\x0d\x0atry {\x0d\x0aconn.close();\x0d\x0a} catch (Exception e2) {\x0d\x0ae2.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}
用Java编写了一个登录类,现在我想用MYSQL数据库的信息来验证登录该怎么做?
添加msyql的jdbc驱动,然后使用JDBC连接MYSQL,不算很难。。。。在知道上面搜索 jdbc mysql就有不少例子
使用Mysql数据库验证用户登录怎么取到的集合竟然不是null
你的变量users2是一个list集合,如果没有用户,list里面为空,但是list这个对象存在,所以是不为null的,你判断users2不应该这样判断,你应该判断users2的长度是否为0?