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("sql.jdbc.Driver");
//2、定义sql
String sql = "insert into course values(?,?,?)";
//3、获取Connection对象
c++图书管理系统源代码//student表⽰你要操作的数据库
//如果是locakhost:3306,也可以简写为"jdbc:mysql:///student"
connection = Connection("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 = uteUpdate();
//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("sql.jdbc.Driver");
/
/2、获取连接对象
connection = Connection("jdbc:mysql://localhost:3306/student","root","root");            //3、定义sql
String sql = "delete from course where cno = ?";
//4、获取执⾏sql对象
c语言程序教程视频下载preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,5);
//5、执⾏sql
int count = uteUpdate();
//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("sql.jdbc.Driver");
//2、获取连接对象
connection = Connection("jdbc:mysql://localhost:3306/student", "root", "root");            //3、定义sql
String sql = "update course set period = ? where cno = ?";
//4、获取执⾏sql对象
access是一种什么数据库preparedStatement = connection.prepareStatement(sql);
//设置参数
preparedStatement.setInt(1,90);
preparedStatement.setInt(2,1);
//5、执⾏sql
int count = uteUpdate();
//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;
brpc githubResultSet resultSet = null;
List<Course> list = null;
try {
//1、注册驱动
Class.forName("sql.jdbc.Driver");
//2、获取连接
connection = Connection("jdbc:mysql://localhost:3306/student", "root", "root");            //3、定义sql
String sql = "select * from course";
//4、获取执⾏sql的对象
preparedStatement = connection.prepareStatement(sql);
//5、执⾏sql
resultSet = uteQuery();
//6、遍历结果集,封装对象,装载集合
Course course = null;
list = new ArrayList<Course>();
while (()) {
//获取数据
int cno = Int("cno");
String cname = String("cname");
int period = Int("period");
//创建Course对象并赋值
course = new Course();
course.setCno(cno);
course.setCname(cname);
course.setPeriod(period);
//装载集合
list.add(course);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
asp与php区别
} 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);
}
}mysql面试题 增删改查
我们可以发现,增删改的操作基本都是差不多的语句,且执⾏sql的语句都是⼀样的,都是uteUpdate()。但查询操作就有所不同了,返回的是⼀个结果集,且执⾏sql的语句就是uteQuery()。
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。