2013年11月4日星期一

Database connection class

 This post last edited by the u012010949 on 2013-11-04 09:40:10
Here is what I wrote a database connection class , but there is a step not clear how it is, check the information to get the
would like to ask , private static final String DRIVER = "com.mysql.jdbc.Driver"; What meaning is that every path or written this way , the database is 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();
}
}
}
}

------ Solution ------------------------------------- -------
com.mysql.jdbc.Driver
This is the calling driver names , mysql every time such written
------ Solution -------------------- ------------------------
can be understood as a kind of database driver name corresponding to one kind
- ---- Solution -------------------------------------------- < br> this is the mysql driver name , different databases corresponding to different drive name.
------ For reference only -------------------------------------- -
to load drivers

没有评论:

发表评论