在JAVA中使用JDBC连接数据库实例操作 ----完成对数据库的增、删、改、查、批处理、使用propertise文件等操作
1,连接数据库
(1)连接数据库需要:连接数据库字符串;连接数据库的账号;连接数据库的密码。
(2)与数据库进行连接:需要加载数据库驱动,通过驱动管理里面的方法得到与数据库的连接。
(3)驱动下载
链接:https://pan.baidu.com/s/1vsIh8quO6ZQd7AaWGFb3zQ
提取码:jdbc
复制这段内容后打开百度网盘手机App,操作更方便哦
(3)代码展示:
package dbhelper;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Helper {
//连接数据库字符串
public static final String URL="jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC";
/*
localhost 主机地址 3306 端口号 test 数据库名称
?characterEncoding=utf8&useSSL=false&serverTimezone=UTC 如果你的MySQL数据库版本是5.5以上的需要添加这行代码
*/
//连接数据库的账号
public static final String USERNAME="root";
//连接数据库的密码
public static final String PWD="root";
//定义一个数据库连接对象
public static Connection conn;
//与数据库进行连接
public static Connection getConn(){
try {
//加载数据库驱动,现在连接的是mysql数据库
Class.forName("com.mysql.jdbc.Driver");
try {
//驱动管理里面的方法得到与数据库的连接
conn= DriverManager.getConnection(URL,USERNAME,PWD);
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
//关闭数据库连接
public static void realease(Connection conn){
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
System.out.println(Helper.getConn());
}
}
(4)运行结果:
2,数据库原始表
3,编写用户实体类
代码展示:
package entity;
public class Deptno {
private String deptno;
private String dname;
private String loc;
public String getDeptno() {
return deptno;
}
public void setDeptno(String deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public Deptno(String deptno, String dname, String loc) {
//有参构造方法
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
public Deptno() {
//无参构造方法
super();
}
}
4,定义接口
代码展示:
package dept;
import entity.Deptno;
import java.util.List;
public interface Ideptno {
//查询表中全部信息
List<Deptno> findAllUname();
//查询表中单个信息
Deptno findUserById(String id);
//修改表中信息
public int updata(Deptno ui);
//添加信息
public int add(Deptno ui);
//删除信息
public int delete(String ui);
//批处理
public int[] addUser(List<Deptno> list);
}
5,定义接口实现类
基本步骤:
1,获得数据库连接对象
2,书写sql语句操作数据库
3,预编译sql语句
4,执行命令
代码展示:
package dept;
import dbhelper.Helper;
import entity.Deptno;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class IdeptnoEntity implements Ideptno{
//查询表中全部信息
@Override
public List<Deptno> findAllUname() {
List<Deptno> list = new ArrayList<Deptno>();
//获得数据库连接对象
Connection coon = Helper.getConn();
//书写sql语句操作数据表
String sql="select * from dept";
//预编译sql语句
try {
PreparedStatement ps = coon.prepareStatement(sql);
//执行查询命令
ResultSet rs = ps.executeQuery();
//循环读取结果集 中的数据
while (rs.next()){
//把结果集里的数据读取出来赋值给用户类的属性上
Deptno dt = new Deptno();
dt.setDeptno(rs.getString(1));
dt.setDname(rs.getString(2));
dt.setLoc(rs.getString(3));
list.add(dt);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//查询表中单条信息
@Override
public Deptno findUserById(String id) {
Deptno dt =null;
ArrayList<Deptno> list = new ArrayList<Deptno>();
//获得数据库连接对象
Connection coon = Helper.getConn();
//书写sql语句操作数据表
String sql="select * from dept WHERE deptno=?";
//预编译sql语句
try {
PreparedStatement ps = coon.prepareStatement(sql);
ps.setString(1,id);
//执行查询命令
ResultSet rs = ps.executeQuery();
//循环读取结果集 中的数据
while (rs.next()){
//把结果集里的数据读取出来赋值给用户类的属性上
dt=new Deptno();
dt.setDeptno(rs.getString(1));
dt.setDname(rs.getString(2));
dt.setLoc(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
}
return dt;
}
//修改表中信息
@Override
public int updata(Deptno ui) {
//获得数据库连接对象
Connection conn = Helper.getConn();
//书写sql语句操作数据表
String sql="update dept set dname=?,loc=? where deptno=?";
int result = 0;
//预编译sql语句
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(3,ui.getDeptno());
ps.setString(1,ui.getDname());
ps.setString(2,ui.getLoc());
//执行修改命令
result=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
//添加信息
@Override
public int add(Deptno ui) {
//获得数据库连接对象
Connection conn = Helper.getConn();
//书写sql语句操作数据表
String sql="insert into dept values (?,?,?)";
int result=0;
//预编译sql语句
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,ui.getDeptno());
ps.setString(2,ui.getDname());
ps.setString(3,ui.getLoc());
//执行添加命令
result=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
//删除信息
@Override
public int delete(String ui) {
//获得数据库连接对象
Connection conn = Helper.getConn();
//书写sql语句操作数据表
String sql="delete from dept where deptno=?";
int result=0;
//预编译sql语句
try {
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1,ui);
//执行删除命令
result=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
//批处理
@Override
public int[] addUser(List<Deptno> list) {
int [] result = null;
Connection conn = Helper.getConn();
String sql="insert into dept values(?,?,?)";
try {
PreparedStatement ps = conn.prepareStatement(sql);
//开启事务
conn.setAutoCommit(false);
//对列表进行迭代
Iterator<Deptno> it = list.iterator();
while (it.hasNext()){
//迭代出Deptno对象
Deptno dt = it.next();
ps.setString(1,dt.getDeptno());
ps.setString(2,dt.getDname());
ps.setString(3,dt.getLoc());
//批处理
ps.addBatch();
}
//执行批处理
result=ps.executeBatch();
//手动提交
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
6,测试
(1),查看表中所有信息:
代码展示:
package dept;
import entity.Deptno;
import java.util.List;
public class Text {
public static void main(String[] args) {
IdeptnoEntity ite = new IdeptnoEntity();
List<Deptno> list = ite.findAllUname();
for (Deptno a : list) {
System.out.println(a.getDeptno()+"\t"+a.getDname()+"\t"+a.getLoc());
}
}
}
运行结果:
(2),查询单条信息
代码展示:
package dept;
import entity.Deptno;
import java.util.Scanner;
public class Test1 {
public static void main(String[] args) {
IdeptnoEntity ite = new IdeptnoEntity();
System.out.println("请输入你想要查看内容的编号:");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
Deptno dt=ite.findUserById(a);
if (dt!=null){
System.out.println("找到了该用户");
System.out.println(dt.getDeptno()+"\t"+dt.getDname()+"\t"+dt.getLoc());
}else {
System.out.println("用户不存在");
}
}
}
运行结果:
(3)增加信息
代码展示:
package dept;
import entity.Deptno;
public class Test3 {
public static void main(String[] args) {
IdeptnoEntity it = new IdeptnoEntity();
int result=it.add(new Deptno("7","售后部","北京市"));
if (result!=0){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
}
}
运行结果:
(4),删除信息
代码展示:
package dept;
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
IdeptnoEntity it = new IdeptnoEntity();
System.out.println("请输入你想要删除的编号:");
Scanner scanner = new Scanner(System.in);
String a = scanner.next();
int result=it.delete(a);
if (result!=0){
System.out.println("删除成功");
}else {
System.out.println("删除失败");
}
}
}
运行结果:
(5),修改信息
代码展示:
package dept;
import entity.Deptno;
public class Test2 {
public static void main(String[] args) {
IdeptnoEntity it = new IdeptnoEntity();
int result=it.updata(new Deptno("3","生产部","南京市"));
if (result!=0){
System.out.println("修改成功");
}else {
System.out.println("未查到该部门");
}
}
}
运行结果:
(6)批处理
代码展示:
package dept;
import entity.Deptno;
import java.util.ArrayList;
public class Test5 {
public static void main(String[] args) {
ArrayList<Deptno> list = new ArrayList<Deptno>();
IdeptnoEntity idep = new IdeptnoEntity();
list.add(new Deptno("7","后勤部","北京市"));
list.add(new Deptno("8","维修部","北京市"));
int[]result=idep.addUser(list);
if (result.length>0){
System.out.println("批处理成功");
}else {
System.out.println("批处理失败");
}
}
}
运行结果:
(7)使用propertise文件
首先创建一个jdbc.propertise文件。
代码展示:
package dept;
import java.sql.*;
import java.util.*;
import java.io.*;
public class Proper {
public static void main(String[] args) {
Properties prop = new Properties();
Connection conn=null;
Statement stat=null;
try {
//获取jdbc.properties中的内容
prop.load(new FileInputStream("jdbc.properties"));
String driver = prop.getProperty("driver");
String url = prop.getProperty("url");
String username = prop.getProperty("username");
String password = prop.getProperty("password");
try {
//注册驱动
Class.forName(driver);
try {
//获取连接
conn=DriverManager.getConnection(url,username,password);
//获取数据库操作对象
stat=conn.createStatement();
//执行sql语句
String sql="update dept set dname='销售部',loc='北京市' where deptno=9";
int result = stat.executeUpdate(sql);
System.out.println(result==1?"修改成功":"修改失败");
} catch (SQLException e) {
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
//释放资源
if (stat!=null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
运行结果: