java连接MySQl数据库实例代码
时间:2021-12-18 18:19:36
package com.abc.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class BaseDao {
public Connection getConn()
{
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String url="jdbc:mysql://127.0.0.1:3306/user";
try {
conn=DriverManager.getConnection(url, "root", "");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
public void closeAll(ResultSet rs,Statement stat,Connection conn)
{
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(stat!=null)
try {
stat.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(conn!=null)
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Cloud 请求重试机制核心代码分析
详解Java注解的实现与使用方法
IP查询系统的异步回调案例
MyBatis常用的jdbcType数据类型
SpringBoot 返回Json实体类属性大小写的解决
jvm调优的几种场景(小结)
![](https://img.aspxhome.com/file/2023/3/60323_0s.png)
SpringDataElasticsearch与SpEL表达式实现ES动态索引
![](https://img.aspxhome.com/file/2023/8/61558_0s.png)
Opencv EigenFace人脸识别算法详解
![](https://img.aspxhome.com/file/2023/5/101075_0s.png)
Java与C++分别用递归实现汉诺塔详解
![](https://img.aspxhome.com/file/2023/7/62237_0s.png)
SpringBoot利用限速器RateLimiter实现单机限流的示例代码
Java文件上传与文件下载实现方法详解
![](https://img.aspxhome.com/file/2023/5/59405_0s.png)
超全MyBatis动态代理详解(绝对干货)
![](https://img.aspxhome.com/file/2023/2/59082_0s.jpg)
SpringCloud实战之Zuul网关服务
![](https://img.aspxhome.com/file/2023/4/60544_0s.png)
详解Java中Period类的使用方法
保证缓存和数据库的数据一致性详解
![](https://img.aspxhome.com/file/2023/3/59573_0s.png)
Spring Security 强制退出指定用户的方法
Android 动画之TranslateAnimation应用详解
![](https://img.aspxhome.com/file/2023/1/82411_0s.png)
springmvc如何使用POJO作为参数
![](https://img.aspxhome.com/file/2023/1/61831_0s.png)