//导入包
import java.sql.*;
import java.util.Scanner;
/*
* 数据库连接
* @ GL-stone
*/
public class Test {
//声明 Connection 对象
private static Connection con;
private static PreparedStatement pStmt;
//声明预处理 PreparedStatement 对象
private static ResultSet res;
//声明结果 ResultSet 对象
//mysql --local-infile=1 -u root -p
private static final String url = "jdbc:mysql://localhost:3306/test";
// 协议:子协议://目标IP地址:端口/数据库 在这里test是之前创建的数据库名
private static final String user = "root";
private static final String password = "jk11200211";
private static String tableName;
//————与数据库创建连接
public Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("数据库驱动加载成功");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
con=DriverManager.getConnection(url,user,password);
System.out.println("数据库连接成功");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return con;
}
public static void main(String[] args) {
Test h = new Test();
con = h.getConnection();
String str;
java通过jdbc对数据库进行简单的增删改查语句
最新推荐文章于 2023-07-12 00:20:06 发布