目录
增加案例
package com.yy.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* @Author 杨
* @CreateDate 2023-04-12 11:20
**/
public class InsertTest {
static String url="jdbc:mysql://localhost:3306/db01?serverTimezone=UTC";
static String user="root";
static String pwd="123sa";
static Connection conn= null;
static PreparedStatement ps= null;
public static void main(String[] args) {
String sql="insert into user() values(null,?,?,?,?,?)";
try {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
conn = DriverManager.getConnection(url, user, pwd);
//预加载Statement对象,数据库的操作对象
ps = conn.prepareStatement(sql);
//预处理加载 ? 问号
ps.setString(1, "付老板");
ps.setString(2, "12345");
ps.setInt(3, 23);
ps.setString(4, "男");
ps.setString(5, "长治");
//执行sql
int i = ps.executeUpdate();
if (i>0){
System.out.println("增加成功...");
}else {
System.out.println("增加失败...");
}
} catch (Exception e) {
System.out.println("异常信息:"+e);
}finally {
try {
//关流
ps.close();
conn.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
}
删除案例
package com.yy.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* @Author 杨
* @CreateDate 2023-04-12 11:20
**/
public class DeleteTest {
static String url="jdbc:mysql://localhost:3306/db01?serverTimezone=UTC";
static String user="root";
static String pwd="123sa";
static Connection conn= null;
static PreparedStatement ps= null;
public static void main(String[] args) {
String sql="delete from user where u_id=?";
try {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
conn = DriverManager.getConnection(url, user, pwd);
//预加载Statement对象,数据库的操作对象
ps = conn.prepareStatement(sql);
//预处理加载 ? 问号
ps.setInt(1, 5);
//执行sql
int i = ps.executeUpdate();
if (i>0){
System.out.println("删除成功...");
}else {
System.out.println("删除失败...");
}
} catch (Exception e) {
System.out.println("异常信息:"+e);
}finally {
try {
//关流
ps.close();
conn.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
}
修改案例
package com.yy.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* @Author 杨
* @CreateDate 2023-04-12 11:20
**/
public class UpdateTest {
static String url="jdbc:mysql://localhost:3306/db01?serverTimezone=UTC";
static String user="root";
static String pwd="123sa";
static Connection conn= null;
static PreparedStatement ps= null;
public static void main(String[] args) {
String sql="update user set u_name=?,u_address=? where u_id=?";
try {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
conn = DriverManager.getConnection(url, user, pwd);
//预加载Statement对象,数据库的操作对象
ps = conn.prepareStatement(sql);
//预处理加载 ? 问号
ps.setString(1, "杨111");
ps.setString(2, "店上村");
ps.setInt(3, 4);
//执行sql
int i = ps.executeUpdate();
if (i>0){
System.out.println("修改成功...");
}else {
System.out.println("修改失败...");
}
} catch (Exception e) {
System.out.println("异常信息:"+e);
}finally {
try {
//关流
ps.close();
conn.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
}
查询案例
package com.yy.test;
import java.sql.*;
/**
* @Author 杨
* @CreateDate 2023-04-12 11:20
**/
public class SelectTest {
static String url="jdbc:mysql://localhost:3306/db01?serverTimezone=UTC";
static String user="root";
static String pwd="123sa";
static Connection conn= null;
static PreparedStatement ps= null;
static ResultSet rs= null;
public static void main(String[] args) {
String sql="select * from user";
try {
//加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//获取连接
conn = DriverManager.getConnection(url, user, pwd);
//预加载Statement对象,数据库的操作对象
ps = conn.prepareStatement(sql);
//执行sql,返回结果集
rs = ps.executeQuery();
while (rs.next()){
int id = rs.getInt("u_id");
String name = rs.getString("u_name");
String pwd = rs.getString("u_pwd");
int age = rs.getInt("u_age");
String sex = rs.getString("u_sex");
String address = rs.getString("u_address");
System.out.println("编号:"+id+"\t"+
"姓名:"+name+"\t"+
"密码:"+pwd+"\t"+
"年龄:"+age+"\t"+
"性别:"+sex+"\t"+
"地址:"+address);
}
} catch (Exception e) {
System.out.println("异常信息:"+e);
}finally {
try {
//关流
rs.close();
ps.close();
conn.close();
} catch (SQLException e) {
System.out.println(e);
}
}
}
}