核对数据库
//这是我以前写的核对数据库实现登陆的方法,你只看jdbc部分就好,我还特地给你加了点注释x0dx0aString sql = "select username,password from account";x0dx0aString user = request.getParameter("user");x0dx0aString pass = request.getParameter("password");x0dx0aint j = 0;x0dx0aConnection conn = null;x0dx0aPreparedStatement ps = null;x0dx0aResultSet rs = null;x0dx0atry {x0dx0aconn = JDBCTools1.getConnection();x0dx0aps = conn.prepareStatement(sql);x0dx0ars = ps.executeQuery();x0dx0a//从表中查询获取所有账户的用户名&密码的ResultSet 对象x0dx0awhile(rs.next()){x0dx0aint i = 0;x0dx0ax0dx0aString username[] = new String[10];//用户名数组x0dx0aString password[] = new String[10];//密码数组x0dx0ausername[i] = rs.getString(1);x0dx0apassword[i] = rs.getString(2);x0dx0aif(user.equals(username[i])&&pass.equals(password[i])){//比对x0dx0aresponse.getWriter().print("you are welcome!");x0dx0aj++;x0dx0a}else if(user.equals(username[i])&&!pass.equals(password[i])){x0dx0aresponse.getWriter().println("the realy password is :"+ username[i] +","+password[i]+"\r\n");x0dx0aresponse.getWriter().println("and you password is :"+user +","+pass+" :so the username or password may not right");x0dx0aj++;x0dx0a}else{x0dx0acontinue;x0dx0a}x0dx0ai++;x0dx0a}x0dx0aif(j == 0){x0dx0aresponse.getWriter().println("Your username may not be properly");x0dx0a}x0dx0a} catch (Exception e) {x0dx0ae.printStackTrace();x0dx0a}finally{x0dx0aJDBCTools1.release(rs, ps, conn);x0dx0a}x0dx0a//这是我JDBCTools的getConnection方法x0dx0agetConnection{x0dx0aString driverClass = oracle.jdbc.driver.OracleDriver;x0dx0aString jdbcUrl = jdbc:oracle:thin:@localhost:1521:orcl;x0dx0a//你的数据库的用户名密码x0dx0aString user = null;x0dx0aString password = null;x0dx0a// 通过反射创建Driver对象x0dx0aClass.forName(driverClass);x0dx0areturn DriverManager.getConnection(jdbcUrl, user, password);}x0dx0a//这是我JDBCTools的release方法x0dx0apublic static void release(ResultSet rs, Statement statement,x0dx0aConnection conn) {x0dx0aif (rs != null) {x0dx0atry {x0dx0ars.close();x0dx0a} catch (SQLException e) {x0dx0ae.printStackTrace();x0dx0a}x0dx0a}x0dx0ax0dx0aif (statement != null) {x0dx0atry {x0dx0astatement.close();x0dx0a} catch (Exception e2) {x0dx0ae2.printStackTrace();x0dx0a}x0dx0a}x0dx0ax0dx0aif (conn != null) {x0dx0atry {x0dx0aconn.close();x0dx0a} catch (Exception e2) {x0dx0ae2.printStackTrace();x0dx0a}x0dx0a}x0dx0a}