linux下javaリンクoracleデータベース
7857 ワード
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()+"
"+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()+"
"+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を選択すればいいです
ojdbc 6.rarダウンロード