EsgynDB 简单步骤测试JDBC能否连通数据库
有时候我们会发现某个应用通过JDBC接口无法连通数据库,但从其他环境中是可以正常连接目标数据库的,为了排除是应用本身的问题,我们可以构建一个简单的JDBC测试程序来验证机器到数据库的连通性。主要步骤为:
- 编写简单的Java测试程序TestConnEsg.java,内容如下
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Calendar;
public class TestConnEsg {
protected static String url = "jdbc:t4jdbc://10.19.41.65:23400,10.19.41.66:23400,10.19.41.67:23400/:schema=v7dev;maxStatements=400;connectionTimeout=0;clipVarchar=1";
private static String usr = "trafodion";
private static String pwd = "traf123";
private static final String driver = "org.trafodion.jdbc.t4.T4Driver";
private static Connection conn = null;
private static Statement stmt = null;
private static ResultSet rs = null;
public static void main(String[] args) throws Exception {
try {
Class.forName(driver);
println_message("loaded driver");
println_message("jdbc url [" + url + "]");
try {
conn = DriverManager.getConnection(url, usr, pwd);
} catch (Exception e) {
e.printStackTrace();
}
println_message("get success connection");
conn.setAutoCommit(false);
PreparedStatement stmt = conn.prepareStatement("select sysdate from dual");
rs = stmt.executeQuery();
conn.commit();
conn.close();
} finally {
if (conn != null) {
conn.close();
}
}
}
public static void println_message(String msg) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(format.format(new Date()) + " -> " + msg);
}
}
- 在应用服务器所在的机器上javac编译代码,生成.class文件
javac TestConnEsg.java
准备EsgynDB数据库对应版本驱动包
使用java命令测试JDBC代码
(注:需要使用-cp引入依赖包)
java -cp .:jdbcT4-1.6.1release.jar TestConnEsg
相关文章