Java实现CURD操作,最基础的版本
import org.junit.Test;
import java.sql.*;
public class JDBCDemo01 {
public static void main(String[] args) {
// testSelect();
// testInsert();
// testUpdate();
testDelete();
}
//删除
public static void testDelete(){
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
connection = DriverManager.getConnection(url, "root", "123456");
String sql = "delete from orderlist where id=9";
statement = connection.createStatement();
int row = statement.executeUpdate(sql); //进行添加行数,返回影响行数
System.out.println(row);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
statement.close();
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//修改
public static void testUpdate(){
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
connection = DriverManager.getConnection(url, "root", "123456");
String sql = "update orderlist set uid='100' where id=9";
statement = connection.createStatement();
int row = statement.executeUpdate(sql); //进行添加行数,返回影响行数
System.out.println(row);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
statement.close();
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//添加
public static void testInsert(){
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
connection = DriverManager.getConnection(url, "root", "123456");
String sql = "insert into orderlist (id, number, uid) values (5, 1005, 3)";
statement = connection.createStatement();
int row = statement.executeUpdate(sql); //进行添加行数,返回影响行数
System.out.println(row);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
try {
statement.close();
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//查询
public static void testSelect() {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
// 加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 创建连接
/*
* @param url a database url of the form
* <code>jdbc:<em>subprotocol</em>:<em>subname</em></code>
* @param user the database user on whose behalf the connection is being
* made
* @param password the user's password
* */
String url = "jdbc:mysql://localhost:3306/db1?useSSL=false&serverTimezone=UTC";
connection = DriverManager.getConnection(url, "root", "123456");
// 编写sql,创建statement,执行sql语句
String sql = "select * from orderlist";
statement = connection.createStatement();
resultSet = statement.executeQuery(sql);
// 查询返回结果,遍历打印
while (resultSet.next()){
int id = resultSet.getInt("id");
String number = resultSet.getString("number");
int uid = resultSet.getInt("uid");
System.out.println(id + " " + number + " " + uid);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
} finally {
// 关闭资源
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}