中国开发网: 论坛: 程序员情感CBD: 贴子 345650
wynnhjg
偶先贴.很烂.
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);
}

}
}

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录