社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtil {
//加载驱动
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取连接
private static Connection geConnection() {Connection con=null;
try {
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root",
"root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
//关闭资源
public void closeAll(
Connection con,
PreparedStatement ps,
ResultSet rs
) throws SQLException {
if (con!=null) {
con.close();
}
if (ps!=null) {
ps.close();
}
if (rs!=null) {
rs.close();
}
}
//输出结果
public static void main(String[] args) {
System.out.println(geConnection());
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!