Java使用Jdbc连接Oracle执行简单查询操作示例
程序员文章站
2022-06-30 09:54:34
本文实例讲述了java使用jdbc连接oracle执行简单查询操作。分享给大家供大家参考,具体如下:
java jdbc 连接 oracle 执行简单查询示例:...
本文实例讲述了java使用jdbc连接oracle执行简单查询操作。分享给大家供大家参考,具体如下:
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 block e.printstacktrace(); } catch (sqlexception e) { // todo auto-generated catch block e.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:
上一篇: java 判断list是否为空过程解析
下一篇: springmvc注解配置实现解析