在IDEA上实现简单的JDBC的增删查改操作,总的分为以下几步:
1.加载驱动
2.创建链接
3.写sql语句
4.得到statement对象
5.执行sql语句
6.处理结果集
7.关闭资源
下面开始详细的讲代码构成:
1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
2.创建链接
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/datename?useSSL=true&characterEncoding=utf-8&user=root&password=root");
datename为要连接数据库的名称,user为数据库用户名,password为数据库密码
以上两步是最基础的语句,大部分要操作数据库的都需要写这两个语句。
实现查找
import java.sql.*;
public class Test {
public static void main(String[] args) {
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hjj?useSSL=true&characterEncoding=utf-8&user=root&password=123456");
System.out.println("创建连接成功");
//3.写sql
String sql = "select * from tb_user";
//4.得到statement对象
preparedStatement = connection.prepareStatement(sql);
//5.执行sql
resultSet = preparedStatement.executeQuery();
//6.处理结果集
while (resultSet.next()){
System.out.println(resultSet.getInt(1));
System.out.println(resultSet.getString(2));
System.out.println(resultSet.getString(3));
}
}catch (Exception e){
e.printStackTrace();
}finally {
//7.关闭资源
try{
connection.close();
}catch (Exception e){
e.printStackTrace();
}
try{
preparedStatement.close();
}catch (Exception e){
e.printStackTrace();
}
try{
resultSet.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
实现增加
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class Add {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hjj?useSSL=true&characterEncoding=utf-8&user=root&password=123456");
System.out.println("增加操作");
//3.写sql
String sql = "insert into tb_user(username,password) "+"values('hjj1',1234)";
//4.创建statement对象
stmt = conn.createStatement();
//5.执行sql
stmt.executeUpdate(sql);
//6.处理结果集
System.out.println("成功插入数据");
}catch (Exception e ){
e.printStackTrace();
}finally {
try{
stmt.close();
conn.close();
}catch (Exception e ){
e.printStackTrace();
}
}
}
}
实现修改
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class change {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hjj?useSSL=true&characterEncoding=utf-8&user=root&password=123456");
System.out.println("修改");
//4.创建statement对象
stmt = conn.createStatement();
//3.写sql
String sql = "update tb_user set username='hhh' where username='hjj'";
//5.执行sql
stmt.executeUpdate(sql);
//6.处理结果集
System.out.println("修改成功");
}catch (Exception e ){
e.printStackTrace();
}finally {
try{
stmt.close();
conn.close();
}catch (Exception e ){
e.printStackTrace();
}
}
}
}
实现删除
import java.awt.desktop.SystemEventListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
public class delete {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/hjj?useSSL=true&characterEncoding=utf-8&user=root&password=123456");
System.out.println("删除");
//4.创建statement对象
stmt = conn.createStatement();
//3.写sql
String sql = "delete from tb_user where username='hhh'";
//5.执行sql
stmt.executeUpdate(sql);
//6.处理结果集
System.out.println("成功删除");
}catch (Exception e ){
e.printStackTrace();
}finally {
try{
stmt.close();
conn.close();
}catch (Exception e ){
e.printStackTrace();
}
}
}
}
在IDEA上实现简单的JDBC增删查改大部分代码都是类似的
,只需要修改SQL语句即可。