mysql版本 8.0.29 jar包与之对应
往mysql中account表中添加一个记录
不抛异常写法:
package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbcDemo02 {
public static void main(String[] args) {
Statement stmt = null;
Connection conn = null;
try {
//1注册驱动
//8.0版本与5.7写法不一样 这里是8.0版本
Class.forName("com.mysql.cj.jdbc.Driver");
//2定义sql
String sql = "insert into account values(null,'王五',1000)";
//3获取Connection对象
//8.0版本!!!
conn = DriverManager.getConnection("jdbc:mysql:///db3?serverTimezone=UTC", "root", "root");
//4获取执行sql的对象 Statement
stmt = conn.createStatement();
//5执行sql
int count = stmt.executeUpdate(sql);//影响的行数
//6处理结果
System.out.println(count);
if(count>0){
System.out.println("添加成功!");
}else {
System.out.println("添加失败!");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
//释放资源
//避免空指针异常
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
修改表中记录
package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbcDemo03 {
public static void main(String[] args) {
Statement stmt = null;
Connection conn = null;
//1加载驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//2定义sql
String sql = "update account set balance =1000 where id =7";
//3获取Connection对象
conn = DriverManager.getConnection("jdbc:mysql:///db3?serverTimezone=UTC", "root", "root");
//4获取执行sql的对象 Statement
stmt = conn.createStatement();
//5执行sql
int count = stmt.executeUpdate(sql);//影响的行数
//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 {
//释放资源
//避免空指针异常
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
删除表中记录
package cn.itcast.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class jdbcDemo04 {
public static void main(String[] args) {
Statement stmt = null;
Connection conn = null;
//1加载驱动
try {
Class.forName("com.mysql.cj.jdbc.Driver");
//2定义sql
String sql = "DELETE FROM ACCOUNT WHERe id = 8";
//3获取Connection对象
conn = DriverManager.getConnection("jdbc:mysql:///db3?serverTimezone=UTC", "root", "root");
//4获取执行sql的对象 Statement
stmt = conn.createStatement();
//5执行sql
int count = stmt.executeUpdate(sql);//影响的行数
//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 {
//释放资源
//避免空指针异常
if(stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}



