Sunday, October 23, 2011

Java: Oracle connection




import java.io.*;
import java.util.*;
import java.sql.*;
import java.math.*;


public class dataSourceClass {
public static InputStream getStream(Hashtable hashtable) throws SQLException {

Enumeration hashEnum = hashtable.keys();

while (hashEnum.hasMoreElements()) {
String hashKey = (String) hashEnum.nextElement();
String hashValue = (String) hashtable.get(hashKey);
}

/*
Load the driver. This is an example of using the Oracle thin driver.
Make sure to use the correct path to the actual driver being used.
*/

DriverManager.registerDriver (new oracle.jdbc.driver.OracleDriver());


// Connect to the database

Connection connection = DriverManager.getConnection ("YourConnectionStringGoesHere");
/* An Oracle connection might look something like:

Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@Name:1521:orcl", "UserID", "Passwd");
*/

// Create a new statement
Statement newStatement = connection.createStatement ();

/*
This sample statement assumes that the database has a table named
"Employees" with columns "First_Name" and "Last_Name"
*/

ResultSet resultSet = newStatement.executeQuery ("select First_Name, Last_Name from Employees");

StringBuffer buffer = new StringBuffer();
buffer.setLength(0);

//since a movieClip is required to display each row of data, we specify "clip" in the datasource.

buffer.append("clip, First_Name, Last_Name\n");

while (resultSet.next ()) {
/*
While printing the values out, we will add the name of the movieClip that will be used to display the data.
For this example, we would have a movieClip in our template called "myClip." This movieClip would contain the
text variables {First_Name} and {Last_Name}.
*/

buffer.append("myClip, " + resultSet.getString(1) + "\n");

}

InputStream inputStream = new ByteArrayInputStream(buffer.toString().getBytes());

return inputStream;

}
}




No comments: