数据库连接类
本帖最后由 u012010949 于 2013-11-04 09:40:10 编辑 下面是我写的一个数据库连接类,但是有一个步骤不清楚是怎么回事,查资料弄到的
想问下,private static final String DRIVER = "com.mysql.jdbc.Driver";什么意思,是指路径还是每次都要这样写,数据库是MYSQL
数据库 jdbc java mysql
package com.henu.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DbConnection
{
private static final String DRIVER = "com.mysql.jdbc.Driver";//什么意思,是指路径还是?
private static final String URL = "jdbc:mysql://localhost:3306/zcbtext";
private static final String USER = "root";//mysql用户名
private static final String PASSWORD = "password";
public static synchronized Connection getConn()
{
Connection conn = null;
try
{
Class.forName(DRIVER);
conn = DriverManager.getConnection(URL, USER, PASSWORD);
}
catch (Exception e)
{
e.printStackTrace();
}
return conn;
}
public static synchronized void closeAll(ResultSet rs, Statement st,
Connection conn)
{
if (rs != null)
{
try
{
rs.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
if (st != null)
{
try
{
st.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
if (conn != null)
{
try
{
conn.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}