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;
}
}
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。