package ds.test;
/**
* @author wynnhjg
* manage the jdbc resource
*/
import java.sql.*;
public class DatabaseConn {
/**
* get the jdbc Connection
*/
static public Connection getConnection() {
String classforname = "net.sourceforge.jtds.jdbc.Driver";
String url ="jdbc:jtds:sqlserver://195.204.140.95:1433;DatabaseName=bemsdb";
String user = "sa";
String pwd = "cmbc";
try {
Class.forName(classforname);
Connection conn = DriverManager.getConnection(url, user, pwd);
return conn;
} catch (java.lang.ClassNotFoundException e) {
System.err.println("get connection error!");
System.err.println(e);
} catch (java.sql.SQLException e) {
System.err.println("get connection error!");
System.err.println(e);
}
// error,return null
return null;
}
/**
* get the RDBMS Product
*/
public static String DataBaseProductName() throws SQLException {
String productName = null;
Connection conn = getConnection();
DatabaseMetaData metaData = conn.getMetaData();
productName = metaData.getDatabaseProductName();
conn.close();
return productName;
}
/**
* close the jdbc resource
*/
public static void closeAll(Connection connection, Statement statement,
ResultSet resultSet) {
if (resultSet != null) {
try {
resultSet.close();
} catch (Exception exception) {
}
}
if (statement != null) {
try {
statement.close();
} catch (Exception exception) {
}
}
if (connection != null) {
try {
connection.close();
} catch (Exception exception) {
}
}
}
}
************************************************
package ds.test;
/**
* @author wynnhjg
* 数据源的测试
*/
import java.sql.*;
import ds.test.DatabaseConn;
public class DsTest {
public static void main(String args[]) {
Connection conn = null;
try {
conn = DatabaseConn.getConnection();
if (!(conn == null)) {
System.out.print("OK,fuck ok");
} else {
System.out.print("fail");
}
} finally {
DatabaseConn.closeAll(conn, null, null);
}
}
}