jsp dbbean mysql_jsp中对MySql的增删查改操作

1.配置数据源与数据池

在Web应用程序中建立一个META-INF目录,在其中建立一个context.xml文件。

//传递给JDBC驱动程序的数据库URL

2.进行连接数据库,这里为什么要单独写一个文件呢?因为一般采用DAO模式设计,在这个模式中,数据库访问与应用程序中实现业务逻辑是分开的。

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.sql.DataSource;

import java.sql.*;

public class DBBean{

Connection con;

DataSource dataSource;

public DBBean() throws NamingException, SQLException {

Context context = new InitialContext();

dataSource = (DataSource)context.lookup("java:comp/env/jdbc/sampleDS");

con = dataSource.getConnection();

}

public Connection getConnection() throws NamingException, SQLException {

return con;

}

}

3.实现数据的增添

Connection con = null;

ArrayListstudents = (ArrayList)request.getParameter("value"); //获取要插入的数据

student std = new student(); //把表的数据放到student类里

try {

DBBean dao = new DBBean(); //链接数据库

con = dao.getConnection();

} catch (NamingException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

String sql = "INSERT INTO student" + "(id, name, sex, major, hometown)VALUES(?,?,?,?,?)";

try(

PreparedStatement pstmt = con.prepareStatement(sql)) {

for(student std:students) {

pstmt.setString(1, std.getId());

pstmt.setString(2, std.getName());

pstmt.setString(3, std.getSex());

pstmt.setString(4, std.getMajor());

pstmt.setString(5, std.getHometown());

pstmt.executeUpdate();

}

}

catch (SQLException e) {

e.printStackTrace();

}

4.实现数据的删除

Connection con = null;

String s = (String)request.getParameter("value"); //要删除的数据的表的第一个值,同时也是主键

student std = new student(); //把表的数据放到student类里

try {

DBBean dao = new DBBean(); //链接数据库

con = dao.getConnection();

} catch (NamingException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

String sql = "DELETE FROM student where id = "+ s;

try(

PreparedStatement pstmt = con.prepareStatement(sql)){

pstmt.executeUpdate();

}catch(SQLException e) {

e.printStackTrace();

}

5.实现数据的查找

Connection con = null;

try {

DBBean dao = new DBBean(); //链接数据库

con = dao.getConnection();

} catch (NamingException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

ArrayListstdList = new ArrayList();

String sql = "SELECT * FROM student";

try (

PreparedStatement pstmt = con.prepareStatement(sql);

ResultSet rst = pstmt.executeQuery()){

while(rst.next()){

student std = new student();

std.setId(rst.getString("id"));

std.setName(rst.getString("name"));

std.setSex(rst.getString("sex"));

std.setMajor(rst.getString("major"));

std.setHometown(rst.getString("hometown"));

stdList.add(std);

}

}catch(SQLException e){

e.printStackTrace();

}

嘿嘿,我知道你也想要单个的查找

Connection con = null;

String s = (String)request.getParameter("value"); //要查找的数据的表的第一个值,同时也是主键

student std = new student(); //把表的数据放到student类里

try {

DBBean dao = new DBBean(); //链接数据库

con = dao.getConnection();

} catch (NamingException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

String sql = "SELECT id,name,sex,major,hometown" +

" FROM student WHERE id=?";

try(

PreparedStatement pstmt = con.prepareStatement(sql)){

pstmt.setString(1, s);

try(ResultSet rst = pstmt.executeQuery()){

if(rst.next()){

std.setId(rst.getString("id"));

std.setName(rst.getString("name"));

std.setSex(rst.getString("sex"));

std.setMajor(rst.getString("major"));

std.setHometown(rst.getString("hometown"));

}

}

}catch(SQLException e) {

e.printStackTrace();

}

6.实现数据的更新

Connection con = null;

String s = (String)request.getParameter("value"); //要修改的数据的表的第一个值,同时也是主键

student std = new student(); //把表的数据放到student类里

try {

DBBean dao = new DBBean(); //链接数据库

con = dao.getConnection();

} catch (NamingException e) {

e.printStackTrace();

} catch (SQLException e) {

e.printStackTrace();

}

String sql = "SELECT id,name,sex,major,hometown" +

" FROM student WHERE id=?";

try(

PreparedStatement pstmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, CONCUR_UPDATABLE)){

pstmt.setString(1, s);

try(ResultSet rst = pstmt.executeQuery()){

if(rst.next()){

//rst.updateString(1, request.getParameter("id"));

rst.updateString(2, request.getParameter("name"));

rst.updateString(3, request.getParameter("sex"));

rst.updateString(4, request.getParameter("major"));

rst.updateString(5, request.getParameter("hometown"));

rst.updateRow();

}

}

}catch(SQLException e) {

e.printStackTrace();

}

怎么说呢,其实这些都应该封装到一个Dao文件,具体的操作看你自己啦

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
package com.visual.utils; import java.sql.*; import java.util.List; public class DBBean { private String url = "jdbc:mysql://47.101.187.125:3310/douban?useSSL=false"; // 数据库信息,旧版本不用useSSL=false private String username = "root"; private String password = "rootRoot123."; private String driverName = "com.mysql.jdbc.Driver"; private Connection con = null; //连接对象 private PreparedStatement pstmt = null; //语句对象 private ResultSet rs = null; //结果集对象 public DBBean() throws ClassNotFoundException, SQLException { Class.forName(driverName); con = DriverManager.getConnection(url, username, password); } /* * sql:要执行的SQL语句 * params:SQL语句需要的变量 */ public int executeUpdate(String sql, List<Object> params) throws SQLException { pstmt = con.prepareStatement(sql); if (params != null && params.size() > 0) { for (int i = 0; i < params.size(); i++) { pstmt.setObject(i + 1, params.get(i)); } } return pstmt.executeUpdate(); } public ResultSet executeQuery(String sql, List<Object> params) throws SQLException { pstmt = con.prepareStatement(sql); if (params != null && params.size() > 0) { for (int i = 0; i < params.size(); i++) { pstmt.setObject(i + 1, params.get(i)); } } return pstmt.executeQuery(); } public void close() { if (rs != null) { try { rs.close(); } catch (Exception ee) { } } if (pstmt != null) { try { pstmt.close(); } catch (Exception ee) { } } if (con != null) { try { con.close(); } catch (Exception ee) { } } } }
最新发布
07-20

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值