2.15 javabean

Javabean

复制代码
package bean;

public class Base_InformationBean {
    private String code;
    private String password;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
复制代码

数据库操作:

复制代码
package bean;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil //用于连接数据库
{
    String name="root";
    String password="123456";
    public Connection getConnection() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            return DriverManager.getConnection("jdbc:mysql://localhost:3306/test",name,password);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    public void closeConnection(Connection conn) {

        if(conn!=null)
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

}
复制代码
复制代码
package bean;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class InfoDAO {
    public boolean Login(Base_InformationBean baseInformationBean) //登录
    {
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="select * from hhh where code=? and password=?";
        try {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,baseInformationBean.getCode());
            pstm.setString(2,baseInformationBean.getPassword());
            ResultSet rs=pstm.executeQuery();
            if(rs.next())
            {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
    public boolean updateCode(Base_InformationBean baseInformationBean) //修改密码
    {
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="update hhh set password=? where code=?";
        try {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,baseInformationBean.getPassword());
            pstm.setString(2,baseInformationBean.getCode());
            pstm.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
    public boolean findCode(Base_InformationBean baseInformationBean) //查找账号
    {
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="select * from hhh where code=?";
        try {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,baseInformationBean.getCode());
            ResultSet rs=pstm.executeQuery();
            if(rs.next())
            {
                return true;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return false;
    }
    public void register(Base_InformationBean baseInformationBean)//注册
    {
        DBUtil db=new DBUtil();
        Connection conn=db.getConnection();
        String sql="insert into hhh(code,password) values(?,?)";
        try
        {
            PreparedStatement pstm=conn.prepareStatement(sql);
            pstm.setString(1,baseInformationBean.getCode());
            pstm.setString(2,baseInformationBean.getPassword());
            pstm.executeUpdate();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
复制代码

 

posted @ 2024-02-28 14:09  new菜鸟  阅读(11)  评论(0)    收藏  举报