DBConnection
package com.sql;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnection {
public static void main(String[] args) {
}
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/commodity?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
String user = "root";
String password = "2020";
public Connection conn;
public DBConnection() {
try {
// 加载驱动jar包
Class.forName(driver); // 反射:获取类的信息
conn = (Connection) DriverManager.getConnection(url, user, password);
// if(!conn.isClosed())
// System.out.println("Succeeded connecting to the Database!");
} catch (Exception e) {
e.printStackTrace();
}
}
public void close() {
try {
this.conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
MySqlUtil
package com.sql;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class MySqlUtil {
static String url = "jdbc:mysql://localhost:3306/car?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=false";
static String user = "root";
static String password = "2020";
public static void main(String[] args) {
delete();
}
/**
* 数据的查询
*/
public static void search() {
// 1. 加载驱动
try {
Class.forName("com.mysql.jdbc.Driver");
// 2.创建链接
Connection connection = (Connection) DriverManager.getConnection(url,user,password);
// 3.创建Statement对象
Statement statement = (Statement) connection.createStatement();
// 4.执行sql
String sql = "select * from student";
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
String id = resultSet.getString("id");
String name = resultSet.getString("name");
String idCard = resultSet.getString("idCard");
String phone = resultSet.getString("phone");
System.out.println(id + " " + name + " " + idCard + " " + phone);
}
if (resultSet !=null) {
resultSet.close();
}
if (statement !=null) {
statement.close();
}
if (connection !=null) {
connection.close();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 数据的插入
* @return
*/
public static int insert() {
int i=0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = (Connection) DriverManager.getConnection(url,user,password);
// 3.创建Statement对象
Statement statement = (Statement) connection.createStatement();
String sql = "insert into student (name,idCard,phone,height) values ('是是是','57892','1289312650','180')";
statement.executeUpdate(sql);
if (statement !=null) {
statement.close();
}
if (connection !=null) {
connection.close();
}
i = 1;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
public static int update() {
int i = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = (Connection) DriverManager.getConnection(url,user,password);
// 3.创建Statement对象
Statement statement = (Statement) connection.createStatement();
String sql = "update student set name = '李胜利' where id = 1" ;
statement.executeUpdate(sql);
if (statement !=null) {
statement.close();
}
if (connection !=null) {
connection.close();
}
i = 1;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
public static int delete() {
int i = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = (Connection) DriverManager.getConnection(url,user,password);
// 3.创建Statement对象
Statement statement = (Statement) connection.createStatement();
String sql = "delete from student where id = 5" ;
statement.executeUpdate(sql);
if (statement !=null) {
statement.close();
}
if (connection !=null) {
connection.close();
}
i = 1;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
}