java增删改查代码_Javaweb简单的增删改查程序(超详细)就是简单的对数据进⾏增删改查。代码如下:
1.bean层:⽤来封装属性及其get set⽅法 toString⽅法,有参构造⽅法,⽆参构造⽅法等。
public classBean {private intid;privateString name;privateString password;privateString sex;public intgetId() {returnid;
}public void setId(intid) {this.id =id;
电子商务网站模板}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getPassword() {returnpassword;
}public voidsetPassword(String password) {this.password =password;
}publicString getSex() {returnsex;
}public voidsetSex(String sex) {this.sex =sex;
}
@OverridepublicString toString() {return "Bean [id=" + id + ", name=" + name + ", password=" + password + ", sex=" + sex + "]";
}public Bean(intid, String name, String password, String sex) {super();this.id =id;this.name =name;this.password
=password;this.sex =sex;
}publicBean() {//TODO Auto-generated constructor stub
}
}
2.DBUtil:对数据库连接关闭操作的封装:
importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.SQLException;importjava.sql.St classDBUtil {private static String url = "jdbc:mysql://localhost:3306/db10?
useUnicode=true&characterEncoding=utf8";private static String user = "root";private static String password = "root";private static String jdbcName="sql.jdbc.Driver";private Connection con=null;public staticConnection getConnection() {
Connection con=null;try{
Class.forName(jdbcName);
Connection(url, user, password);//System.out.println("数据库连接成功");
} catch(Exception e) {//TODO Auto-generated catch block//System.out.println("数据库连接失败");
e.printStackTrace();
}returncon;
}public static voidclose(Connection con) {if(con!=null)try{
con.close();
模块建房尺寸}catch(SQLException e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}public static voidclose(Statement state, Connection conn) {if(state!=null) {try{
state.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if(conn!=null) {try{
conn.close();
}catch(SQLException e) {
e.printStackTrace();
}
}
}public static voidclose(ResultSet rs, Statement state, Connection conn) {if(rs!=null) {try{
rs.close();
}catch(SQLException e) {
e.printStackTrace();
}
}if(state!=null) {try{
state.close();
}catch(SQLException e) {
mysql面试题 增删改查e.printStackTrace();
}
}if(conn!=null) {try{
conn.close();
}catch(SQLException e) {
group by 排序e.printStackTrace();
}
}
}
}
可以将其定义为⼀个⼯具类,每次使⽤的时候直接复制,然后更改url⾥数据库的名字,这样可以提⾼效率。
dao层:对数据库的各种增删改查⽅法的封装:
1 importjava.sql.Connection;
2 importjava.sql.PreparedStatement;
3 importjava.sql.ResultSet;
4 importjava.sql.SQLException;
5 importjava.sql.Statement;
6 importjava.util.ArrayList;
7 importjava.util.List;8
9 importorg.junit.jupiter.api.Test;10
11 public class Dao {//dao层
12 private DBUtil dbutil=newDBUtil();13
14
15 publicDao() {16 //TODO Auto-generated constructor stub
17 }18 @Test19 public boolean insert(Bean bean) {//插⼊数据的⽅法
20 boolean f=false;21 String sql="insert into info(id,name,password,sex)
values('"+Id()+"','"+Name()+"','"+Password()+"','"+Sex()+"')";22 Connection
Connection();//数据库连接,加载驱动
23 Statement state=null;24 try
25 {26 ateStatement();//实例化Statement对象,⽅便对sql语句进⾏操作
27 System.out.println(conn);uteUpdate(sql);29 f=true;30 //执⾏数据库更新操作⽤于执⾏INSERT、UPDATE或DELETE语句以及SQLDDL(数据定义语⾔)语句,31 //例如CREATETABLE和DROPTABLE,(创建表和删除表)
32 }catch(Exception e)//当try语句中s出现异常时,会执⾏catch中的语句
33 {34 e.printStackTrace();//捕获异常的语句
35 }36 finally //finally作为异常处理的⼀部分,它只能⽤在try/catch语句中,并且附带⼀个语句块,表⽰这段语句最终⼀定会被执⾏(不管有没有抛出异常),经常被⽤在需要释放资源的情况下。
37 {38 DBUtil.close(conn);39 }40 returnf;41 }42
43 public boolean delete(int id ) {//删除⽅法
44 String sql="delete from info where id='"+id+"'";45 boolean f=false;46 Connection conn =Connection();47 Statement st=null;48 try{49 ateStatement();uteUpdate(sql);51 f=true;52 } catch(SQLException e) {53 //TODO Auto-generated catch block
54 e.printStackTrace();55 }56 finally{57 DBUtil.close(st, conn);58 }59 returnf;60 }61 public boolean update(Bean bean) {//更新⽅法
62 String sql="update info set
name='"+Name()+"',password='"+Password()+"',sex='"+Sex()+"'where id='"+Id()+"'";63 Connection Connection();64 boolean f=false;65 Statement st=null;66 try{67 ateStatement();uteUpdate(sql);69 f=true;70 } catch(SQLException e) {71 //TODO Auto-generated catch block
72 e.printStackTrace();73 }74 returnf;75 }76
77 public List list(){//查询所有⽅法
78 String sql="select * from info order by id ASC";79 Connection Connection();80 Statement st=null;81 List list=new ArrayList<>();82 ResultSet rs=null;83 Bean bean=null;84 try{85 ateStatement();86
90 int Int("id");91 String name = rs.getString("name");92 String password = rs.getString("password");93 String sex = rs.getString("sex");94 bean=newBean(id,name,password,sex);95 list.add(bean);96 }97 } catch(SQLException e) {98
//TODO Auto-generated catch block
99 e.printStackTrace();100 }101 finally{102 DBUtil.close(rs, st, conn);103 }104 returnlist;105 }106
107
108
109
110
111 }
对数据库进⾏操作的⽅法都封装在⾥⾯。
servlet:简单说servlet就是跳转的类,当什么情况下⼲什么跳转到哪⾥。
importjava.io.IOException;importjava.io.UnsupportedEncodingException;importjava.util.List;importjavax.servlet.ServletException Servlet implementation class servlet*/@WebServlet("/servlet")public class servlet extendsHttpServlet {
Dao dao=newDao();private static final long serialVersionUID = 1L;/***@seeHttpServlet#HttpServlet()*/
publicservlet() {super();//TODO Auto-generated constructor stub
}private void update(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException
{//TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");int id = Integer.Parameter("id"));
String name= Parameter("name");
String password= Parameter("password");
String sex= Parameter("sex");
Bean bean=newBean(id,name,password,sex);
dao.update(bean);
request.setAttribute("message", "修改成功");
简述mvc架构包含的组件以及作用
}private void list(HttpServletRequest request, HttpServletResponse response) throwsException {//TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
List list =dao.list();
request.setAttribute("list", list);
}private void delete(HttpServletRequest request, HttpServletResponse response) throwsException, IOException {//TODO
Auto-generated method stub
request.setCharacterEncoding("UTF-8");int id=Integer.Parameter("id"));
dao.delete(id);//进⾏数据库的删除操作
request.setAttribute("message", "删除成功");
}private void insert(HttpServletRequest request, HttpServletResponse response) throwsIOException, ServletException
{//TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");int id = Integer.Parameter("id"));
String name= Parameter("name");
String password= Parameter("password");
String sex= Parameter("sex");
Bean bean=newBean(id,name,password,sex);if(dao.insert(bean)) {
request.setAttribute("message", "添加成功");
}
}/***@seeHttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//TODO Auto-generated method stub
request.setCharacterEncoding("utf-8");
String Parameter("method");if("insert".equals(method)) {
insert(request,response);
}else if("delete".equals(method)) {try{
delete(request,response);
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}else if("update".equals(method)) {
update(request,response);
translate to english}else if("list".equals(method)) {try{
list(request,response);
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}
}
}/***@seeHttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throwsServletException, IOException {//TODO Auto-generated method stub
doGet(request, response);
}
}
注意:在创建的时候⼀定要选择创建servlet⽽不是类如图:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论