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下载

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值