当前位置:编程学习 > JAVA >>

用jdbc操作数据库的简单例子

1、在mysql中创建数据库  users  (Integer id,String name, String pass,String 易做图,Integer age);
 
2、分层实现:
 
cn.csdn.web.util     封装单利模式的Connction对象
 
cn.csdn.web.domain   封装实体bean
 
cn.csdn.web.dao   封装接口与接口实现类
 
cn.csdn.web.junit    测试类
 
 
 
首先利用单利模式创建Connection对象   (一定要记得在lib文件夹中放入驱动jar包)
 
cn.csdn.web.util     封装单利模式的Connction对象
package cn.csdn.web.Util;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
public class JdbcUtil {
 
//定义数据库URL语句
private static final String url = "jdbc:mysql://localhost:3306/3g?user=root&password=211314&useUnicode=true&characterEncoding=UTF8";
//利用单利模式创建Connction对象
private static Connection conn = null;
//加载驱动
public static Connection getConn(){
if(conn==null){
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
}
return conn;
}
 
//释放资源方法
public static void release(ResultSet rs,PreparedStatement pstmt){
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();
}
}
}
}
 
 
 
在domain层中封装实体bean
 
cn.csdn.web.util     封装单利模式的Connction对象
package cn.csdn.web.domain;
 
public class Users {
private Integer id;
private String name;
private String pass;
private String 易做图;
private Integer age;
public Users() {
super();
// TODO Auto-generated constructor stub
}
public Users(Integer id, String name, String pass, String 易做图, Integer age) {
super();
this.id = id;
this.name = name;
this.pass = pass;
this.易做图 = 易做图;
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 getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getSex() {
return 易做图;
}
public void setSex(String 易做图) {
this.易做图 = 易做图;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + ", pass=" + pass
+ ", 易做图=" + 易做图 + ", age=" + age + "]";
}
}
 
 
 
domain层中封装操作接口与接口实现类
 
package cn.csdn.web.dao;
 
import cn.csdn.web.domain.Users;
 
public inte易做图ce UsersDao {
boolean insert(Users entity);//增
boolean deleteById(Integer id);//删
boolean updateById(Users entity);//改
Users findById(Integer id);//查
}
 
cn.csdn.web.util     封装单利模式的Connction对象
package cn.csdn.web.dao;
 
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
 
import cn.csdn.web.Util.JdbcUtil;
import cn.csdn.web.domain.Users;
 
 
public class UsersDaoImpl implements UsersDao {
 
 
//封装数据库对象
private static Connection conn;
private PreparedStatement pstmt;
private ResultSet rs;
 
//增
public boolean insert(Users entity) {
//声明返回值变量
boolean flag = false;
//获取连接对象
conn = JdbcUtil.getConn();
//定义sql语句
String sql = "insert into users(id,name,pass,易做图,age) values(?,?,?,?,?)";
try {
//根据sql语句创建预处理对象
pstmt = conn.prepareStatement(sql);
//为占位符赋值
int index = 1;
pstmt.setObject(index++, entity.getId());
pstmt.setObject(index++, entity.getName());
pstmt.setObject(index++, entity.getPass());
pstmt.setObject(index++, entity.getSex());
pstmt.setObject(index++, entity.getAge());
//执行更新
int i = pstmt.executeUpdate();
if(i>0){
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
//释放资源
JdbcUtil.release(rs, pstmt);
return flag;
}
 
 
//删
public boolean deleteById(Integer id) {
//声明返回值变量
boolean flag = false;
//获取连接对象
conn = JdbcUtil.getConn();
//定义sql语句
String sql = "delete from users where id=?";
try {
//根据sql语句创建预处理对象
pstmt = conn.prepareStatement(sql);
//为占位符赋值
int index = 1;
pstmt.setObject(index++, id);
//执行更新
int i = pstmt.executeUpdate();
if(i>0){
flag = true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
//释放资源
JdbcUtil.release(rs, pstmt);
return flag;
}
 
 
//改
public boolean updateById(Users entity) {
//声明返回值变量
boolean flag = false;
//获取连接对象
conn = JdbcUtil.getConn();
//定义sql语句
String sql = "update users set name=?,pass=?,易做图=?,age=? where id=?";
try {
//根据sql语句创建预处理对象
pstmt = conn.prepareStatement(sql);
//为占位符赋值
int index = 1;
pstmt.se
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,