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就行了

ojdbc6.rar下載

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM