Java Jdbc 串連 Oracle 執行簡單查詢樣本

來源:互聯網
上載者:User

Java Jdbc 串連 Oracle  執行簡單查詢樣本:

 

package com.test.dbtest;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;/**Jdbc 串連 Oracle 資料庫 簡單樣本 *@author wanggq *@version 建立時間:2014年3月31日 上午11:00:06 *類說明 */public class TestO_procedure01 {public static void main(String[] args) {String driver = "oracle.jdbc.driver.OracleDriver";String url = "jdbc:Oracle:thin:@localhost:1521:orcl";Statement stmt = null;ResultSet res = null;Connection conn = null;CallableStatement proc = null;String sql = " select T.REC_NO, T.AIRLINE,T.DEPARTURE,T.ARRIVAL from CDP_MAIN_ORDER t where t.departure=upper('pek')";try {Class.forName(driver);conn = DriverManager.getConnection(url, "abc123", "abc123");stmt = conn.createStatement();res = stmt.executeQuery(sql);while(res.next()){String rec = res.getString("REC_NO");String airline = res.getString("AIRLINE");String dept = res.getString("DEPARTURE");String arr = res.getString("ARRIVAL");System.out.println(rec+" "+airline+" "+dept+" "+arr);}} catch (ClassNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

也可以使用防止SQL注入PreparedStatement方法

PreparedStatement stmt = null;ResultSet res = null;Connection conn = null;CallableStatement proc = null;String sql = " select T.REC_NO, T.AIRLINE,T.DEPARTURE,T.ARRIVAL from CDP_MAIN_ORDER t where t.departure=upper(?)";try {Class.forName(driver);conn = DriverManager.getConnection(url, "abc123", "abc123");stmt = conn.prepareStatement(sql);stmt.setString(1, "pek");res = stmt.executeQuery();



DB:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.