JAVA 编写的 医院管理系统,利用ACCESS做的数据库.
源代码在线查看: dbconnection.java~11~
package hospital.db;
import java.sql.*;
/**
* 本类用于与数据库建立接
*
* 作者:Fido Dido
*/
public abstract class DBConnection{
private static Connection conn=null;
private static String m_strErrMsg;
public static String getErrMsg()
{
return m_strErrMsg;
}
public static boolean TestConn()
{
try
{
if(conn == null){
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital","root","");
Debug.log("Connecion created.");
conn.close();
}
m_strErrMsg = "连接成功!";
}
catch(Exception ex)
{
// Debug.log(Debug.getExceptionMsg(ex));
m_strErrMsg = ex.getLocalizedMessage();
return false;
}
return true;
}
/**
* 与数据库建立连接
*
* 返回值-Connection对象
*/
public static Connection getConnection(){
try{
if(conn == null){
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital","root","");
Debug.log("Connecion created.");
}
else{
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT COUNT(*) FROM administrator");
if(rs==null||!rs.next()){
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/hospital","root","root");
Debug.log("Connecion re-created.");
}
}
}
catch(Exception ex){
Debug.log(Debug.getExceptionMsg(ex));
}
finally{
return conn;
}
}
}