java连接mysql增_Java连接MYSQL进行操作(增,删,改)

连接数据库,并用表格输出数据

创建insex.jsp

序号学号姓名性别出生日期操作

int n=0;

String driver_class="com.mysql.cj.jdbc.Driver";

String serverip="localhost";

String databasename="oa";

String username="root";

String pwd="123456";

String jdbcUrl="jdbc:mysql://"+serverip+":3306/"+databasename+"?serverTimezone=Asia/Shanghai&useSSL=true";

Class.forName(driver_class);

Connection conn= DriverManager.getConnection(jdbcUrl,username,pwd);

PreparedStatement pst= conn.prepareStatement("select * from student");

ResultSet rs= pst.executeQuery();

//ResultSet 一个指向数据库的变量,是不包含内容的

while(rs.next()){

n++;

%>

添加

">删除

&name=&bir=">修改

}

pst.close();

conn.close();

%>

运行结果如下:

896eac060d0b109cbc7711850775c404.png

创建delete.jsp

String id=request.getParameter("id");

String driver_class="com.mysql.cj.jdbc.Driver";

String serverip="localhost";

String databasename="oa";

String username="root";

String pwd="123456";

String jdbcUrl="jdbc:mysql://"+serverip+":3306/"+databasename+"?serverTimezone=Asia/Shanghai&useSSL=true";

Class.forName(driver_class);

Connection conn= DriverManager.getConnection(jdbcUrl,username,pwd);

PreparedStatement pst1=conn.prepareStatement("delete from student where id=?");

pst1.setString(1,id);

pst1.execute();

pst1.close();

conn.close();

response.sendRedirect("index.jsp");

%>

在之前的insex.jsp中已经调用过了

41387ca923d32e44f1e9c365eaa7d7a5.png

运行后:从7条减为6条了

003cf303df06109e0701a99bbf40a574.png

创建insert_action.jsp

请输入姓名:

请输入性别:

请输入出生日期:

创建insert.jsp

String name=request.getParameter("username");

name=new String(name.getBytes("iso-8859-1"),"UTF-8");

String sex=request.getParameter("radiobutton");

sex=new String(sex.getBytes("iso-8859-1"),"UTF-8");

String birthday=request.getParameter("birthday");

String driver_class="com.mysql.cj.jdbc.Driver";

String serverip="localhost";

String databasename="oa";

String username="root";

String pwd="123456";

String jdbcUrl="jdbc:mysql://"+serverip+":3306/"+databasename+"?serverTimezone=Asia/Shanghai&useSSL=true";

Class.forName(driver_class);

Connection conn= DriverManager.getConnection(jdbcUrl,username,pwd);

PreparedStatement pst1=conn.prepareStatement("insert into student(user_name,sex,birthday)values(?,?,?)");

pst1.setString(1,name);

pst1.setString(2,sex);

pst1.setString(3,birthday);

pst1.execute();

pst1.close();

conn.close();

response.sendRedirect("index.jsp");

%>

在之前的index.jsp中

2d02ddcc67feabd8e04cd0783bd18aa4.png

点击添加后

bbe7007c90dbaf78ed54737a3a2d0aee.png

输入数据再点击添加按钮

6c3e0a1cffce80befc717b2c2c322d5c.png

可以看到数据添加成功了

ef761632cef2ae7b433c42958c920997.png

创建update_action.jsp

String n=request.getParameter("id");

String name=request.getParameter("name");

String sex=request.getParameter("sex");

String birthday=request.getParameter("bir");

%>

请输入姓名:

请输入性别:

function loadsex(){

if(sex.equals("男")){

%>

document.getElementById("radiobutton1").checked="checked";

document.getElementById("radiobutton2").checked="";

}else{

%>

document.getElementById("radiobutton1").checked="";

document.getElementById("radiobutton2").checked="checked";

}

%>

}

οnlοad=loadsex;

请输入出生日期:

在创建update.jsp

String id=request.getParameter("id");

String name=request.getParameter("username");

name=new String(name.getBytes("iso-8859-1"),"UTF-8");

String sex=request.getParameter("radiobutton");

sex=new String(sex.getBytes("iso-8859-1"),"UTF-8");

String birthday=request.getParameter("birthday");

String driver_class="com.mysql.cj.jdbc.Driver";

String serverip="localhost";

String databasename="oa";

String username="root";

String pwd="123456";

String jdbcUrl="jdbc:mysql://"+serverip+":3306/"+databasename+"?serverTimezone=Asia/Shanghai&useSSL=true";

Class.forName(driver_class);

Connection conn= DriverManager.getConnection(jdbcUrl,username,pwd);

PreparedStatement pst1=conn.prepareStatement("update student set user_name=?,sex=?,birthday=? where id=?;");

pst1.setString(1,name);

pst1.setString(2,sex);

pst1.setString(3,birthday);

pst1.setString(4,id);

pst1.execute();

pst1.close();

conn.close();

response.sendRedirect("index.jsp");

%>

在之前的index.jsp中

4e73b403f7d05334c6ba31c320eda119.png

点击修改后,出现修改前的数据,直接在文本框中修改即可

9a5a5582136086baec8b02d7eb9bdf0f.png

把傻明改为明za,再点击修改按钮

c02abefa81bc587d2b8def7a734c3995.png

fd25acdaed3f4c0f27e1d79087ece0e2.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值