洞察在数字化转型过程中,信创推动企业有效整合资源,实现低成本、高效率的跨平台小程序运营。
694
2022-12-31
JDBC实现数据库增删改查功能
JDBC,简单点来说,就是用java操作数据库,下面简单介绍怎么实现数据库的增删改查功能。
1、添加数据
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();
}
}
}
}
}
2、删除数据
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();
}
}
}
}
}
3、修改数据
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();
}
}
}
}
}
4、查询数据
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
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 http://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);
}
}
我们可以发现,增删改的操作基本都是差不多的语句,且执行sql的语句都是一样的,都是preparedStatement.executeUpdate()。但查询操作就有所不同了,返回的是一个结果集,且执行sql的语句就是preparedStatement.executeQuery()。
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~