eclipse连接mysql数据库实现怎删改查操作实例(附带源码)

package model;
public class User {
private int id;
private String name;
private String password;
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package dao;
import java.util.List;
import model.User;
public interface IUser {
public void add(User user);
public void delete(int id);
public void update(User user);
public User load(int id);
public List<User> load();
public List<User> load(String content);
}
package dao; import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import com.sun.xml.internal.bind.v2.runtime.Name; import model.User;
import util.DBUtil;
import util.UserException; public class UserImp implements IUser{ public void add(User user) { Connection connection=DBUtil.getConnection(); String sql = "select count(*) from t_user where name = ?";//准备sql语句 PreparedStatement preparedStatement = null;
ResultSet resultSet = null; try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getName()); resultSet = preparedStatement.executeQuery(); while(resultSet.next()) {
if (resultSet.getInt(1) > 0) {
throw new UserException("用户已存在") ;
}
} String sql1 = "insert into t_user(name,password) value (?,?)";
preparedStatement = connection.prepareStatement(sql1);
preparedStatement.setString(1, user.getName());
preparedStatement.setString(2, user.getPassword());
preparedStatement.executeUpdate();
} catch (SQLException e) { e.printStackTrace();
}finally { DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
} } public void delete(int id) {
Connection connection = DBUtil.getConnection();
String sql = "delete from t_user where id = ?";
PreparedStatement preparedStatement = null; try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
} catch (SQLException e) { e.printStackTrace();
}finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
} } public void update(User user) { Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "update t_user set name = ? , password=? where id = ?";
//创建语句传输对象
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getName());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setInt(3, user.getId());
preparedStatement.executeUpdate();
} catch (SQLException e) { e.printStackTrace();
}finally {
DBUtil.close(preparedStatement);
DBUtil.close(connection);
} } public User load(int id) {
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from t_user where id = ?";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
User user = null;
try {
preparedStatement = connection.prepareStatement(sql); preparedStatement.setInt(1, id);
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
user = new User();
user.setId(id);
user.setName(resultSet.getString("name"));;
user.setPassword(resultSet.getString("password")); }
} catch (SQLException e) { e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return user;
} @Override
public List<User> load() {
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from t_user ";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//集合中只能放入user对象
List<User> users = new ArrayList<User>();
User user = null;
try { preparedStatement = connection.prepareStatement(sql); resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password")); users.add(user);
}
} catch (SQLException e) { e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return users;
} @Override
public List <User> load(String content) {
Connection connection = DBUtil.getConnection();
//准备sql语句
String sql = "select * from t_user ";
//创建语句传输对象
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
//集合中只能放入user对象
List<User> users = new ArrayList<User>();
User user = null;
try {
if (content == null || "".equals(content)) {
preparedStatement = connection.prepareStatement(sql);
}else {
sql += "where name like ? ";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, "%"+ content +"%"); }
resultSet = preparedStatement.executeQuery();
while(resultSet.next()) {
user = new User();
user.setId(resultSet.getInt("id"));
user.setName(resultSet.getString("name"));
user.setPassword(resultSet.getString("password")); users.add(user);
}
} catch (SQLException e) { e.printStackTrace();
}finally {
DBUtil.close(resultSet);
DBUtil.close(preparedStatement);
DBUtil.close(connection);
}
return users;
}
}
package filter; import java.io.IOException; import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse; public class CharFilter implements Filter{
String encoding = null;
public void init(FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getInitParameter("encoding");
} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException { request.setCharacterEncoding(encoding);
chain.doFilter(request, response);
} public void destroy() { }
}
package util; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; public class DBUtil { public static Connection getConnection() {
/*
* 加载驱动
*/
try { Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { e.printStackTrace();
}
String user = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3306/mysql";
/*
* 创建连接对象
*/
Connection connection = null;
try { connection = DriverManager.getConnection(url,user,password);
} catch (SQLException e) { e.printStackTrace();
}
return connection;
}
/*
* 关闭资源的方法
*/ public static void close(Connection connection) {//关闭连接对象的方法
try {
if (connection != null) {
connection.close();
} } catch (SQLException e) { e.printStackTrace();
}
}
public static void close(PreparedStatement preparedStatement ) {//关闭语句传输对象的方法
try {
if (preparedStatement != null) {
preparedStatement.close();
} } catch (SQLException e) { e.printStackTrace();
}
}
public static void close(ResultSet resultSet ) {//关闭结果集的方法
try {
if (resultSet != null) {
resultSet.close();
} } catch (SQLException e) { e.printStackTrace();
}
} }
package util; public class UserException extends RuntimeException
{ public UserException() {
super();
// TODO Auto-generated constructor stub
} public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
// TODO Auto-generated constructor stub
} public UserException(String message, Throwable cause) {
super(message, cause);
// TODO Auto-generated constructor stub
} public UserException(String message) {
super(message);
// TODO Auto-generated constructor stub
} public UserException(Throwable cause) {
super(cause);
// TODO Auto-generated constructor stub
} }
<%@page import="com.sun.org.apache.xalan.internal.xsltc.compiler.sym"%>
<%@page import="dao.UserImp"%>
<%@page import="model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
String name=(String)request.getParameter("username");
String password=(String)request.getParameter("pass");
User user=new User();
user.setName(name);
user.setPassword(password); System.out.print(user.getName());
System.out.print(user.getPassword()); UserImp userImp=new UserImp();
try{
userImp.add(user);}catch(Exception e){} response.sendRedirect("list.jsp");
%> <h2>添加成功!</h2> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title> </head>
<body>
<form action="add.jsp" method="post">
<table border="1" bordercolor="#FFFF33" align="center" >
<tr> <td align="right" colspan="2">用户名<input type="text" name="username" style="width:100px;" /></td><br> </tr>
<tr>
<td align="right" colspan="2">密 码<input type="password" name="pass" style="width:100px;"/></td>
</tr>
<tr>
<td align="left" style="width:60px;height:10px;font-size:10px;">没有用户名?<a href="register.jsp">[注册]</a>一个吧</td> <td align="right"><input type="submit" value="添加"style="width:100px;height:30px;font-size:16px;"/></td> </tr> </table> </form> </body>
</html>
<%@page import="model.User"%>
<%@page import="dao.UserImp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
int id = Integer.parseInt(request.getParameter("id"));
UserImp userImp=new UserImp();
User user=new User();
userImp.delete(id);
response.sendRedirect("list.jsp");
%> </html>
<%@page import="model.User"%>
<%@page import="java.util.List"%>
<%@page import="dao.UserImp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
UserImp userDao = new UserImp();
String content=request.getParameter("content");
if(content==null||"".equals(content)){
content="";} List<User> users = userDao.load(content); %>
</head>
<a href="addInput.jsp">继续添加</a>
<body>
<table align="center" border="1" width="500">
<tr align="center">
<td colspan="5" >增删改查</td>
</tr>
<tr>
<form action="list.jsp" method="post">
<td colspan="4">
请输入用户名 :
<input type="text" name="content" size="40" value="<%=content%>"/>
<input type="submit" value="搜索" />
</td>
</form>
</tr> <tr>
<td>用户编号</td>
<td>用户名</td>
<td>用户密码</td>
<td colspan="2"> 用户操作</td>
</tr>
<%
for( User user : users ){
%>
<tr>
<td> <%=user.getId() %></td>
<td> <%=user.getName()%></td>
<td> <%=user.getPassword() %></td> <td> <a href="delete.jsp?id=<%=user.getId() %>" >删除</a></td>
<td> <a href="update.jsp?id=<%=user.getId() %>" >更新</a></td> </tr>
<%
}
%>
</table> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="addInput.jsp" method="post">
<table border="1" bordercolor="#FFFF33" align="center" >
<tr> <td align="right" colspan="1">用户名<input type="text" name="username" style="width:100px;" /></td><br> </tr>
<tr>
<td align="right" colspan="1">密 码<input type="password" name="pass" style="width:100px;"/></td>
</tr>
<tr>
<td align="right" colspan="1">确认密码<input type="password" name="pass1" style="width:100px;"/></td>
</tr>
<tr>
<td align="right" colspan="1">手机号<input type="text" name="phone" style="width:100px;"/></td>
</tr>
<tr> <td align="right"><input type="submit" value="注册"style="width:100px;height:30px;font-size:16px;"/></td> </tr> </table> </form> </body> </html>
<%@page import="model.User"%>
<%@page import="dao.UserImp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
int id = Integer.parseInt(request.getParameter("id"));
UserImp userImp=new UserImp();
User user=new User();
user=userImp.load(id);
%>
</head>
<body>
<form action="update1.jsp" method="post">
<table border="1" bordercolor="#FFFF33" align="center" >
<tr>
<input type="hidden" name="id" style="width:100px;" value="<%=user.getId()%>"/>
<td align="right" colspan="2">用户名<input type="text" name="username" style="width:100px;" value="<%=user.getName()%>"/></td><br> </tr>
<tr>
<td align="right" colspan="2">密 码<input type="password" name="pass" style="width:100px;"/></td>
</tr>
<tr> <td align="right"><input type="submit" value="更新"style="width:100px;height:30px;font-size:16px;"/></td> </tr> </table> </form>
</body> </html>
<%@page import="dao.UserImp"%>
<%@page import="model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<%
int id = Integer.parseInt(request.getParameter("id"));
String name=request.getParameter("name");
String password=request.getParameter("pass");
UserImp userImp=new UserImp();
User user=new User();
user = userImp.load(id);
//user.setName(name);
user.setPassword(password); userImp.update(user);
response.sendRedirect("list.jsp");
%>
</head>
<body> </body>
</html>
eclipse连接mysql数据库实现怎删改查操作实例(附带源码)的更多相关文章
- 通过jdbc连接MySql数据库的增删改查操作
一.获取数据库连接 要对MySql数据库内的数据进行增删改查等操作,首先要获取数据库连接 JDBC:Java中连接数据库方式 具体操作如下: 获取数据库连接的步骤: 1.先定义好四个参数 String ...
- java jdbc 连接mysql数据库 实现增删改查
好久没有写博文了,写个简单的东西热热身,分享给大家. jdbc相信大家都不陌生,只要是个搞java的,最初接触j2ee的时候都是要学习这么个东西的,谁叫程序得和数据库打交道呢!而jdbc就是和数据库打 ...
- 【C#】使用NHibernate连接MySQL数据库及增删改查
学习资料 http://www.sikiedu.com/course/51/task/891/show https://www.codeproject.com/Articles/26123/NHibe ...
- 使用nodejs连接mysql数据库实现增删改查
首先要有数据库 使用xampp 或者 phpstudy 可以傻瓜式安装 新建一个项目文件夹 之后在这个目录下初始化package.json (npm init) 先在项目中安装mysql 和 ex ...
- 使用NHibernate连接MySQL数据库及增删改查
学习资料 http://www.sikiedu.com/course/51/task/891/show https://www.codeproject.com/Articles/26123/NHibe ...
- php 连接mysql数据库以及增删改查
php 连接数据库 一般是用面向对象的方法,需要先创建一个对象,即造一个连接对象,然后再写sql语句,(增改查删),最后执行sql语句 其中在创建连接对象时 我们用到的是MySQLI 是不区分大小写 ...
- 连接mysql数据库实现增删改查(一)
在python中我们通过pymysql来连接数据库,具体实现如下 ''' 连接mysql数据库 此类进行封装了一些基础的操作数据库方法 ''' import pymysql from Homework ...
- php之PDO连接mysql数据库,增删改查等等操作实例
我们使用传统的 mysql_connect .mysql_query方法来连接查询数据库时,如果过滤不严就有SQL注入风险,导致网站被攻击. 虽然可以用mysql_real_escape_string ...
- C++ API方式连接mysql数据库实现增删改查
这里复制的 http://www.bitscn.com/pdb/mysql/201407/226252.html 一.环境配置 1,装好mysql,新建一个C++控制台工程(从最简单的弄起,这个会了, ...
随机推荐
- Tensorflow之Slim使用
一些参考文档: 官方文档: TensorFlow-Slim image classification model library TensorFlow-Slim使用说明 TensorFlow-Slim ...
- java 根据系统日期获取前一天、后一天时间(根据初始日期推算出期望(向前/向后)日期)
1.情景展示 java 根据系统当前日期获取前一天日期.后一天日期,或者根据初始日期推算出期望(向前/向后)日期. 2.解决方案 导包 import java.text.ParseExcepti ...
- github远程建了分支,本地看不到的问题
原因:Git branch -a 查看的是本地仓库的所有分支 远程新建的没有同步前 就是看不到 解决:$git checkout master //首先切到master分支 $git pull ...
- Asp.net的HttpContext.Current.Items详解
之前asp.net只是在使用Session来进行用户会话时的信息存储,甚至很少留意Session完整的类调用是HttpContext.Current.Sessoin.... 好吧,我还是处于只会固定写 ...
- 分析技术和方法论营销理论知识框架,营销方面4P、用户使用行为、STP,管理方面5W2H、逻辑树、金字塔、生命周期
原文:五种分析框架:PEST.5W2H.逻辑树.4P.用户使用行为 最近在一点点的啃<谁说菜鸟不懂得数据分析>,相当慢,相当的费脑力,总之,真正的学习伴随着痛苦:) 最初拿到这本书的时候, ...
- system generator学习笔记【01】
作者:桂. 时间:2018-05-18 18:26:50 链接:http://www.cnblogs.com/xingshansi/p/9045914.html 前言 学习使用system gene ...
- 基础008_定浮点转化[floating point IP]
作者:桂. 时间:2018-05-15 21:55:50 链接:http://www.cnblogs.com/xingshansi/p/9042564.html 前言 本文为Xilinx float ...
- Android recovery支持adb shell
Android recovery支持adb shell 近期开发过程注意到recovery不支持adb shell.为了便于调试方便,决定添加此功能. 刚開始我们採用的是user版本号系统,进入rec ...
- guid与Base64编码互相转换
guid的长度比较长,本文提供一种方法,将guid转为base64字符串,只有22位长度,比较好! 参考:https://blog.csdn.net/tgghfbflishuai/article/de ...
- 微信jssdk常见错误及解决方法
调用config 接口的时候传入参数 debug: true 可以开启debug模式,页面会alert出错误信息.以下为常见错误及解决方法: invalid url domain当前页面所在域名与使用 ...