Java通过JDBC连接数据库
Java常用的操作数据库的方法
本次使用的开发工具为eclipse,所需的数据库驱动为mysql-connector-java-5.1.7-bin.jar。
点此下载驱动
前期准备工作
打开eclipse新建一个项目,在该项目下新建一个名为lib的文件夹,然后将mysql-connector-java-5.1.7-bin.jar复制到该目录下,然后右键Build Path ---> Add to Build Path即可。
代码编写
- 获得数据库连接的方法:
public static Connection getConn(){
Connection conn = null;
try{
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获得连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbbook","root","123456");
}catch (Exception e){
e.printStackTrace();
}
return conn;
}
- 释放数据库连接资源的方法(三个参数)
public static void releaseResources(ResultSet rs,Statement stmt,Connection conn){
if(rs != null){
try{
rs.close();
}catch (Exception e){
e.printStackTrace();
}
rs = null;
}
if(stmt != null){
try{
stmt.close();
}catch (Exception e){
e.printStackTrace();
}
stmt = null;
}
if(conn != null){
try{
conn.close();
}catch (Exception e){
e.printStackTrace();
}
conn = null;
}
}
- 释放资源的重载方法(两个参数)
public static void releaseResources(Statement stmt,Connection conn){
if(stmt != null){
try{
stmt.close();
}catch (Exception e){
e.printStackTrace();
}
stmt = null;
}
if(conn != null){
try{
conn.close();
}catch (Exception e){
e.printStackTrace();
}
conn = null;
}
}
- 查询数据库表中所有信息的方法
public static void getTable(){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
//获取连接
conn = getConn();
//编写SQL语句
String sql = "select * from books";
//预编译SQL
stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery();
while(rs != null){
int id = rs.getInt("id");//单条记录中的id字段
String name = rs.getString("name");//单条记录中的name字段
double price = rs.getDouble("price");//单条记录中的price字段
String author = rs.getString("author");//单条记录中的author字段
String type = rs.getString("chuBanShe");//单条记录中的chuBanShe字段
int num = rs.getInt("counts");//单条记录中的counts字段
System.out.println(id + " " + name + " " + price + " " + author + " " + type + " " + num);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//释放资源
releaseResources(rs,stmt,conn);
}
}
- 带参查询方法
public static void getTable(String bookName,String bookAuthor){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try{
//获得连接
conn = getConn();
//编写SQL语句
String sql = "select * from books where name = ? and author = ?";
//预编译SQL
stmt = conn.prepareStatement(sql);
//设置参数
stmt.setString(1,bookName);
stmt.setString(2,bookAuthor);
rs = stmt.executeQuery();
while(rs != null){
int id = rs.getInt("id");//单条记录中的id字段
String name = rs.getString("name");//单条记录中的name字段
double price = rs.getDouble("price");//单条记录中的price字段
String author = rs.getString("author");//单条记录中的author字段
String type = rs.getString("chuBanShe");//单条记录中的chuBanShe字段
int num = rs.getInt("counts");//单条记录中的counts字段
System.out.println(id + " " + name + " " + price + " " + author + " " + type + " " + num);
}
}catch (Exception e){
e.printStackTrace();
}finally {
//释放资源
releaseResources(rs,stmt,conn);
}
}
- 向数据库表中插入信息的方法
public static void insertTable(){
Connection conn = null;
PreparedStatement stmt = null;
try{
//获得连接
conn = getConn();
//编写SQL语句
String sql = "insert into books values (null,'青铜葵花',36,'曹文轩','人民出版社',16)";
//预编译SQL
stmt = conn.prepareStatement(sql);
//执行SQL语句
int i = stmt.executeUpdate();//返回受影响的记录条数
if(i > 0){
System.out.println("插入成功!");
}
}catch (Exception e){
e.printStackTrace();
}finally {
//释放资源
releaseResources(stmt,conn);
}
}
- 在数据库表中更改信息的方法
public static void updateTable(){
Connection conn = null;
PreparedStatement stmt = null;
try{
//获得连接
conn = getConn();
//编写SQL语句,将id为1的记录信息更换
String sql = "update books set name = '三国演义',price = 26,author = '罗贯中',chuBanShe = '人民出版社',counts = 16 where id = 1";
//预编译SQL
stmt = conn.prepareStatement(sql);
//执行SQL语句
int i = stmt.executeUpdate();//返回受影响的记录条数
if(i > 0){
System.out.println("修改成功!");
}
}catch (Exception e){
e.printStackTrace();
}finally {
//释放资源
releaseResources(stmt,conn);
}
}
- 在数据库表中删除信息的方法
public static void deleteTable(){
Connection conn = null;
PreparedStatement stmt = null;
try{
//获得连接
conn = getConn();
//编写SQL语句,将id为1的记录删除
String sql = "delete from books where id = 1";
//预编译SQL
stmt = conn.prepareStatement(sql);
//执行SQL语句
int i = stmt.executeUpdate();//返回受影响的记录条数
if(i > 0){
System.out.println("删除成功!");
}
}catch (Exception e){
e.printStackTrace();
}finally {
//释放资源
releaseResources(stmt,conn);
}
}
JDBC的API
DriverManager:驱动管理类
- 主要作用:
- 一、注册驱动
Class.forName("com.mysql.jdbc.Driver"); - 二、获得连接
Connection getConnection(String url,String username,String password);- url的写法:
jdbc:mysql://localhost:3306/数据库名- jdbc:协议
- mysql:子协议
- localhost:主机名
- 3306:端口号
- url的简写:
jdbc:mysql:///数据库名
- url的写法:
- 一、注册驱动
Connection:连接对象
- 主要作用:
- 一、创建执行SQL语句的对象
- Statement createStatement() :执行SQL语句,有SQL注入的漏洞存在。
- PreparedStatement prepareStatement(String sql) :预编译SQL语句,解决SQL注入的漏洞。
- CallableStatement prepareCall(String sql) :执行SQL中存储过程。
- 二、进行事务的管理
- setAutoCommit(boolean autoCommit) :设置事务是否自动提交
- commit() :事务提交
- rollback() :事务回滚
- 一、创建执行SQL语句的对象
Statement:执行SQL
- 主要作用:
- 一、执行SQL语句
- boolean execute(String sql) :执行SQL,执行select语句返回true否则返回false。
- ResultSet executeQuery(String sql) :执行SQL中的select语句。
- int executeUpdate(String sql) :执行SQL中的insert、update、delete语句。
- 二、执行批处理操作
- addBatch(String sql) :添加到批处理
- executeBatch() :执行批处理
- clearBatch() :清空批处理
- 一、执行SQL语句

浙公网安备 33010602011771号