linux 下 java 链接oracle数据库
import java.sql.*; public class Hello { 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"; public Hello() { 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()); } } 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,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; } /** * @param args */ public static void main(String[] args) { Hello he = new Hello(); 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()); } } }
运行的时候需要执行ojdbc.rar文件,之前在window下运行没问题,但是放linux下运行老提示找不到加载不了数据库类,后来用eclipse把ojdbc.rar一起打包就行了
打包时要选择可运行的jar就行了