//java连接MySQl数据库实例代码
package com.abc.dao;
/*dkplus专业搜集和编写实用电脑软件教程,
*搜集各种软件资源和计算机周边,独立制作视频和ppt和音频微信公众号,
*点击进入 dkplus官方博客 (java网络编程http://dkplus.),
*微信搜索dkplus关注公众号可获取海量计算机周边资源。*/
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();
}
}
}