2017年11月13日 星期一

Java Connect Informix DB example

(1)   Import ifxjdbc.jar 
 
(2) import java.sql.*;
(3) 注意連線字串
(4) 範例程式

package hhh;
import java.sql.*;


public class hahah {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Connection conn = null;
    Statement statement = null;
    ResultSet resultSet = null;
    
    String dbURL = "jdbc:informix-sqli://Host:port/database name:informixserver=servername;"
                                    +  "DB_LOCALE=zh_tw.big5;user=帳號;password=密碼;";
                   
    try {
    //測試連線
    Class.forName("com.informix.jdbc.IfxDriver");
System.out.println("Driver OK");
conn =DriverManager.getConnection(dbURL);
System.out.println("connection OK");

Statement query = null;
ResultSet rs = null;
try
{
query = conn.createStatement();
rs = query.executeQuery("SELECT * FROM TableName WHERE 條件= 'xxx'");
while (rs.next())
{
System.out.println(rs.getString(0));
}
rs.close();
query.close();
catch (SQLException exce)
{
System.out.println("Caught: " + exce.getErrorCode());
}
conn.close();
    catch (ClassNotFoundException drvEx)
    {
    System.err.println("Could not load JDBC driver");
    System.out.println("Exception: " + drvEx);
    drvEx.printStackTrace();
    }
    catch(SQLException sqlEx)
    {
    while(sqlEx != null)
    {
    System.err.println("SQLException information");
    System.err.println("Error msg: " + sqlEx.getMessage());
    System.err.println("SQLSTATE: " + sqlEx.getSQLState());
    System.err.println("Error code: " + sqlEx.getErrorCode());
    sqlEx.printStackTrace();
    sqlEx=sqlEx.getNextException();
    }
    }
}
}

Java 一些應用 [ csv reader / array of class / for( : ) ]

分享一些工作上用到的寫法 ( java )  csv reader  array of class for (:)  因為工作上需要讀取csv檔案的需求 , java 本身有提供一些 method  首先要 import opencsv-3.8.jar ...