JDBC实现数据库增删改查功能
JDBC作为Java连接数据库的标准接口,允许开发者通过Java代码执行数据库操作。本文将简要阐述如何利用JDBC完成对数据库的基本增删改查任务。
一、数据插入操作
package cn.itcast.jdbc;
import java.sql.*;
public class JdbcDemo2 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2、定义sql
String sql = "insert into course values(?,?,?)";
//3、获取Connection对象
//student表示你要操作的数据库
//如果是locakhost:3306,也可以简写为"jdbc:mysql:///student"
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
//4、获取执行sql的对象
preparedStatement = connection.prepareStatement(sql);
//传入参数
preparedStatement.setInt(1,5);
preparedStatement.setString(2,"JavaWeb");
preparedStatement.setInt(3,88);
//5、执行sql
int count = preparedStatement.executeUpdate();
//6、处理结果
System.out.println(count);
if (count > 0) {
System.out.println("添加成功");
} else {
System.out.println("添加失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//7、释放资源
//避免空指针异常
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
二、数据删除操作
package cn.itcast.jdbc;
import java.sql.*;
public class JdbcDemo4 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2、获取连接对象
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student","root","root");
//3、定义sql
String sql = "delete from course where cno = ?";
//4、获取执行sql对象
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,5);
//5、执行sql
int count = preparedStatement.executeUpdate();
//6、处理结果
System.out.println(count);
if (count > 0) {
System.out.println("删除成功");
} else {
System.out.println("删除失败");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//7、释放资源
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
三、数据更新操作
package cn.itcast.jdbc;
import java.sql.*;
public class JdbcDemo3 {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2、获取连接对象
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root");
//3、定义sql
String sql = "update course set period = ? where cno = ?";
//4、获取执行sql对象
preparedStatement = connection.prepareStatement(sql);
//设置参数
preparedStatement.setInt(1,90);
preparedStatement.setInt(2,1);
//5、执行sql
int count = preparedStatement.executeUpdate();
//6、处理结果
System.out.println(count);
if (count > 0) {
System.out.println("修改成功!");
} else {
System.out.println("修改失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
//7、释放资源
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
四、数据查询操作
package cn.itcast.jdbc;
import cn.itcast.domain.Course;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class JDBCDemo5 {
/**
* 查询所有Course对象
* @return
*/
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
List list = null;
try {
//1、注册驱动
Class.forName("com.mysql.jdbc.Driver");
//2、获取连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "root", "root");
//3、定义sql
String sql = "select * from course";
//4、获取执行sql的对象
preparedStatement = connection.prepareStatement(sql);
//5、执行sql
resultSet = preparedStatement.executeQuery();
//6、遍历结果集,封装对象,装载集合
Course course = null;
list = new ArrayList();
while (resultSet.next()) {
//获取数据
int cno = resultSet.getInt("cno");
String cname = resultSet.getString("cname");
int period = resultSet.getInt("period");
//创建Course对象并赋值
course = new Course();
course.setCno(cno);
course.setCname(cname);
course.setPeriod(period);
//装载集合
list.add(course);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
System.out.println(list);
}
}
观察上述操作可以发现,插入、删除和修改在代码结构上高度相似,且都依赖于PreparedStatement对象的executeUpdate()方法来执行非查询类的SQL语句。相比之下,查询操作具有独特性,它需要调用executeQuery()方法,并返回一个包含查询结果的ResultSet对象。
以上内容涵盖了JDBC进行数据库核心操作的基本流程,希望能为读者的学习提供有益参考,同时也欢迎读者持续关注本站获取更多技术资讯。