java操作oracle数据库

 package com.chinacache.boss.queryservice.service.impl; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import com.chinacache.boss.queryservice.exception.BusinessException; import oracle.sql.ArrayDescriptor; public class Main { public static void main(String[] args) throws Exception { Connection conn = null; PreparedStatement pstmt = null; java.sql.Array sqlArray = null; conn = getOracleConnection(); // For oracle you need an array descriptor specifying // the type of the array and a connection to the database // the first parameter must match with the SQL ARRAY type created ArrayDescriptor arrayDescriptor = ArrayDescriptor.createDescriptor( "CHAR_ARRAY", conn);// CREATE OR REPLACE TYPE CHAR_ARRAY AS table OF VARCHAR2(255) // then obtain an Array filled with the content below String[] content = { "5137", "V2", "V3", "V4" }; sqlArray = new oracle.sql.ARRAY(arrayDescriptor, conn, content); pstmt = conn.prepareStatement("" + "select * " + "from bandwith_area_test t where t.channel_id in (select * from the (select cast(? as CHAR_ARRAY) from dual)) and t.area_id = ?" + "and t.day >= ? and t.day <= ? order by t.day"); Date start = null; Date end = null; try { SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm"); start = df.parse("200907011000"); end = df.parse("200907012000"); } catch (ParseException e) { e.printStackTrace(); throw new BusinessException(e.getMessage()); } pstmt.setArray(1, sqlArray); pstmt.setString(2, "9050"); pstmt.setDate(3, new java.sql.Date(start.getTime())); pstmt.setDate(4, new java.sql.Date(end.getTime())); int rowCount = pstmt.executeUpdate(); System.out.println("rowCount=" + rowCount); System.out.println("--Demo_PreparedStatement_SetArray end--"); pstmt.close(); conn.close(); } // private static Connection getHSQLConnection() throws Exception { // Class.forName("org.hsqldb.jdbcDriver"); // System.out.println("Driver Loaded."); // String url = "jdbc:hsqldb:data/tutorial"; // return DriverManager.getConnection(url, "sa", ""); // } // public static Connection getMySqlConnection() throws Exception { // String driver = "org.gjt.mm.mysql.Driver"; // String url = "jdbc:mysql://localhost/demo2s"; // String username = "oost"; // String password = "oost"; // // Class.forName(driver); // Connection conn = DriverManager.getConnection(url, username, password); // return conn; // } public static Connection getOracleConnection() throws Exception { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@192.168.6.184:1521:logbill"; String username = "aaaa"; String password = "aaaa"; Class.forName(driver); // load Oracle driver Connection conn = DriverManager.getConnection(url, username, password); return conn; } }

你可能感兴趣的:(sql相关)