- 注重版权,转载请注明原作者和原文链接
作者:码农BookSea
原文链接:https://editor.csdn.net/md?articleId=105959128
本人刚写博客不久,是个新人,望大家能给予一些鼓励。 您的一个赞或者是评论区的一句话都将是对我最大的激励。
简单一句话先来了解下JDBC:
JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口组成。
说白了就是实现 java和数据库的连接。
附上例子:
先让大家看下我的路径:
看到这么多的类屏幕前的你是不是被吓到了?
其实也不难。
放代码!
(以下代码建议按顺序观看)
Dept
package com.etc.entity;
//部门类
public class Dept {
private int deptNo; //部门号
private String deptName; //部门名
private String deptAddress; //部门地址
//构造方法
public Dept() {
super();
}
public Dept(String deptName, String deptAddress) {
super();
this.deptName = deptName;
this.deptAddress = deptAddress;
}
public Dept(int deptNo, String deptName, String deptAddress) {
super();
this.deptNo = deptNo;
this.deptName = deptName;
this.deptAddress = deptAddress;
}
//重写toString方法
@Override
public String toString() {
return "Dept [deptNo=" + deptNo + ", deptName=" + deptName + ", deptAddress=" + deptAddress + "]";
}
//get和set访问器
public int getDeptNo() {
return deptNo;
}
public void setDeptNo(int deptNo) {
this.deptNo = deptNo;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getDeptAddress() {
return deptAddress;
}
public void setDeptAddress(String deptAddress) {
this.deptAddress = deptAddress;
}
}
TestAdd
package com.etc.option;
//增方法
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* 第一个JDBC程序,向数据库中添加一条记录
* @author GGBOOM
*
*/
public class TestAdd {
public static void main(String[] args) throws ClassNotFoundException {
//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
//JDBC需要这个JAR包来完成我们对数据库的访问
//jar包其实就是很多的字节码文件
//2.连接数据库
//连接数据库的四个变量
String driver="com.mysql.jdbc.Driver";
//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
String url="jdbc:mysql://localhost:3306/myschool"; // 指JDBC连接方式; jdbc:mysql
//指你的本机地址; localhost
//SQL数据库的端口号; 3306
//要连接的数据库; myschool
String userName="root"; //账号
String userPwd="root";//密码
String sql="INSERT INTO dept(deptName,deptAddress) VALUES(?,?)";
Class.forName(driver);//加载驱动
Connection conn=null;
PreparedStatement pstmt=null;
try {
conn = DriverManager.getConnection(url, userName, userPwd); //可以使用DriverManager获取连接对象
pstmt=conn.prepareStatement(sql);//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
pstmt.setString(1,"公关部");
pstmt.setString(2,"金华");
//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
int result=pstmt.executeUpdate();
if(result>0) {
System.out.println("插入成功!");
}else {
System.out.println("插入失败!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(pstmt!=null) {
pstmt.close(); //关闭操作节省资源
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
TestDel
package com.etc.option;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* 第一个JDBC程序,向数据库中添加一条记录
* @author GGBOOM
*
*/
public class Testdel {
public static void main(String[] args) throws ClassNotFoundException {
//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
//JDBC需要这个JAR包来完成我们对数据库的访问
//jar包其实就是很多的字节码文件
//2.连接数据库
//连接数据库的四个变量
String driver="com.mysql.jdbc.Driver";
//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
String url="jdbc:mysql://localhost:3306/myschool";
String userName="root";
String userPwd="root";
String sql="delete from dept where deptNo=?";
//3.加载驱动
Class.forName(driver);
//可以使用DriverManager获取连接对象
Connection conn=null;
PreparedStatement pstmt=null;
try {
conn = DriverManager.getConnection(url, userName, userPwd);
//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, 6);
//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
int result=pstmt.executeUpdate();
if(result>0) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(pstmt!=null) {
pstmt.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
TestUpd
package com.etc.option;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* 第一个JDBC程序,向数据库中添加一条记录
* @author GGBOOM
*
*/
public class TestUpd {
public static void main(String[] args) throws ClassNotFoundException {
//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
//JDBC需要这个JAR包来完成我们对数据库的访问
//jar包其实就是很多的字节码文件
//2.连接数据库
//连接数据库的四个变量
String driver="com.mysql.jdbc.Driver";
//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
String url="jdbc:mysql://localhost:3306/myschool";
String userName="root";
String userPwd="root";
String sql="UPDATE DEPT SET deptAddress=? WHERE deptno=?";
//3.加载驱动
Class.forName(driver);
//可以使用DriverManager获取连接对象
Connection conn=null;
PreparedStatement pstmt=null;
try {
conn = DriverManager.getConnection(url, userName, userPwd);
//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,"金华");
pstmt.setInt(2, 5);
//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
int result=pstmt.executeUpdate();
if(result>0) {
System.out.println("修改成功!");
}else {
System.out.println("修改失败!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(pstmt!=null) {
pstmt.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
TestQuery
package com.etc.option;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 第一个JDBC程序,向数据库中添加一条记录
* @author GGBOOM
*
*/
public class TestQuery {
public static void main(String[] args) throws ClassNotFoundException {
//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
//JDBC需要这个JAR包来完成我们对数据库的访问
//jar包其实就是很多的字节码文件
//2.连接数据库
//连接数据库的四个变量
String driver="com.mysql.jdbc.Driver";
//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
String url="jdbc:mysql://localhost:3306/myschool";
String userName="root";
String userPwd="root";
String sql="SELECT * from dept";
//3.加载驱动
Class.forName(driver);
//可以使用DriverManager获取连接对象
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null; //结果集合
try {
conn = DriverManager.getConnection(url, userName, userPwd);
//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()) {
//下面两种凡是的区别,一般使用第二种方式,第一种方式如果我现在调换了列的位置,那么我们要去修改下面的代码
//第二种方式根据列名来获取值,那么我们不管怎么去修改列的位置,都不会影响到我们的取值
int deptNo=rs.getInt(1);//通过索引取值
String deptName=rs.getString("deptName");//通过列名
String deptAddress=rs.getString("deptAddress");
System.out.println("deptNo:"+deptNo+" deptName: "+deptName+" deptAddress: "+deptAddress);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(rs!=null) {
rs.close();
}
if(pstmt!=null) {
pstmt.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
BaseDao
package com.etc.dao;
//将增删改查这4个操作的共有语句整理进来
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BaseDao {
private static final String DRIVER="com.mysql.jdbc.Driver";
//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
private static final String URL="jdbc:mysql://localhost:3306/myschool";
private static final String USERNAME="root";
private static final String USERPWD="root";
//获取连接对象
public static Connection getConnection() {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
Connection conn=null;
try {
conn = DriverManager.getConnection(URL, USERNAME, USERPWD);
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//代表多个Object对象
public static PreparedStatement setParam(Connection conn,String sql,Object...param) throws SQLException{
PreparedStatement pstmt=null;
pstmt=conn.prepareStatement(sql);
for(int i=0;i<param.length;i++) {
pstmt.setObject(i+1, param[i]);
}
return pstmt;
}
//增删改的通用方法
public static int exeUpdate(PreparedStatement pstmt) throws SQLException {
int result=0;
//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
result=pstmt.executeUpdate();
return result;
}
public static void closeAll(Connection conn,PreparedStatement pstmt,ResultSet rs) {
try {
if(pstmt!=null) {
pstmt.close();
}
if(conn!=null) {
conn.close();
}
if(rs!=null) {
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
DeptDao
package com.etc.dao;
import java.util.List;
import com.etc.entity.Dept;
/**
* 部门Dao接口
* @author GGBOOM
*
*/
public interface DeptDao {
/**
* 添加
* @param dept
*/
void deptAdd(Dept dept);
/**
* 根据部门号删除
* @param deptNo
*/
void deptDel(int deptNo);
/**
* 根据部门号修改
* @param dept
*/
void deptUpdate(Dept dept);
/**
* 查询全部部门
* @return
*/
List<Dept> deptQuery();
}
DeptDaoImpl
package com.etc.dao.impl;
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 com.etc.dao.BaseDao;
import com.etc.dao.DeptDao;
import com.etc.entity.Dept;
public class DeptDaoImpl implements DeptDao {
@Override
public void deptAdd(Dept dept) {
String sql="INSERT INTO dept(deptName,deptAddress) VALUES(?,?)";
Connection conn=null;
PreparedStatement pstmt=null;
try {
conn = BaseDao.getConnection();
pstmt=BaseDao.setParam(conn,sql, dept.getDeptName(),dept.getDeptAddress());
//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
int result=BaseDao.exeUpdate(pstmt);
if(result>0) {
System.out.println("插入成功!");
}else {
System.out.println("插入失败!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
BaseDao.closeAll(conn,pstmt,null);
}
}
@Override
public void deptDel(int deptNo) {
//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
//JDBC需要这个JAR包来完成我们对数据库的访问
//jar包其实就是很多的字节码文件
//2.连接数据库
//连接数据库的四个变量
String driver="com.mysql.jdbc.Driver";
//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
String url="jdbc:mysql://localhost:3306/myschool";
String userName="root";
String userPwd="root";
String sql="delete from dept where deptNo=?";
//3.加载驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
//可以使用DriverManager获取连接对象
Connection conn=null;
PreparedStatement pstmt=null;
try {
conn = DriverManager.getConnection(url, userName, userPwd);
//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1, deptNo);
//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
int result=pstmt.executeUpdate();
if(result>0) {
System.out.println("删除成功!");
}else {
System.out.println("删除失败!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(pstmt!=null) {
pstmt.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public void deptUpdate(Dept dept) {
//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
//JDBC需要这个JAR包来完成我们对数据库的访问
//jar包其实就是很多的字节码文件
//2.连接数据库
//连接数据库的四个变量
String driver="com.mysql.jdbc.Driver";
//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
String url="jdbc:mysql://localhost:3306/myschool";
String userName="root";
String userPwd="root";
String sql="UPDATE DEPT SET deptAddress=? , deptName=? WHERE deptno=?";
//3.加载驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
//可以使用DriverManager获取连接对象
Connection conn=null;
PreparedStatement pstmt=null;
try {
conn = DriverManager.getConnection(url, userName, userPwd);
//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,dept.getDeptAddress());
pstmt.setString(2,dept.getDeptName());
pstmt.setInt(3, dept.getDeptNo());
//增删改都是对数据库的记录进行更改,他们都是使用executeUpdate这个方法来完成,返回值都是受影响行数
int result=pstmt.executeUpdate();
if(result>0) {
System.out.println("修改成功!");
}else {
System.out.println("修改失败!");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(pstmt!=null) {
pstmt.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
@Override
public List<Dept> deptQuery() {
//1.mysql-connector-java-5.1.39-bin.jar添加到构建路径
//JDBC需要这个JAR包来完成我们对数据库的访问
//jar包其实就是很多的字节码文件
//2.连接数据库
//连接数据库的四个变量
String driver="com.mysql.jdbc.Driver";
//如果使用的是jdbc:mysql://127.0.0.1:3306/myschool;那么我们要注意必须将配置里面的远程访问打开
String url="jdbc:mysql://localhost:3306/myschool";
String userName="root";
String userPwd="root";
String sql="SELECT * from dept";
List<Dept> list=new ArrayList<Dept>();
//3.加载驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
//可以使用DriverManager获取连接对象
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
try {
conn = DriverManager.getConnection(url, userName, userPwd);
//通过连接对象Connection得到一个PreparedStatement对象,他可以帮助我们完成针对数据库的增删查改操作
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()) {
//下面两种凡是的区别,一般使用第二种方式,第一种方式如果我现在调换了列的位置,那么我们要去修改下面的代码
//第二种方式根据列名来获取值,那么我们不管怎么去修改列的位置,都不会影响到我们的取值
int deptNo=rs.getInt(1);//通过索引取值
String deptName=rs.getString("deptName");//通过列名
String deptAddress=rs.getString("deptAddress");
System.out.println("deptNo:"+deptNo+" deptName: "+deptName+" deptAddress: "+deptAddress);
list.add(new Dept(deptNo,deptName,deptAddress));
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if(rs!=null) {
rs.close();
}
if(pstmt!=null) {
pstmt.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
TestDept
package com.etc.option;
import com.etc.dao.DeptDao;
import com.etc.dao.impl.DeptDaoImpl;
import com.etc.entity.Dept;
public class TestDept {
public static void main(String[] args) {
DeptDao dd=new DeptDaoImpl();//实例了对应的接口
//新增
Dept dept=new Dept("公关部","北京");
dd.deptAdd(dept);
//修改
// Dept dept=new Dept(7,"公关部1","上海");
// dd.deptUpdate(dept);
//删除
//dd.deptDel(7);
//查询
/* List<Dept> list=dd.deptQuery();
for(Dept dept1:list) {
System.out.println(dept1);
}*/
}
}
不懂的朋友仔细看看我的注释,我这注释写的相当良心了。
老铁,如果确实对你有帮助请点个免费的赞鼓励一下
白嫖不好,创作不易。各位的点赞就是我创作的最大动力,如果我有哪里写的不对,欢迎评论区留言进行指正,我们下篇文章见!