注意记得把oracle驱动类加入classpath里面去,驱动包在csdn上搜下就可以找到。如果用Eclipse,放入classpath特别简单,用记事本编辑就要在环境变量在配。
package
com.data;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/** */ /**
*
*
* @author <a href="mailto:lqinglong@yahoo.cn">qinglong.lu</a>
*
* 2008-3-26
*/
public class OracleUtil ... {
private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";
//private final String url = "jdbc:oracle:thin:@192.168.0.191:1521:ALBUM" ;
private final String url ="jdbc:oracle:oci8:@oradb.wodexiangce.com" ;
private String username = "ooo" ;
private String password = "pppp";
public OracleUtil()...{
try...{
Class.forName(oracleDriverName).newInstance();
}catch(ClassNotFoundException e)...{
e.printStackTrace() ;
}catch (IllegalAccessException e) ...{
e.printStackTrace();
}catch(InstantiationException e)...{
e.printStackTrace() ;
}
}
public Connection getConnection()...{
try ...{
return DriverManager.getConnection(url,username,password);
} catch (SQLException e) ...{
e.printStackTrace();
return null ;
}
}
public static void main(String args[]) throws SQLException ...{
// Load the Oracle JDBC driver
OracleUtil o = new OracleUtil();
Connection conn = o.getConnection();
conn.close();
}
}
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/** */ /**
*
*
* @author <a href="mailto:lqinglong@yahoo.cn">qinglong.lu</a>
*
* 2008-3-26
*/
public class OracleUtil ... {
private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";
//private final String url = "jdbc:oracle:thin:@192.168.0.191:1521:ALBUM" ;
private final String url ="jdbc:oracle:oci8:@oradb.wodexiangce.com" ;
private String username = "ooo" ;
private String password = "pppp";
public OracleUtil()...{
try...{
Class.forName(oracleDriverName).newInstance();
}catch(ClassNotFoundException e)...{
e.printStackTrace() ;
}catch (IllegalAccessException e) ...{
e.printStackTrace();
}catch(InstantiationException e)...{
e.printStackTrace() ;
}
}
public Connection getConnection()...{
try ...{
return DriverManager.getConnection(url,username,password);
} catch (SQLException e) ...{
e.printStackTrace();
return null ;
}
}
public static void main(String args[]) throws SQLException ...{
// Load the Oracle JDBC driver
OracleUtil o = new OracleUtil();
Connection conn = o.getConnection();
conn.close();
}
}