目录
1.概念
2.步骤
1.首先建立数据库和所需要进行操作的表
SQL代码如下:
2.连接数据库
代码入下:
3.执行SQL语句(查询为例)
完整代码展示:
4.删除表中的数据
完整代码展示:
5.更新表中的数据
1.概念
JDBC是一套接口(面向接口写实现类,能够解耦合,提高代码的扩展力)
因为每个数据库底层的实现原理是不同的,如果没有接口 访问不同数据库就要不同的代码
2.步骤
1.首先建立数据库和所需要进行操作的表
SQL代码如下:
drop table if exists student;
create table student(
id bigint primary key auto_increment,
sn varchar(5),
name varchar(255),
qq_mail varchar(255),
classes_id bigint,
foreign key (classes_id) references classes(id)
);
insert into student(sn, name, qq_mail, classes_id) values
('09982','黑旋风李逵','xuanfeng@qq.com',1),
('00835','菩提老祖',null,1),
('00391','白素贞',null,1),
('00031','许仙','xuxian@qq.com',1),
('00054','不想毕业',null,1),
('51234','好好说话','say@qq.com',2),
('83223','tellme',null,2),
('09527','老外学中文','foreigner@qq.com',2);
2.连接数据库
由于创建数据库连接,关闭连接,释放资源的代码都是固定的,所以我们可以考虑把这些操作封装在一个工具类中
数据库名称:java78
数据包名称:student
端口号:3306
用户名:root
密码:111111
代码入下:
package utils;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtail {
//先定义一个数据源对象
private static DataSource dataSource = null;
private static final String USER = "root";
private static final String PASSWORD = "111111";
private static final String URL = "jdbc:mysql://127.0.0.1:3306/java78?carscterEncoding=utf-8&useSSL=false";
//初始化数据源
static {
MysqlDataSource mysqlDataSource = new MysqlDataSource();
mysqlDataSource.setURL(URL);
mysqlDataSource.setUser(USER);
mysqlDataSource.setPassword(PASSWORD);
dataSource = mysqlDataSource;
}
//用private修饰构造方法,使外部不能new这个类的新对象
private DBUtail() {}
//获取数据库连接的方法
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
//关闭对象并释放资源
public static void close (ResultSet resultSet, PreparedStatement statement ,Connection connection
) {
if(resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(connection != null) {
try {
connection.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
}
3.执行SQL语句(查询为例)
完整代码展示:
import model.Student;
import utils.DBUtail;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Demo01_Connection {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
//建立数据库连接
try {
//通过数据源获取一个数据库连接
connection = DBUtail.getConnection();
//接受用户的值
System.out.println("请输入学号->");
Scanner scanner = new Scanner(System.in);
String sn = scanner.next();
//定义SQL语句
//根据学号查询
String sql = "select * from student where sn = ?";
System.out.println(sql);
// //获取Statement对象
// statement = connection.createStatement();
//获取一个预处理对象
statement = connection.prepareStatement(sql);
//处理占位符
statement.setString(1,sn);
//执行SQL
resultSet = statement.executeQuery();
//解析结果集
if(resultSet.next()) {
//创建结果集中的java对象
Student student = new Student();
//依次读取结果
student.setId(resultSet.getInt(1));
student.setSn(resultSet.getString(2));
student.setName(resultSet.getString(3));
student.setMail(resultSet.getString(4));
student.setClass_id(resultSet.getInt(5));
//打印结果
System.out.println(student);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//依次关闭资源
DBUtail.close(resultSet,statement,connection);
}
}
}
4.删除表中的数据
完整代码展示:
import utils.DBUtail;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class Demo02_Delete {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
try {
//获取数据库连接
connection = DBUtail.getConnection();
//构建SQL语句
String sql = "delete from student where name = ?";
//接收用户的输入
System.out.println("请输入要删除的同学姓名:");
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
//对SQL语句进行处理,并替换占位符
statement = connection.prepareStatement(sql);
statement.setString(1,name);
//执行SQL语句并且获取结果
int row = statement.executeUpdate();
if(row <= 0) {
System.out.println("删除失败");
} else {
System.out.println("删除成功");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtail.close(null,statement,connection);
}
}
}
5.更新表中的数据
import utils.DBUtail;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class Demo03_Insert {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBUtail.getConnection();
String sql = "insert into student(sn,name,qq_mail,classes_id) values (?,?,?,?)";
Scanner sc = new Scanner(System.in);
System.out.println("请输入学号");
String sn = sc.next();
System.out.println("请输入姓名");
String name = sc.next();
System.out.println("请输入邮箱");
String mail = sc.next();
System.out.println("请输入班级");
long classesid = sc.nextLong();
statement = connection.prepareStatement(sql);
statement.setString(1,sn);
statement.setString(2,name);
statement.setString(3,mail);
statement.setLong(4,classesid);
int row = statement.executeUpdate();
if(row <= 0) {
System.out.println("新增数据失败");
} else {
System.out.println("新增数据成功");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBUtail.close(null,statement,connection);
}
}
}
版权声明:本文为博主作者:小锦鲤yaw原创文章,版权归属原作者,如果侵权,请联系我们删除!
原文链接:https://blog.csdn.net/m0_57248981/article/details/130378160