// fileReadWriteTest.java
import java.io.*;
import java.sql.*;
public class fileReadWriteTest
{
public static void main(String[] args) throws SQLException
{
OracleTest myOracleTest = new OracleTest();
try
{
Connection myConnection = myOracleTest.getConnection();
System.out.println("Now begin to excute.............");
PreparedStatement myPreparedStatement = myConnection.prepareStatement("select aa, bb from wdtest");
//myPreparedStatement.setInt(1,2);
ResultSet myResultSet = myPreparedStatement.executeQuery();
StringBuffer myStringBuffer = new StringBuffer();
while(myResultSet.next())
{
myStringBuffer.append(myResultSet.getString("aa")+" ");
myStringBuffer.append(myResultSet.getInt("bb")+"/n");
}
System.out.println(myStringBuffer.toString());
//System.out.println(new String(myStringBuffer.toString().getBytes("ISO-8859-1"),"GBK"));
}catch(Exception ex)
{
System.out.println(myOracleTest.getErrorMessage(ex,"Application error,please contact to your Software Designer!").toString());
}
}
void readMyFile() {
String record = null;
int recCount = 0;
try {
FileReader fr = new FileReader("c://log-svc.xml");
BufferedReader br = new BufferedReader(fr);
record = new String();
while ((record = br.readLine()) != null) {
recCount++;
System.out.println(recCount + ": " + record);
}
br.close();
fr.close();
} catch (IOException e) {
System.out.println("Uh oh, got an IOException error!");
e.printStackTrace();
}
}
}
//OracleTest.java
import java.sql.Connection;
import java.sql.DriverManager;
public class OracleTest { private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver"; private final String oracleUrlToConnect = "jdbc:oracle:thin:@IP地址:端口号(默认为1521):数据库名称"; private Connection myConnection = null; /** * To load the jdbc driver */ public OracleTest() { try { Class.forName(oracleDriverName); }catch(ClassNotFoundException ex) { System.out.println(getErrorMessage(ex,"The Driver loaded error,please contact to your Software Designer!").toString()); } } public StringBuffer getErrorMessage(Exception ex,String alarmMessage) { StringBuffer errorStringBuffer = new StringBuffer(); errorStringBuffer.append(alarmMessage); errorStringBuffer.append(ex.getMessage()); return errorStringBuffer; } /** * getConnection method * @return Connection */ public Connection getConnection() { try { this.myConnection = DriverManager.getConnection(oracleUrlToConnect,"用户名","密码"); }catch(Exception ex) { System.out.println(getErrorMessage(ex,"Can not get connection,please contact to your Software Designer!").toString()); } return this.myConnection; } }