package com.hisoft.test.jdbc;
import com.hisoft.epetshop.util.JdbcUtil;
import java.sql.*;
/**
* @program: epetShop-01
* @description:
* @author: wlg
* @create: 2020-10-13 08:18:12
**/
public class TestJdbc {
public static void query() {
final Connection connection = JdbcUtil.getConnection();
//3.创建Statement或者PreparedStatement对象
Statement stmt = null;
ResultSet rs = null;
try {
stmt = connection.createStatement();
//4.执行SQL语句
rs = stmt.executeQuery("select * from petowner");
while (rs.next()) {
int id = rs.getInt(1);
String name = rs.getString(2);
String password = rs.getString(3);
int money = rs.getInt(4);
System.out.println(id + "\t" + name + "\t" + password + "\t" + money);
}
} catch (SQLException e) {
e.printStackTrace();
}
//6.释放资源--Connection,Statement,ResultSet
JdbcUtil.closeAll(connection, stmt, rs);
}
public static void add() {
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2.获取连接
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql:///epetshop", "root", "root");
System.out.println("连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
//3.创建Statement或者PreparedStatement对象
Statement stmt = null;
try {
stmt = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
//4.执行SQL语句,增删改操作都使用executeUpdate方法,返回受影响的条数
try {
int count = stmt.executeUpdate("insert into petowner(name,password,money) values('郭靖','123456',300)");
System.out.println("成功添加了" + count + "条记录");
} catch (SQLException e) {
e.printStackTrace();
}
//5.释放资源
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void update() {
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2.获取连接
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql:///epetshop", "root", "root");
System.out.println("连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
//3.创建Statement或者PreparedStatement对象
Statement stmt = null;
try {
stmt = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
//4.执行SQL语句,增删改操作都使用executeUpdate方法,返回受影响的条数
try {
int count = stmt.executeUpdate("update petowner set name = '黄蓉',password = '654321',money = 500 where id = 3");
System.out.println("成功修改了" + count + "条记录");
} catch (SQLException e) {
e.printStackTrace();
}
//5.释放资源
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void delete() {
//1.加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
//2.获取连接
Connection connection = null;
try {
connection = DriverManager.getConnection("jdbc:mysql:///epetshop", "root", "root");
System.out.println("连接成功");
} catch (SQLException e) {
e.printStackTrace();
}
//3.创建Statement或者PreparedStatement对象
Statement stmt = null;
try {
stmt = connection.createStatement();
} catch (SQLException e) {
e.printStackTrace();
}
//4.执行SQL语句,增删改操作都使用executeUpdate方法,返回受影响的条数
try {
int count = stmt.executeUpdate("delete from petowner where id = 3");
System.out.println("成功删除了" + count + "条记录");
} catch (SQLException e) {
e.printStackTrace();
}
//5.释放资源
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
// query();
// add();
// update();
delete();
}
}