1.简易论坛制作
基本功能:登录,注册,修改登录用户信息,管理员修改用户,回帖,发帖。
下面上图:
点击注册用户的界面如下,验证均已写好(输入格式不正确,不能提交哦)
附上一张代码量截图:
采用典型的MVC模式编写,采用俩张mysql数据库表单,一张用于登录,一张用于存储发帖回帖信息。
数据库导入
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Dbcon {
public Connection getcon(){
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/luntan";
String user="root";
String pwd="mysql";
conn=DriverManager.getConnection(url,user,pwd);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void closeconn(){
Connection con=getcon();
try {
if(!con.isClosed()){
con.close();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
// Dbcon db = new Dbcon();
// db.getcon();
// db.closeconn();
}
}
将相关功能代码现在java文件中编写,主方法中测试,测试成功后在servlet中引用,在动态网页jsp中显示,用户登录注册代码如下:
package it.com.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import sun.security.timestamp.TSRequest;
import it.com.com.Dbcon;
public class UserDao {
public List<Map> findAll(){
Dbcon db = new Dbcon();
Connection con=db.getcon();
String sql ="SELECT * FROM users";
List<Map> list = new ArrayList<Map>();
try {
PreparedStatement pst=con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while(rs.next()){
Map map = new HashMap();
map.put("userName", rs.getString("userName"));
map.put("userPwd", rs.getString("userPwd"));
map.put("Ename", rs.getString("Ename"));
map.put("Email", rs.getString("Email"));
map.put("Logo", rs.getString("Logo"));
list.add(map);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public void resiger(String userName,String userPwd,String Ename,String Email,String Logo){
Dbcon db = new Dbcon();
Connection con=db.getcon();
String sql="INSERT INTO users (userName,userPwd,Ename,Email,Logo) VALUES (?,?,?,?,?)";
try {
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1,userName);
pst.setString(2,userPwd);
pst.setString(3,Ename);
pst.setString(4,Email);
pst.setString(5,Logo);
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(String userName,String userPwd,String Ename,String Email,String Logo){
Dbcon db = new Dbcon();
Connection con=db.getcon();
String sql="UPDATE users SET userPwd=?,Ename=?,Email=?,Logo=? WHERE userName=? ";
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1, userPwd);
pst.setString(2, Ename);
pst.setString(3, Email);
pst.setString(4, Logo);
pst.setString(5, userName);
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int pagesize(){
List<Map> list = findAll();
int pagesize=list.size();
int n=0;
if(pagesize%5==0){
n=pagesize/5;
}else{
n=pagesize/5+1;
}
return n;
}
public int deleteByName(String userName){
Dbcon db = new Dbcon();
Connection con=db.getcon();
String sql="DELETE FROM users WHERE userName=?";
int n=0;
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1, userName);
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
n=1;
}
return n;
}
public int login1(String username,String userpwd){
Dbcon db = new Dbcon();
Connection con=db.getcon();
String sql ="SELECT * FROM users WHERE userName=?";
int n=0;
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1, username);
ResultSet rs = pst.executeQuery();
while(rs.next()){
n=1;//如果找到则执行
if(rs.getString("userPwd").equals(userpwd)){
n=2;//密码正确
}else{
n=3;//密码不正确
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return n;
}
public List<Map> select(String userName){
Dbcon db = new Dbcon();
Connection con=db.getcon();
List<Map> list =new ArrayList<Map>();
String sql="select * from users where userName=?";
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1, userName);
ResultSet rs = pst.executeQuery();
if(rs.next()){
Map map = new HashMap();
map.put("userName", rs.getString("userName"));
map.put("userPwd", rs.getString("userPwd"));
map.put("Ename", rs.getString("Ename"));
map.put("Email", rs.getString("Email"));
map.put("Logo", rs.getString("Logo"));
list.add(map);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDao us = new UserDao();
// List<Map> list=us.findAll();
// for(Map map:list){
// System.out.println(map);
// }
//us.update("x1", "xxx", "xxx", "xxx", "xxx");
//us.deleteByName("rt");
//us.resiger("asda", "sdad", "asda", "sdasd", "sdafdd", 221);
// List<Map>list1= us.select("rgt");
// for(Map map:list1){
// System.out.println(map);
// }
//
//us.resiger("x10", "xxx", "lala", "waa", "12");
}
}