1、数据库:sql server2012
2、框架:struts2,其他的都是原始的java代码
3、前端是bootstrap框架
用户的信息类
User.java
package cn.com.bean;
public class User {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(int id, String username, String password) {
super();
this.id = id;
this.username = username;
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + "]";
}
public User() {
super();
}
}
对数据库的增删改查的封装类
package cn.com.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.com.bean.User;
public class ConnectJdbc {
public static Connection ct = null;
public static PreparedStatement ps = null;
public static ResultSet rs = null;
//连接数据库
public static Connection getconn(){
// 1.加载驱动
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
ct = DriverManager.getConnection(
"jdbc:sqlserver://192.168.1.152:1433;databaseName = lay",
"sa", "123456");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ct;
}
// 增改删
public static void update(String sql, String[] sz) {
try {
ct=getconn();
// 3.发送sql语句
ps = ct.prepareStatement(sql);
for (int i = 0; i < sz.length; i++) {
ps.setString(i + 1, sz[i]);
}
// 4.执行
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally {
try {
ps.close();
ct.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//查询
public static List select(String sql, String[] sz) {
List list=new ArrayList();
try {
ct=getconn();
// 3.发送sql语句
ps = ct.prepareStatement(sql);
for (int i = 0; i < sz.length; i++) {
ps.setString(i + 1, sz[i]);
}
// 4.执行
rs=ps.executeQuery();
while (rs.next()) {
User u=new User(rs.getInt(1), rs.getString(2), rs.getString(3));
list.add(u);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
} finally {
try {
rs.close();
ps.close();
ct.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return list;
}
}
登录注册的业务层
package cn.com.serice;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ModelDriven;
import cn.com.bean.User;
import cn.com.jdbc.ConnectJdbc;
public class Login implements ModelDriven {
private User user = new User();
public String login() {
ConnectJdbc conn = new ConnectJdbc();
String sql = "select * from [user] where username=? and password=?";
System.out.println(user.getUsername() + ":" + user.getPassword());
String[] sz = { user.getUsername(), user.getPassword() };
List list = conn.select(sql, sz);
String tom = "";
if (list.size() > 0) {
tom = "success";
} else {
tom = "error";
}
// ajax实现从注册到登录
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("utf-8");
try {
response.getWriter().write(tom);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
// 用户注册
public String regedit() {
ConnectJdbc conn = new ConnectJdbc();
String sql = "insert into [user](username,password) values(?,?)";
System.out.println(user.getUsername() + ":" + user.getPassword());
String[] sz = { user.getUsername(), user.getPassword() };
conn.update(sql, sz);
return null;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
struts.xml文件
/index.jsp
完整的代码在我的资源下载页
地址:https://download.csdn.net/download/qq_37591637/11251677