JDBC

出自 ProgWiki
前往: 導覽搜尋

JDBC(Java Database Connectivity),參照:『維基百科~JDBC』。

技術文件

應用範例

Select

import java.sql.*;
 
public class SelectSample 
{
    public static void main(String args[]) 
    {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
 
        try 
        {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection("jdbc:odbc:MyDB","UserId","Password");
 
            Statement stmt = conn.createStatement();
 
            StringBuffer sql = new StringBuffer();  
            sql.append("select * from TestTable ");
 
            ResultSet rs = stmt.executeQuery(sql.toString());	
 
            while(rs.next())
            {
                int nId = rs.getInt("Id");
                String strName = rs.getString("Name");
                String strTel = rs.getString("Tel");
 
                System.out.print(strName);
            }
        }
        catch(Exception e)
        {
 
        }
        finally
        {
            if (rs != null)
                rs.close(); 	
            if (stmt != null)
                stmt.close(); 	
            if (conn != null)
                conn.close(); 	   
        }
    }
}

DataType的對應關係

JDBC Driver相關

相關