Sunteți pe pagina 1din 7

Get Stored Procedure Name And Type

import import import import import java.sql.Connection; java.sql.DatabaseMetaData; java.sql.DriverManager; java.sql.ResultSet; java.sql.Statement;

public class Main { public static void main(String[] args) throws Exception { Connection conn = getMySqlConnection(); System.out.println("Got Connection."); Statement st = conn.createStatement(); st.executeUpdate("drop table survey;"); st.executeUpdate("create table survey (id int,name varchar(30));" ); st.executeUpdate("insert into survey (id,name ) values (1,'nameVa lue')"); ResultSet rs = null; DatabaseMetaData meta = conn.getMetaData(); rs = meta.getProcedures(null, null, "%"); while (rs.next()) { String spName = rs.getString("PROCEDURE_NAME"); int spType = rs.getInt("PROCEDURE_TYPE"); System.out.println("Stored Procedure Name: " + spName); if (spType == DatabaseMetaData.procedureReturnsResult) { System.out.println("procedure Returns Result"); } else if (spType == DatabaseMetaData.procedureNoResult) { System.out.println("procedure No Result"); } else { System.out.println("procedure Result unknown"); } } st.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, pass word); return conn; }

public static Connection getOracleConnection() throws Exception { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:caspian"; String username = "mp"; String password = "mp2"; Class.forName(driver); // load Oracle driver Connection conn = DriverManager.getConnection(url, username, pass word); return conn; } }

Program on inout parameter import import import import java.sql.CallableStatement; java.sql.Connection; java.sql.DriverManager; java.sql.Types;

public class Main { public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName); String String String String String String serverName = "127.0.0.1"; portNumber = "1433"; mydatabase = serverName + ":" + portNumber; url = "jdbc:JSQLConnect://" + mydatabase; username = "username"; password = "password";

Connection connection = DriverManager.getConnection(url, username , password); CallableStatement cs = connection.prepareCall("{call myprocinout( ?)}"); // Register the type of the IN/OUT parameter cs.registerOutParameter(1, Types.VARCHAR); // Set the value for the IN/OUT parameter cs.setString(1, "a string"); // Execute the stored procedure and retrieve the IN/OUT value cs.execute(); String outParam = cs.getString(1); // OUT parameter System.out.println(outParam); } }

Program to get the all stored procedure names in database import import import import java.sql.Connection; java.sql.DatabaseMetaData; java.sql.DriverManager; java.sql.ResultSet;

public class Main {

public static void main(String[] argv) throws Exception { String driverName = "com.jnetdirect.jsql.JSQLDriver"; Class.forName(driverName); String String String String String String serverName = "127.0.0.1"; portNumber = "1433"; mydatabase = serverName + ":" + portNumber; url = "jdbc:JSQLConnect://" + mydatabase; username = "username"; password = "password";

Connection connection = DriverManager.getConnection(url, username , password); DatabaseMetaData dbmd = connection.getMetaData(); // Get all stored procedures in any schema and catalog ResultSet resultSet = dbmd.getProcedures(null, null, "%"); // Get stored procedure names from the result set while (resultSet.next()) { String procName = resultSet.getString(3); System.out.println(procName); } } }

Insert a Text file into oracle import import import import import java.io.File; java.io.FileInputStream; java.sql.Connection; java.sql.DriverManager; java.sql.PreparedStatement;

public class InsertTextFileToOracle { public static Connection getConnection() throws Exception { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:oracle"; String username = "username"; String password = "password"; Class.forName(driver); Connection conn = DriverManager.getConnection(url, username, pass word); return conn; } public static void main(String[] args)throws Exception { String id = "001"; String fileName = "fileName.txt"; FileInputStream fis = null; PreparedStatement pstmt = null; Connection conn = null; try { conn = getConnection(); conn.setAutoCommit(false); File file = new File(fileName); fis = new FileInputStream(file); pstmt = conn.prepareStatement("insert into DataFiles(id, fileNa

me, fileBody) values (?, ?, ?)"); pstmt.setString(1, id); pstmt.setString(2, fileName); pstmt.setAsciiStream(3, fis, (int) file.length()); pstmt.executeUpdate(); conn.commit(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); e.printStackTrace(); } finally { pstmt.close(); fis.close(); conn.close(); } } }

Serialized And Deserialize Object Oracle


import import import import import import import import import import import java.io.InputStream; java.io.ObjectInputStream; java.io.ObjectOutputStream; java.io.OutputStream; java.sql.CallableStatement; java.sql.Connection; java.sql.DriverManager; java.sql.PreparedStatement; java.sql.ResultSet; java.util.ArrayList; java.util.List;

import oracle.sql.BLOB; /**** CREATE TABLE java_objects (object_id NUMBER, object_name varchar(128) , object_value BLOB DEFAULT empty_blob(), primary key (object_id)); SQL> desc java_objects; Name Null? Type ----------------------------------------- -------- --------------------------OBJECT_ID NOT NULL NUMBER OBJECT_NAME VARCHAR2(128) OBJECT_VALUE BLOB SQL> select SEQUENCE_NAME, MIN_VALUE, MAX_VALUE, INCREMENT_BY, LAST_N UMBER from user_sequences; SEQUENCE_NAME MIN_VALUE MAX_VALUE INCREMENT_BY LAS T_NUMBER ------------------------------ ---------- ---------- ------------ ---------ID_SEQ 1 1.0000E+27 1 21 JAVA_OBJECT_SEQUENCE 1 1.0000E+27 1 1 */ public class Main { public static void main(String[] args) throws Exception { String WRITE_OBJECT_SQL = "BEGIN "

value) "

+ "

INSERT INTO java_objects(object_id, object_name, object_

+ " VALUES (?, ?, empty_blob()) " + " RETURN object_value I NTO ?; " + "END;"; String READ_OBJECT_SQL = "SELECT object_value FROM java_objects W HERE object_id = ?"; Connection conn = getOracleConnection(); conn.setAutoCommit(false); List<Object> list = new ArrayList<Object>(); list.add("This is a short string."); list.add(new Integer(1234)); list.add(new java.util.Date()); // write object to Oracle long id = 0001; String className = list.getClass().getName(); CallableStatement cstmt = conn.prepareCall(WRITE_OBJECT_SQL); cstmt.setLong(1, id); cstmt.setString(2, className); cstmt.registerOutParameter(3, java.sql.Types.BLOB); cstmt.executeUpdate(); BLOB blob = (BLOB) cstmt.getBlob(3); OutputStream os = blob.getBinaryOutputStream(); ObjectOutputStream oop = new ObjectOutputStream(os); oop.writeObject(list); oop.flush(); oop.close(); os.close(); // Read object from oracle PreparedStatement pstmt = conn.prepareStatement(READ_OBJECT_SQL); pstmt.setLong(1, id); ResultSet rs = pstmt.executeQuery(); rs.next(); InputStream is = rs.getBlob(1).getBinaryStream(); ObjectInputStream oip = new ObjectInputStream(is); Object object = oip.readObject(); className = object.getClass().getName(); oip.close(); is.close(); rs.close(); pstmt.close(); conn.commit(); // de-serialize list a java object from a given objectID List listFromDatabase = (List) object; System.out.println("[After De-Serialization] list=" + listFromDat abase); 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, pass word); return conn; } public static Connection getOracleConnection() throws Exception { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:databaseName"; String username = "userName"; String password = "password"; Class.forName(driver); // load Oracle driver Connection conn = DriverManager.getConnection(url, username, pass word); return conn; } }

Insert BLOG(Picture or Photo) Data Type Into Oracle Database


import import import import import import import java.io.File; java.io.FileInputStream; java.io.OutputStream; java.sql.Connection; java.sql.DriverManager; java.sql.ResultSet; java.sql.Statement;

import oracle.jdbc.OracleResultSet; public class Main { public static void main(String[] args) throws Exception { Connection conn = getOracleConnection(); int rows = 0; FileInputStream fin = null; OutputStream out = null; ResultSet rs = null; Statement stmt = null; oracle.sql.BLOB photo = null; conn.setAutoCommit(false); stmt = conn.createStatement(); String id = "001"; String binaryFileName = "fileName.dat"; rows = stmt.executeUpdate("insert into my_pictures(id, photo ) va lues ('" + id + "', empty_blob() )"); System.out.println(rows + " rows inserted"); rs = stmt.executeQuery("select photo from my_pictures where id =

'" + id

+ "' for update nowait"); rs.next(); photo = ((OracleResultSet) rs).getBLOB(1); fin = new FileInputStream(new File(binaryFileName)); out = photo.getBinaryOutputStream(); // Get the optimal buffer size from the BLOB byte[] buffer = new byte[photo.getBufferSize()]; int length = 0; while ((length = fin.read(buffer)) != -1) { out.write(buffer, 0, length); } conn.commit(); out.close(); fin.close(); rs.close(); stmt.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, pass word); return conn; } public static Connection getOracleConnection() throws Exception { String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:databaseName"; String username = "userName"; String password = "password"; Class.forName(driver); // load Oracle driver Connection conn = DriverManager.getConnection(url, username, pass word); return conn; } }

S-ar putea să vă placă și