package com.whx.util;
import com.mysql.cj.jdbc.Driver;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JDBC {
private static final String USER="root";
private static final String PASS="123456";
private static final String URL="jdbc:mysql://127.0.0.1:3306/protect";
//1.加载驱动:只加载一次
static{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//2.取得连接
private static Connection getConn(){
try {
return DriverManager.getConnection(URL,USER,PASS);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
System.out.println(getConn());
}
//3.编译sql语句
public static int update(String sql){
try {
PreparedStatement preparedStatement=getConn().prepareStatement(sql);
return preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;//出现异常返回0
}
}
数据库连接
最新推荐文章于 2024-08-20 13:24:05 发布