登录注册(数据库连接)

dao层:

连接数据库:

package com.dao;

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

public class DbUtil {
    private String dbDriver = "com.mysql.jdbc.Driver";
    private String dbUrl = "jdbc:mysql://localhost:3306/lx";
    private String dbUser = "root";
    private String dbPw = "root";
    private Connection conn = null;

    public Connection getConnection(){
        try {
            //注册驱动
            Class.forName(dbDriver);
            //获取数据库连接对象
            conn = DriverManager.getConnection(dbUrl, dbUser, dbPw);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
        return conn;

    }
    public static void main(String[] args) {
        DbUtil db = new DbUtil();
        Connection con=db.getConnection();
        if(con!=null){
            System.out.println("数据库连接成功");
        }
    }
}

对数据库进行操作

package com.dao;

import com.model.Human;

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

public class HumanDao {
    //插入
    public void insertInfo(Connection con, Human h) throws SQLException {
        String sql = "insert into people(name,age) values('"+h.getName()+"','"+h.getAge()+"')";
        PreparedStatement psmt = con.prepareStatement(sql);
        psmt.executeUpdate(sql);
        psmt.close();
    }
    //查找
    public boolean find(Connection con,String name,int age) throws SQLException{
        boolean b=false;
        Human h = new Human();
        String sql = "select * from people where name = '"+name+"' and age='"+age+"'";
        PreparedStatement psmt = con.prepareStatement(sql);
        ResultSet rs = psmt.executeQuery(sql);
//        while(rs.next()){
//            h.setName(rs.getString("name"));
//            h.setAge(rs.getInt("age"));
//        }
        if (rs.next()){
            b=true;
        }
        return b;

    }
}

servlet 层

package com.slt;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.DbUtil;
import com.dao.HumanDao;
import com.model.Human;
public class HumanServlet extends HttpServlet {
    private DbUtil dbutil = new DbUtil();
    private Connection conn = null;


    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       //接受请求(登录)
        response.setContentType("text/html");
        String name1 = request.getParameter("name1");
        String age = request.getParameter("age1");
        int age1 = Integer.parseInt(age);
        conn = dbutil.getConnection();
        Human h = new Human();
        HumanDao hdao = new HumanDao();
        try {
           boolean b = hdao.find(conn,name1,age1);
//            request.getSession().setAttribute("h", h);
//            request.getRequestDispatcher("/index.jsp").forward(request, response);
            if(b){
                request.getRequestDispatcher("welcom.jsp").forward(request, response);
            }else{
                response.sendRedirect("index.jsp");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     //发送请求(注册)
        response.setContentType("text/html");
        String name = request.getParameter("name");
        String humanage = request.getParameter("age");
        int age = Integer.parseInt(humanage);
        Human h = new Human(name,age);
        HumanDao hdao = new HumanDao();
        try {
            conn = dbutil.getConnection();
            hdao.insertInfo(conn,h );
            System.out.println("插入成功");

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
       }
    }


}

model 层

package com.model;

public class Human {
    private  String name;
    private  int age;
    private int id;

    public Human() {
        super();
    }

    public Human(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

你可能感兴趣的:(登录注册(数据库连接))