jsp+javabean+servlet+mysql编写的登录程序,急用,谢谢 5
展开全部
javabpackage login;
public class Databean {
String username;
String qx;
public String getQx() {
return qx;
}
public void setQx(String qx) {
this.qx = qx;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public boolean isBoo() {
return boo;
}
public void setBoo(boolean boo) {
this.boo = boo;
}
String pwd;
boolean boo;
}
package login;
/* 数据访问组件 */
import java.sql.*;
public class DbPool{
//取得连接
public Connection getConnection() {
Connection con;
try
{
Class.forName("com.mysql.jdbc.Driver");
String uri= "jdbc:mysql://localhost/Login";
con=DriverManager.getConnection(uri,"root","");
return con;
}
catch(Exception e)
{
System.out.println(e);
return null;
}
}
//释放资源
}
servlet 如下:::
import java.sql.*;
import java.util.*;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;
public class LoginHandler {
public LoginHandler() {
}
Connection conn;
PreparedStatement ps;
ResultSet rs;
//检查是否已注册
public int checkLogin(String name,String password){
//从数据访问组件dbPool中取得连接
DbPool db=new DbPool();
conn = db.getConnection();
try {
String sql = "select * from t_userInfo where username=? and password=?";
ps = conn.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,password);
rs = ps.executeQuery();
if(rs.next())
{
String s=rs.getString(3);
if(s.equals("com"))
return 1;
else
conn.close();
return 2;
//释放资源
}
else {
conn.close();
return 0;
}
} catch (SQLException e) {
System.out.println(e);
}
return 0;
}
public boolean checkLoginregister(String username,String pwd){
DbPool db=new DbPool();
conn = db.getConnection();
int n=0;
try{
String sql = "insert into t_userInfo values(?,?,?) ";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,pwd);
ps.setString(3, "com");
n=ps.executeUpdate();
}catch(Exception e){
System.out.println(e);
}
if(n!=0)
{
return true;
}
else
{
return false;
}
}
}
jsp:
<%@page contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form method="post" action="loginservlet" >
用户名: <input type="text" name="username" size="15"><br><br>
密码: <input type="password" name="password" size="15"><br><br>
<input type="submit" name="submit" value="登录"><br>
</form>
</body>
</html>
public class Databean {
String username;
String qx;
public String getQx() {
return qx;
}
public void setQx(String qx) {
this.qx = qx;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public boolean isBoo() {
return boo;
}
public void setBoo(boolean boo) {
this.boo = boo;
}
String pwd;
boolean boo;
}
package login;
/* 数据访问组件 */
import java.sql.*;
public class DbPool{
//取得连接
public Connection getConnection() {
Connection con;
try
{
Class.forName("com.mysql.jdbc.Driver");
String uri= "jdbc:mysql://localhost/Login";
con=DriverManager.getConnection(uri,"root","");
return con;
}
catch(Exception e)
{
System.out.println(e);
return null;
}
}
//释放资源
}
servlet 如下:::
import java.sql.*;
import java.util.*;
import javax.servlet.http.HttpSession;
import javax.xml.ws.Response;
public class LoginHandler {
public LoginHandler() {
}
Connection conn;
PreparedStatement ps;
ResultSet rs;
//检查是否已注册
public int checkLogin(String name,String password){
//从数据访问组件dbPool中取得连接
DbPool db=new DbPool();
conn = db.getConnection();
try {
String sql = "select * from t_userInfo where username=? and password=?";
ps = conn.prepareStatement(sql);
ps.setString(1,name);
ps.setString(2,password);
rs = ps.executeQuery();
if(rs.next())
{
String s=rs.getString(3);
if(s.equals("com"))
return 1;
else
conn.close();
return 2;
//释放资源
}
else {
conn.close();
return 0;
}
} catch (SQLException e) {
System.out.println(e);
}
return 0;
}
public boolean checkLoginregister(String username,String pwd){
DbPool db=new DbPool();
conn = db.getConnection();
int n=0;
try{
String sql = "insert into t_userInfo values(?,?,?) ";
ps = conn.prepareStatement(sql);
ps.setString(1,username);
ps.setString(2,pwd);
ps.setString(3, "com");
n=ps.executeUpdate();
}catch(Exception e){
System.out.println(e);
}
if(n!=0)
{
return true;
}
else
{
return false;
}
}
}
jsp:
<%@page contentType="text/html; charset=gb2312" %>
<html>
<head>
<title>登录页面</title>
</head>
<body>
<form method="post" action="loginservlet" >
用户名: <input type="text" name="username" size="15"><br><br>
密码: <input type="password" name="password" size="15"><br><br>
<input type="submit" name="submit" value="登录"><br>
</form>
</body>
</html>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询