如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
参考文章:https://blog.csdn.net/qq_40718312/article/details/95489257
maven jar包查询地址:https://search.maven.org/
引入mysql jar
<code class=""><dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</code>
增
<code class="">package net.jdbc.test;
import java.sql.*;
public class Add {
public static void main(String[] args) {
ResultSet rs = null;
PreparedStatement statement = null;
Connection connection = null;
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
connection = DriverManager.getConnection
("jdbc:mysql://120.78.193.246:3306/jdd?useSSL=true&" +
"characterEncoding=utf-8&user=" +
"jdd&password=fEh4mJFPRG87z3k4");
System.out.println("创建连接成功");
//3.写sql
String sql = "insert into bookinfo(book_name,price,public_date,store) values(?,?,?,?)";
//4.得到statement对象
statement = connection.prepareStatement(sql);
//5.执行sql得到结果集
statement = connection.prepareStatement(sql);
//6.处理结果集,插入数据
statement.setString(1, "Rose");
statement.setString(2, "123");
statement.setString(3, "2019-02-01");
statement.setString(4, "1");
statement.executeUpdate();
System.out.println("插入成功!");
//7.关闭资源
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
</code>
删
<code class="">package net.jdbc.test;
import java.sql.*;
public class Del {
public static void main(String[] args) {
ResultSet rs = null;
PreparedStatement statement = null;
Connection connection = null;
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
connection = DriverManager.getConnection
("jdbc:mysql://120.78.193.246:3306/jdd?useSSL=true&" +
"characterEncoding=utf-8&user=" +
"jdd&password=fEh4mJFPRG87z3k4");
System.out.println("创建连接成功");
//3.写sql
String sql = "delete from bookinfo where book_id=?";
//4.得到statement对象
statement = connection.prepareStatement(sql);
//5.执行sql得到结果集
statement.setInt(1,2);
statement.executeUpdate();
System.out.println("删除成功!");
//7.关闭资源
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
</code>
改
<code class="">package net.jdbc.test;
import java.sql.*;
public class Update {
public static void main(String[] args) {
ResultSet rs = null;
PreparedStatement statement = null;
Connection connection = null;
try {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");
//2.创建连接
connection = DriverManager.getConnection
("jdbc:mysql://120.78.193.246:3306/jdd?useSSL=true&" +
"characterEncoding=utf-8&user=" +
"jdd&password=fEh4mJFPRG87z3k4");
System.out.println("创建连接成功");
//3.写sql
String sql = "update bookinfo set book_name=?,store=? where book_id=?";
//4.得到statement对象
statement = connection.prepareStatement(sql);
//5.执行sql得到结果集
statement = connection.prepareStatement(sql);
//6.处理结果集,插入数据
statement.setString(1,"abc");
statement.setString(2,"789");
statement.setInt(3,1);
statement.executeUpdate();
System.out.println("修改成功!");
//7.关闭资源
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
</code>
查
<code class="">package net.jdbc.test;
import java.math.BigDecimal;
import java.sql.*;
public class Select {
//数据库url、用户名和密码
static final String DB_URL="jdbc:mysql://120.78.193.246:3306/jdd?";
static final String USER="jdd";
static final String PASS="fEh4mJFPRG87z3k4";
public static void main(String[] args) {
try {
//1、注册JDBC驱动
Class.forName("com.mysql.jdbc.Driver");
//2、获取数据库连接
Connection connection = DriverManager.getConnection(DB_URL, USER, PASS);
//3、操作数据库
Statement statement = connection.createStatement();//获取操作数据库的对象
String sql="select * from bookinfo";
ResultSet resultSet = statement.executeQuery(sql);//执行sql,获取结果集
while(resultSet.next()){ //遍历结果集,取出数据
int book_id = resultSet.getInt("book_id");
String book_name = resultSet.getString("book_name");
BigDecimal price = resultSet.getBigDecimal("price");
Date public_date = resultSet.getDate("public_date");
String store = resultSet.getString("store");
//输出数据
System.out.print("图书编号:"+book_id);
System.out.print(",图书名称:"+book_name);
System.out.print(",价格"+price);
System.out.print(",出版日期"+public_date);
System.out.print(",库存"+store);
System.out.println();
}
//4、关闭结果集、数据库操作对象、数据库连接
resultSet.close();
statement.close();
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
}
}
</code>