从事数据库工作多年,目前看好分布式NeSQL/HTAP数据库在企业客户市场的发展。未来的主要方向是——致力于 NewSQL/HTAP 数据库的推广普及。
分类: Sybase
2014-01-26 15:27:51
package net.db.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
/**
* JDBC Sample.
*
* @author wangbing
* @version CVS $Revision$ $Date$
*/
public class JDBCDemoIQ {
// jConnect Driver
//private static final String DRIVER = "com.sybase.jdbc3.jdbc.SybDriver"; //jdbc 3.0
//private static final String DRIVER = "com.sybase.jdbc4.jdbc.SybDriver"; //jdbc 4.0
//private static final String URL = "jdbc:sybase:Tds:192.168.0.199:2638/iqdemo";
// iAnywhere Driver for sqlanywhere 12.0.1(IQ 15.4 Client) and sqlanywhere 16(IQ 16 Client)
//private static final String DRIVER = "sybase.jdbc.sqlanywhere.IDriver"; //jdbc 3.0
private static final String DRIVER = "sybase.jdbc4.sqlanywhere.IDriver"; //jdbc 4.0
private static final String URL = "jdbc:sqlanywhere:eng=HAIQ-DB-01_iqdemo;links=tcpip(host=192.168.0.199;port=2638)";
// iAnywhere Driver for sqlanywhere 11.0.1(IQ 15.2 Client)
//private static final String DRIVER = "ianywhere.ml.jdbcodbc.jdbc3.IDriver"; //jdbc 3.0
//private static final String DRIVER = "ianywhere.ml.jdbcodbc.jdbc4.IDriver"; //jdbc 3.0
//private static final String URL = "jdbc:ianywhere:driver=SQL Anywhere 11;eng=HAIQ-DB-01_iqdemo;links=tcpip(host=192.168.0.199;port=2638)";
public JDBCDemoIQ() {
}
public static void main(String[] args) {
JDBCDemoIQ demo = new JDBCDemoIQ();
try {
demo.select();
} catch (Exception e) {
e.printStackTrace();
}
}
public void select() throws Exception {
Connection con = this.getConnection(DRIVER, URL, "DBA", "sql");
PreparedStatement pstmt = con
.prepareStatement("select id, GivenName || Surname as name from Customers where Surname like 'A%'");
ResultSet rs = pstmt.executeQuery();
this.processResult(rs);
this.closeConnection(con, pstmt);
}
private void processResult(ResultSet rs) throws Exception {
if (rs.next()) {
ResultSetMetaData rsmd = rs.getMetaData();
int colNum = rsmd.getColumnCount();
for (int i = 1; i <= colNum; i++) {
if (i == 1) {
System.out.print(rsmd.getColumnName(i));
} else {
System.out.print("\t" + rsmd.getColumnName(i));
}
}
System.out.print("\n");
System.out.println("-----------------------");
do {
for (int i = 1; i <= colNum; i++) {
if (i == 1) {
System.out.print(rs.getString(i));
} else {
System.out.print("\t"
+ (rs.getString(i) == null ? "null" : rs
.getString(i).trim()));
}
}
System.out.print("\n");
} while (rs.next());
} else {
System.out.println("query not result.");
}
}
private Connection getConnection(String driver, String url, String user,
String password) throws Exception {
Class.forName(driver);
return DriverManager.getConnection(url, user, password);
}
private void closeConnection(Connection con, Statement stmt)
throws Exception {
if (stmt != null) {
stmt.close();
}
if (con != null) {
con.close();
}
}
}