用jdbc操作数据库的简单例子
1、在mysql中创建数据库 users (Integer id,String name, String pass,String sex,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 sex;
private Integer age;
public Users() {
super();
// TODO Auto-generated constructor stub
}
public Users(Integer id, String name, String pass, String sex, Integer age) {
super();
this.id = id;
this.name = name;
this.pass = pass;
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 getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
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;
}
@Override
public String toString() {
return "Users [id=" + id + ", name=" + name + ", pass=" + pass
+ ", sex=" + sex + ", age=" + age + "]";
}
}
domain层中封装操作接口与接口实现类
package cn.csdn.web.dao;
import cn.csdn.web.domain.Users;
public interface 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,sex,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=?,sex=?,age=? where id=?";
try {
//根据sql语句创建预处理对象
pstmt = conn.prepareStatement(sql);
//为占位符赋值
int index = 1;
pstmt.setObject(index++, entity.getName());
pstmt.setObject(index++, entity.getPass());
pstmt.setObject(index++, entity.getSex());
pstmt.setObject(index++, entity.getAge());
pstmt.setObject(index++, entity.getId());
//执行更新
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 Users findById(Integer id) {
//声明返回值变量
Users entity = new Users();
//获取连接对象
conn = JdbcUtil.getConn();
//定义sql语句
String sql = "select * from users where id=?";
try {
//根据sql语句创建预处理对象
pstmt = conn.prepareStatement(sql);
//为占位符赋值
int index = 1;
pstmt.setObject(index++, id);
//执行更新
rs = pstmt.executeQuery();
if(rs.next()){
entity.setId(rs.getInt("id"));
entity.setName(rs.getString("name"));
entity.setPass(rs.getString("pass"));
entity.setSex(rs.getString("sex"));
entity.setAge(rs.getInt("age"));
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//释放资源
JdbcUtil.release(rs, pstmt);
return entity;
}
}
junit测试
package cn.csdn.web.junit;
import org.junit.Test;
import cn.csdn.web.dao.UsersDao;
import cn.csdn.web.dao.UsersDaoImpl;
import cn.csdn.web.domain.Users;
public class UsersTest {
UsersDao uDao = new UsersDaoImpl();
@Test
public void insert(){
Users entity = new Users();
entity.setName("测试1");
entity.setPass("123");
entity.setSex("女");
entity.setAge(21);
boolean flag = uDao.insert(entity);
if(flag){
System.out.println("插入成功");
}else{
System.out.println("插入失败");
}
}
@Test
public void findById(){
Users entity = uDao.findById(1);
System.out.println(entity.toString());
}
@Test
public void update(){
Users entity = uDao.findById(4);
entity.setName("测试2");
entity.setAge(20);
boolean flag = uDao.updateById(entity);
if(flag){
System.out.println("更新成功");
}else{
System.out.println("更新失败");
}
}
@Test
public void deleteById(){
boolean flag = uDao.deleteById(27);
if(flag){
System.out.println("删除成功");
}else{
System.out.println("删除失败");
}
}
}