设为首页 收藏本站
查看: 476|回复: 0

[经验分享] JDBC连接MYSQL,JDBC增删改查 经典 范例

[复制链接]
发表于 2016-10-19 03:33:02 | 显示全部楼层 |阅读模式
  一切详情请看代码:绝对经典,注视写的很清楚
  

package cn.csdn.dao;import java.util.List;import cn.csdn.domain.User;public interface UserDao {boolean insert(User entity);boolean checkUser(String name);public boolean delete(User entity);public boolean update(User entity);User findById(Integer id);List findAll();
}package cn.csdn.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import cn.csdn.domain.User;
public class UserDaoImpl implements UserDao{
/*声明操作数据的对象*/
private static Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
/*静态块*/
static{
/*准备驱动程序,加载驱动*/
try {
Class.forName("com.mysql.jdbc.Driver");
/*面试必考题*/
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/job?user=root&password=admin&useUnicode=true&characterEncoding=UTF-8");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean checkUser(String name) {
/*第一步:声明返回变量*/
boolean flag=false;
/*第二步:获取连接对象 conn */
/*第三步:定义sql语句 "select * from user where name='"+name+"'";*/
String sql = "select * from user where name=?";
try {
/*第四步:根据sql语句创建预处理对象*/
pstmt = conn.prepareStatement(sql);
/*第五步:为站位符 赋值*/
int index = 1;
pstmt.setString(index++, name);
/*第六步:执行查询*/
rs = pstmt.executeQuery();
/*第七步:判断*/
if(rs.next()){
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
}
/*第八步:关闭*/
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return flag;
}
@Override
public boolean insert(User entity) {
/*第一步:声明返回变量*/
boolean flag=false;
/*第二步:获取连接对象 conn */
/*第三步:定义sql语句 ;insert  into user(name,sex,age)values('"+entity.getName()+"','"+entity.getSex()+"',"+entity.getAge()+");"*/
String sql = "insert  into user(name,sex,age)values(?,?,?);";
try {
/*第四步:根据sql语句创建预处理对象*/
pstmt = conn.prepareStatement(sql);
/*第五步:为站位符 赋值*/
int index = 1;
pstmt.setObject(index++, entity.getName());
pstmt.setObject(index++, entity.getSex());
pstmt.setObject(index++, entity.getAge());
/*第六步:执行更新*/
int i = pstmt.executeUpdate();
/*第七步:判断*/
if(i>0){
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
}
/*第八步:关闭*/
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*记得一定要把返回的值,修改成返回的变量*/
return flag;
}
@Override
public boolean delete(User entity) {
/*第一步:声明返回变量*/
boolean flag=false;
/*第二步:获取连接对象 conn */
/*第三步:定义sql语句 ;insert  into user(name,sex,age)values('"+entity.getName()+"','"+entity.getSex()+"',"+entity.getAge()+");"*/
String sql = "delete from user where id=?";
try {
/*第四步:根据sql语句创建预处理对象*/
pstmt = conn.prepareStatement(sql);
/*第五步:为站位符 赋值*/
int index = 1;
pstmt.setInt(index++, entity.getId());
/*第六步:执行更新*/
int i = pstmt.executeUpdate();
/*第七步:判断*/
if(i>0){
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
}
/*第八步:关闭*/
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*记得一定要把返回的值,修改成返回的变量*/
return flag;
}
public static void main(String[] args) {
/*看明白了嘛*/
UserDao userDao = new UserDaoImpl();
/*boolean flag = userDao.checkUser("redarmy");
if(flag){
System.out.println("用户名已经被占用了");
}else{
System.out.println("用户名还没有被使用,你可以使用此用户名");
}*/
/*boolean flag = userDao.insert(new User(null, "红军12", "男", 20));
if(flag){
System.out.println("注册成功");
}else{
System.out.println("注册失败");
}*/
/*List users = userDao.findAll();
for (int i = 0; i < users.size(); i++) {
User entity = (User) users.get(i);
System.out.println(entity.getId()+"  "+entity.getName());
}*/
//userDao.update(new User(7,"孙悟空","女",10000));
User entity = userDao.findById(7);
System.out.println(entity.getId()+"  "+entity.getName());
}
@Override
public List findAll() {
/*第一步:定义返回结果*/
List allentities = new ArrayList();
/*第二步:获取连接对象*/
/*第三步:定义sql语句*/
String sql ="select * from user";
try {
/*第四步:根据sql语句创建预处理对象*/
pstmt = conn.prepareStatement(sql);
/*第五步:为站位符 赋值*/
/*第六步:执行查询*/
rs = pstmt.executeQuery();
/*第七步:判断*/
while(rs.next()){
User entity = new User();
entity.setId(rs.getInt("id"));
entity.setName(rs.getString("name"));
entity.setSex(rs.getString("sex"));
entity.setAge(rs.getInt("age"));
allentities.add(entity);
}
} catch (SQLException e) {
e.printStackTrace();
}
/*第八步:关闭*/
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return allentities;
}
@Override
public boolean update(User entity) {
/*第一步:声明返回变量*/
boolean flag=false;
/*第二步:获取连接对象 conn */
/*第三步:定义sql语句 ;insert  into user(name,sex,age)values('"+entity.getName()+"','"+entity.getSex()+"',"+entity.getAge()+");"*/
String sql = "update user set name=?,sex=?,age=? where id=?";
try {
/*第四步:根据sql语句创建预处理对象*/
pstmt = conn.prepareStatement(sql);
/*第五步:为站位符 赋值*/
int index = 1;
pstmt.setObject(index++, entity.getName());
pstmt.setObject(index++, entity.getSex());
pstmt.setObject(index++, entity.getAge());
pstmt.setInt(index++, entity.getId());
/*第六步:执行更新*/
int i = pstmt.executeUpdate();
/*第七步:判断*/
if(i>0){
flag = true;
}
} catch (SQLException e) {
e.printStackTrace();
}
/*第八步:关闭*/
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*记得一定要把返回的值,修改成返回的变量*/
return flag;
}
@Override
public User findById(Integer id) {
/*第一步:定义返回结果*/
User entity = new User();
/*第二步:获取连接对象*/
/*第三步:定义sql语句*/
String sql ="select * from user where id=?";
try {
/*第四步:根据sql语句创建预处理对象*/
pstmt = conn.prepareStatement(sql);
/*第五步:为站位符 赋值*/
int index = 1;
pstmt.setInt(index++, id);
/*第六步:执行查询*/
rs = pstmt.executeQuery();
/*第七步:判断*/
if(rs.next()){
entity.setId(rs.getInt("id"));
entity.setName(rs.getString("name"));
entity.setSex(rs.getString("sex"));
entity.setAge(rs.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
}
/*第八步:关闭*/
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return entity;
}
}
package cn.csdn.domain;import java.io.Serializable;/*业务Bean*/
public class User implements Serializable{/*** */private static final long serialVersionUID = 2646300928284173726L;/**递增序列*/private Integer id;/**姓名*/private String name;/*性别*/private String sex;/*年龄*/private Integer age;public User() {super();// TODO Auto-generated constructor stub}public User(Integer id, String name, String sex, Integer age) {super();this.id = id;this.name = name;this.sex = sex;this.age = age;}public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}}



  ----------------代码源自于redarmychen


  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-287985-1-1.html 上篇帖子: (转)MySQL 的数据类型和建库策略 下篇帖子: MySQL心得7-2-存储函数、触发器
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表