纯jsp实现简单的个人博客

该博客详细介绍了如何使用JSP来构建一个简单的个人博客系统,包括数据库连接、用户和博客的bean类、DAO接口及其实现、UserService接口及其实现、以及处理用户注册、登录、编辑资料和发布文章的方法。通过这个系统,用户可以进行个人信息管理和文章管理。
摘要由CSDN通过智能技术生成
1、创建数据库连接
package util;
import java.sql.Connection;
import java.sql.DriverManager;
public class jdbc {
 public static Connection getConn() throws Exception
 {
  String url="jdbc:mysql://127.0.0.1:3306/user";
  String username="root";
  String password="123456";
  Class.forName("com.mysql.jdbc.Driver");       //加载数据库驱动
  return DriverManager.getConnection(url,username,password);
 }
}

2.博客的bean
package bean;
public class Boke {
 private int id;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 private String title;
 private String content;
 public String getTitle() {
  return title;
 }
 public void setTitle(String title) {
  this.title = title;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
}

3.用户的bean
package bean;
public class User {
 private int id;
 private String username;
 private String password;
 private String shiming;
 private String hangye;
 private String zhiwei;
 private String xingbie;
 private String shengri;
 private String diqu;
 private String jianshu;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getShiming() {
  return shiming;
 }
 public void setShiming(String shiming) {
  this.shiming = shiming;
 }
 public String getHangye() {
  return hangye;
 }
 public void setHangye(String hangye) {
  this.hangye = hangye;
 }
 public String getZhiwei() {
  return zhiwei;
 }
 public void setZhiwei(String zhiwei) {
  this.zhiwei = zhiwei;
 }
 public String getXingbie() {
  return xingbie;
 }
 public void setXingbie(String xingbie) {
  this.xingbie = xingbie;
 }
 public String getShengri() {
  return shengri;
 }
 public void setShengri(String shengri) {
  this.shengri = shengri;
 }
 public String getDiqu() {
  return diqu;
 }
 public void setDiqu(String diqu) {
  this.diqu = diqu;
 }
 public String getJianshu() {
  return jianshu;
 }
 public void setJianshu(String jianshu) {
  this.jianshu = jianshu;
 }
 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;
 }
 @Override
 public String toString() {
  return "User [username=" + username + ", password=" + password + "]";
 }
}

4.Userdao接口
package dao;
import java.util.List;
import bean.User;
public interface Userdao {
 public boolean insert(User user);
 public User query(String username);
 public int query2(User user);   //0登陆成功,1用户不存在,2密码错误,3获取异常;
}

5.Userdaoimpl实现Userdao
package dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import bean.User;
import dao.Userdao;
import util.jdbc;
public class Userdaoimpl implements Userdao{
 
 private Connection conn=null;
 private PreparedStatement pst = null;
 private ResultSet rs = null;
 
 public boolean insert(User user) {
  
  try {
   conn=jdbc.getConn();
   String sql="insert into user(username, password) values (?, ?)";
   pst=conn.prepareStatement(sql);
   pst.setString(1, user.getUsername());
   pst.setString(2, user.getPassword());
   int i=pst.executeUpdate();
   if(pst!=null)
    pst.close();
   if(conn!=null)
    conn.close();
   if(i==1)
    return true;
   else
    return false;
  } catch (Exception e) {
   e.printStackTrace();
  }
  return false;
 }
 public User query(String username) {
  User user=new User();
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    user.setUsername(rs.getString("username"));
    user.setPassword(rs.getString("password"));
   }
   if(rs!=null)
    rs.close();
   if(pst!=null)
    pst.close();
   if(conn!=null)
    conn.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  return user;
 }
 public int query2(User user) {
  int i = 3;
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, user.getUsername());
   System.out.println(user.getUsername());
   rs=pst.executeQuery();
   if(rs.next())
   {
    if(!rs.getString("password").equals(user.getPassword()))
     i= 2;
    else
     i= 0;
   }
   else
   {
    i= 1;
   }
   if(rs!=null)
    rs.close();
   if(pst!=null)
    pst.close();
   if(conn!=null)
    conn.close();
  }catch (Exception e) {
   e.printStackTrace();
  }
  return i;
 }
}

6.Userservice接口
package service;
import bean.User;
public interface Userservice {
 public boolean insert(User user);
 public User query(String username);
 public int query2(User user);
}

7.Userserviceimpl实现Userservice接口
package service.impl;
import bean.User;
import dao.Userdao;
import dao.impl.Userdaoimpl;
import service.Userservice;
public class Userserviceimpl implements Userservice{
 private Userdao userdao;
 
 public Userserviceimpl()
 {
  userdao=new Userdaoimpl();
 }
 public boolean insert(User user) {
  
  return userdao.insert(user);
 }
 public User query(String username) {
  // TODO Auto-generated method stub
  return userdao.query(username);
 }
 public int query2(User user) {
  // TODO Auto-generated method stub
  return userdao.query2(user);
 }
 
}

8.方法类,嫌麻烦没写在Userdao里
package moth;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JOptionPane;
import bean.Boke;
import bean.User;
import util.jdbc;
public class method
{
 private Connection conn=null;
 private PreparedStatement pst = null;
 private ResultSet rs = null;
 public String hangye(String username) throws Exception
 {
  conn=jdbc.getConn();
  String sql="select * from user where username=?";
  pst=conn.prepareStatement(sql);
  pst.setString(1, username);
  rs=pst.executeQuery();
  if(rs.next())
  {
   String hangye=rs.getString("hangye");
   if(hangye==null||hangye.equals(""))
    return "未填写行业";
   else
    return rs.getString("hangye");
  }
  else
  {
   return "获取数据异常";
  }
 }
 public String zhiwei(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String zhiwei=rs.getString("zhiwei");
    if(zhiwei==null||zhiwei.equals(""))
     return "未填写职位";
    else
     return rs.getString("zhiwei");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "获取数据异常";
 }
 public String shiming(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String shiming=rs.getString("shiming");
    if(shiming==null||shiming.equals(""))
     return "未填写姓名";
    else
     return rs.getString("shiming");
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return "获取数据异常";
 }
 public String xingbie(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String xingbie=rs.getString("xingbie");
    if(xingbie==null||xingbie.equals(""))
     return "未填写性别";
    else
     return rs.getString("xingbie");
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return "获取数据异常";
 }
 public String shengri(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String shengri=rs.getString("shengri");
    if(shengri==null||shengri.equals(""))
     return "未填写生日";
    else
     return rs.getString("shengri");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "获取数据异常";
 }
 public String jianshu(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String jianshu=rs.getString("jianshu");
    if(jianshu==null||jianshu.equals(""))
     return "未填写简述";
    else
     return rs.getString("jianshu");
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
  return "获取数据异常";
 }
 public String diqu(String username)
 {
  try {
   conn=jdbc.getConn();
   String sql="select * from user where username=?";
   pst=conn.prepareStatement(sql);
   pst.setString(1, username);
   rs=pst.executeQuery();
   if(rs.next())
   {
    String diqu=rs.getString("diqu");
    if(diqu==null||diqu.equals(""))
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值