1、创建数据库连接
package util;
import java.sql.Connection;
import java.sql.DriverManager;
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);
}
}
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;
}
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 + "]";
}
}
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获取异常;
}
public boolean insert(User user);
public User query(String username);
public int query2(User user); //0登陆成功,1用户不存在,2密码错误,3获取异常;
}
package dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
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;
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;
}
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;
}
}
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);
}
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;
import dao.Userdao;
import dao.impl.Userdaoimpl;
import service.Userservice;
public class Userserviceimpl implements Userservice{
private Userdao userdao;
public Userserviceimpl()
{
userdao=new Userdaoimpl();
}
public Userserviceimpl()
{
userdao=new Userdaoimpl();
}
public boolean insert(User user) {
return userdao.insert(user);
}
return userdao.insert(user);
}
public User query(String username) {
// TODO Auto-generated method stub
return userdao.query(username);
}
// TODO Auto-generated method stub
return userdao.query(username);
}
public int query2(User user) {
// TODO Auto-generated method stub
return userdao.query2(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 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(""))
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(""))