“大家好,我是雄雄,欢迎关注微信公众号:雄雄的小课堂。
”
最近,正好在看jdbc
,于是就整理了一份增删改查的案例,记录记录,以备后用!!
java
编辑器:myeclipse![10](G:\公众号运营\雄雄老师\雄雄老师\10.png)![10](G:\公众号运营\雄雄老师\雄雄老师\10.png) 10.5
数据库:mysql
jdk
版本:1.8
首先,我们先来看看数据表的设计结构图:
image-20210914172557109列说明:
empno
:员工编号
empName
:姓名
age
:年龄
salary
:工资
deptNo
:部门编号
在写功能方法之前,我们需要声明几个变量,分别是Connection
(连接数据库)、PreparedStatement
(执行sql语句)、ResultSet
(结果集)对象。
static Connection conn = null; //连接对象
static PreparedStatement pre = null; //sql操作
static ResultSet rs = null; //结果集
public static Connection getConnection(){
//加载驱动
String driver = "com.mysql.cj.jdbc.Driver";
try {
Class.forName(driver);
//创建连接
String url = "jdbc:mysql://localhost:3306/empdb";
String uname = "dblog";
String pass = "root";
conn = DriverManager.getConnection(url,uname,pass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
此方法就是作为一个通用方法,在实现其他功能的时候,不需要在编写一遍,只需在用到的时候调用即可,大大减少代码量,提高开发效率。
public static void CloseConnection(ResultSet rs,
PreparedStatement pre,
Connection conn){
try{
if(rs!=null){
rs.close();
}
if(pre!=null){
pre.close();
}
if(conn!=null){
conn.close();
}
}catch (Exception e) {
e.printStackTrace();
}
}
public static void AddEmp(){
conn = getConnection();
String sql = "insert into emp (empname,age,salary,deptno) values(?,?,?,?)";
try {
pre = conn.prepareStatement(sql);
pre.setObject(1,"范啸天");
pre.setObject(2,20);
pre.setObject(3,3000);
pre.setObject(4,101);
int rel = pre.executeUpdate();
if(rel>0){
System.out.println("成功");
}else{
System.out.println("失败");
}
CloseConnection(rs, pre, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
//范啸天改成王炎霸
public static void UpdateEmp(){
conn = getConnection();
String sql = "update emp set empname = ? where empname = ?";
try {
pre = conn.prepareStatement(sql);
pre.setObject(1,"王炎霸");
pre.setObject(2,"范啸天");
int rel = pre.executeUpdate();
if(rel>0){
System.out.println("修改成功");
}else{
System.out.println("修改失败");
}
CloseConnection(rs, pre, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void delEmp(){
String sql = "delete from emp where empname = ?";
//连接数据库的方法
conn = getConnection();
try {
pre = conn.prepareStatement(sql);
pre.setString(1,"王炎霸");
int rel= pre.executeUpdate();
if(rel>0){
System.out.println("成功");
}else{
System.out.println("失败");
}
CloseConnection(rs, pre, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void FindEmp(){
//连接数据库
conn = getConnection();
//写sql语句
String sql = "select * from emp";
//执行sql
try {
pre = conn.prepareStatement(sql);
rs = pre.executeQuery();
System.out.println("编号\t姓名\t年龄\t工资\t部门编号");
while(rs.next()){
System.out.print(rs.getInt(1)+"\t");
System.out.print(rs.getString(2)+"\t");
System.out.print(rs.getInt("age")+"\t");
System.out.print(rs.getDouble(4)+"\t");
System.out.println(rs.getInt(5)+"\t");
}
CloseConnection(rs, pre, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void FindEmpById(){
conn = getConnection();
String sql = "select * from emp where empno = ?";
try {
pre = conn.prepareStatement(sql);
pre.setObject(1,1);
rs = pre.executeQuery();
System.out.println("编号\t姓名\t年龄\t工资\t部门编号");
while(rs.next()){
System.out.print(rs.getObject(1)+"\t");
System.out.print(rs.getObject(2)+"\t");
System.out.print(rs.getObject(3)+"\t");
System.out.print(rs.getObject(4)+"\t");
System.out.println(rs.getObject(5));
}
CloseConnection(rs, pre, conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
今天分享的内容就这些啦,欢迎点赞、留言、和转发!