import java.sql.*;public classHello {private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";private final String oracleUrlToConnect ="jdbc:oracle:thin:@00.00.00.00:1521:ics";private Connection myConnection = null;private final String loginName="db_xxxx";private final String loginPassword="DB_xxxx";publicHello()
{try{
Class.forName(oracleDriverName);
}catch(ClassNotFoundException ex)
{//System.out.println(getErrorMessage(ex,"The Driver loaded error,please contact to your Software Designer!").toString());
System.out.println(ex.getMessage()+"\n"+ex.getStackTrace());
}
}publicStringBuffer getErrorMessage(Exception ex,String alarmMessage)
{
StringBuffer errorStringBuffer= newStringBuffer();
errorStringBuffer.append(alarmMessage);
errorStringBuffer.append(ex.getMessage());returnerrorStringBuffer;
}/*** getConnection method
*@returnConnection*/
publicConnection getConnection()
{try{this.myConnection =DriverManager.getConnection(oracleUrlToConnect,loginName,loginPassword);//System.out.print("connection successfully");
}catch(Exception ex)
{//System.out.println(getErrorMessage(ex,"Can not get connection,please contact to your Software Designer!").toString());
System.out.println(ex.getMessage()+"\n"+ex.getStackTrace());
}return this.myConnection;
}/***@paramargs*/
public static voidmain(String[] args) {
Hello he= newHello();try{
Connection conn=he.getConnection();
Statement stmt=conn.createStatement() ;
ResultSet rs=stmt.executeQuery("select xxx from tb_xxx where rownum<=5");while(rs.next())
{
System.out.println(rs.getString(1));
}//System.out.println("Now begin to excute.............");
if(rs != null){ //关闭记录集
try{
rs.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}if(stmt != null){ //关闭声明
try{
stmt.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}if(conn != null){ //关闭连接对象
try{
conn.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
}catch(Exception ex)
{
System.out.println(he.getErrorMessage(ex,"Application error,please contact to your Software Designer!").toString());
}
}
}