不同工具连接数据库
Eclipse
-
导入mysql的jar包(到src同级的就可)
-
将jar包添加到引入库(构建路径)
-
连接数据库
测试代码:
package com.fl.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/**
* @author:FL
* @version: 2023年6月17日下午7:05:15
*/
public class JDBCTest03 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// 1. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 获取连接
String url="jdbc:mysql://localhost:3306/db_shop?serverTimezone=UTC ";
String user = "root";
String password = "123456";
conn=DriverManager.getConnection(url ,user,password);
// 3. 获取数据库操作对象
stmt = conn.createStatement();
// 4. 执行sql
String sql = "DELETE FROM supplier WHERE supplier_name = '只鹦你太美' ";
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? "删除成功":"删除失败");
} catch (Exception e) {
e.printStackTrace();
}finally {
if(stmt != null) {
try {
stmt.close();
} catch (Exception e2) {
// TODO: handle exception
}
if(conn != null) {
try {
conn.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
}
}
控制台:
Idea
-
导入mysql的jar包(到src同级的就可)
-
open Module setting(按F4或右键工程)
-
增加库(libraries)(点击加号添加)
-
选择库文件,确定
-
连接数据库
测试代码:
package com.fl.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
/**
* @author:FL
* @version: 2023年6月17日下午7:05:15
*/
public class JDBCTest01 {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try {
// 1. 注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 2. 获取连接
String url="jdbc:mysql://localhost:3306/db_shop?serverTimezone=UTC ";
String user = "root";
String password = "123456";
conn=DriverManager.getConnection(url ,user,password);
// 3. 获取数据库操作对象
stmt = conn.createStatement();
// 4. 执行sql
String sql = "DELETE FROM supplier WHERE supplier_name = '只鹦你太美' ";
int count = stmt.executeUpdate(sql);
System.out.println(count == 1 ? "删除成功":"删除失败");
} catch (Exception e) {
e.printStackTrace();
}finally {
if(stmt != null) {
try {
stmt.close();
} catch (Exception e2) {
// TODO: handle exception
}
if(conn != null) {
try {
conn.close();
} catch (Exception e2) {
// TODO: handle exception
}
}
}
}
}
}
控制台: