java怎么写登入代码( 三 )


5.怎么用JAVA写一个用户登入程序同意楼上的说法,具体点可以这样:创建一个用户表,里边包括LoginName(登录名),UserName(用户名),Password(密码),Age(年龄),Address(地址) 。
然后编写Java程序(用MVC架构)模型层(M):DBConnection.java(负责连接数据库)import java.sql.Connection;import java.sql.DriverManager;import java.sql.*;public class DBConnection { private static final String DRIVER_CLASS = "sun.jdbc.odbc.JdbcOdbcDriver"; private static final String DB_URL = "jdbc:odbc:text"; public DBConnection() { } public static Connection getConnection() { Connection conn = null; try { Class.forName(DRIVER_CLASS); conn = DriverManager.getConnection(DB_URL); } catch (SQLException ex) { System.out.println(ex.getMessage()); } catch ( ex) { System.out.println(ex.getMessage()); } return conn; }}第2个负责数据库查询操作的类:DBUserManager.javaimport edu.systop.text.model.entity.User;import edu.systop.text.model.dao.DBConnection;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.DriverManager;import java.sql.*;public class DBUserManager { private static final String SQL_SELECT = "SELECT LoginName,UserName,PassWord,Age,Address FROM UserInfo WHERE LoginName = ? AND PassWord = ?"; public DBUserManager() { } public boolean checkDB(User u) { boolean b = false; Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; conn = DBConnection.getConnection(); try { psmt = conn.prepareStatement(SQL_SELECT); psmt.setString(1, u.getLoginName()); psmt.setString(2, u.getPassWord()); rs = psmt.executeQuery(); b = rs.next(); if (rs.next()) { b = true; } } catch (SQLException ex) { System.out.println(ex.getMessage()); } finally { cleanDB(rs, psmt, conn); } return b; } public User checkBC(User u) { Connection conn = null; PreparedStatement psmt = null; ResultSet rs = null; User tmp = new User(); conn = DBConnection.getConnection(); try { psmt = conn.prepareStatement(SQL_SELECT); psmt.setString(1, u.getLoginName()); psmt.setString(2, u.getPassWord()); rs = psmt.executeQuery(); if (rs.next()) { tmp.setLoginName(rs.getString(1)); tmp.setUserName(rs.getString(2)); tmp.setAge(rs.getInt(4)); tmp.setAddress(rs.getString(5)); } } catch (SQLException ex) { System.out.println(ex.getMessage()); } finally { cleanDB(rs, psmt, conn); } return tmp; } public void cleanDB(ResultSet rs, PreparedStatement psmt, Connection conn) { try { if (rs != null) { rs.close(); } if (psmt != null) { psmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException ex) { System.out.println(ex.getMessage()); } }第3个实体用户类:User.javapackage edu.systop.text.model.entity;public class User { private String loginName; private String userName; private String passWord; private int age; private String address; public User() { } public void setLoginName(String loginName) { this.loginName = loginName; } public void setUserName(String userName) { this.userName = userName; } public void setPassWord(String passWord) { this.passWord = passWord; } public void setAge(int age) { this.age = age; } public void setAddress(String address) { this.address = address; } public String getLoginName() { return loginName; } public String getUserName() { return userName; } public String getPassWord() { return passWord; } public int getAge() { return age; } public String getAddress() { return address; }}然后编写控制层(C):GetInfoServlet.javapackage edu.systop.text.control;import javax.servlet.*;import javax.servlet.; charset=gbk"; //Initialize global variables public void init() throws ServletException { } //Process the HTTP Get request public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } //Process the HTTP Post request public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String loginName = request.getParameter("loginName"); String passWord = request.getParameter("passWord"); User u = new User(); u.setLoginName(loginName); u.setPassWord(pa 。