本文主要是介绍JAVA连接sybase数据库程序代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
JAVA连接sybase数据库程序代码
功能:JAVA连接sybase数据库程序代码 需加入 两个jar 包
test.java:
package com.sam;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
public class test {
public static void main(String[] args) throws SQLException, Exception {
String queryString = "SELECT";
queryString += " YEAR(T.SII_DATE) AS SII_YEAR,";
queryString += " MONTH(T.SII_DATE) AS SII_MONTH,";
queryString += " DAY(T.SII_DATE) AS SII_DAY,";
queryString += " YEAR(T.OTC_DATE) AS OTC_YEAR,";
queryString += " MONTH(T.OTC_DATE) AS OTC_MONTH,";
queryString += " DAY(T.OTC_DATE) AS OTC_DAY,";
queryString += " YEAR(T.ROTC_DATE) AS ROTC_YEAR,";
queryString += " MONTH(T.ROTC_DATE) AS ROTC_MONTH,";
queryString += " DAY(T.ROTC_DATE) AS ROTC_DAY,";
queryString += " T.COMPANY_ID,";
queryString += " T.COMPANY_NAME,";
queryString += " T.CHAIRMAN_NAME,";
queryString += " F.CAPITAL_AMT,";
queryString += " T.BROKER_NO,";
queryString += " T.BROKER_OTHERS,";
queryString += " T.GREAT_DIF";
queryString += " FROM T68GUID_FMCBIK T";
queryString += " INNER JOIN FMCBIK F";
queryString += " ON F.COMPANY_ID=T.COMPANY_ID";
queryString += " WHERE F.LISTING_DATE !=0";
queryString += " AND F.LAST_DATE =0";
queryString += " AND (F.CODE = '91' OR (F.CODE !='91' AND F.FIN_CODE != '') )";
queryString += " AND (T.SII_DATE is not null and T.SII_DATE!='0')";
queryString += " AND (T.OTC_DATE is not null and T.OTC_DATE!='0')";
queryString += " AND (T.ROTC_DATE is not null and T.ROTC_DATE!='0')";
try {
Class.forName("com.sybase.jdbc4.jdbc.SybDriver").newInstance();
} catch (ClassNotFoundException e1) {
e1.printStackTrace();
}
String url = "jdbc:sybase:Tds:192.168.5.74:2638/twse_mops";
System.out.println(url);
try {
Connection myConn = DriverManager.getConnection(url, "dba", "sql");
Statement myStmt = myConn.createStatement();
// 执行查询
ResultSet rs = myStmt.executeQuery(queryString);
// 获得数据结果集合
ResultSetMetaData rmeta = rs.getMetaData();
// 确定数据集的列数,亦字段数
int numColumns = rmeta.getColumnCount();
System.out.println("numColumns = " + numColumns);
// 输出每一个数据值
for (int i = 1; i <= numColumns; i++) {
if (i < numColumns)
System.out.print(rmeta.getColumnName(i) + " | ");
else
System.out.println(rmeta.getColumnName(i));
}
while (rs.next()) {
for (int i = 1; i <= numColumns; i++) {
if (i < numColumns)
System.out.print(rs.getString(i).trim() + " | ");
else
System.out.println(rs.getString(i).trim());
}
}
// 关闭所有打开的资源
rs.close();
myStmt.close();
myConn.close();
} catch (SQLException e2) {
e2.printStackTrace();
}
}
}
这篇关于JAVA连接sybase数据库程序代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!