当前位置: 技术问答>java相关
java如何与ORACLE连接??分必给!
来源: 互联网 发布时间:2014-12-30
本文导语: java如何与ORACLE连接??分必给! 最好给个例子!!! | import java.sql.*; public class connectToOracle extends java.applet.Applet { Driver driver; Connection conn = null; static String driverUsed = ...
java如何与ORACLE连接??分必给!
最好给个例子!!!
最好给个例子!!!
|
import java.sql.*;
public class connectToOracle extends java.applet.Applet {
Driver driver;
Connection conn = null;
static String driverUsed =
"oracle.jdbc.driver.OracleDriver";
static String serverAddress =
"jdbc:oracle:thin:scott/tiger@www.myCompany.com:1243:myInstance";
// jdbc:oracle:thin is the driver used
// scott/tiger is user/password
// www.myServer.com is the same machine from where the Applet was loaded
// 1234 is the port used
// myInstance is where my data is
public void init(){
makeConnection(serverAddress);
}
public void makeConnection(String svr) {
try {
System.out.println("Loading ... " + driverUsed);
driver =
(Driver)Class.forName(driverUsed).newInstance();
System.out.println("Connecting ... " + svr);
conn =
DriverManager.getConnection(svr);
System.out.println("Ready.");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public class connectToOracle extends java.applet.Applet {
Driver driver;
Connection conn = null;
static String driverUsed =
"oracle.jdbc.driver.OracleDriver";
static String serverAddress =
"jdbc:oracle:thin:scott/tiger@www.myCompany.com:1243:myInstance";
// jdbc:oracle:thin is the driver used
// scott/tiger is user/password
// www.myServer.com is the same machine from where the Applet was loaded
// 1234 is the port used
// myInstance is where my data is
public void init(){
makeConnection(serverAddress);
}
public void makeConnection(String svr) {
try {
System.out.println("Loading ... " + driverUsed);
driver =
(Driver)Class.forName(driverUsed).newInstance();
System.out.println("Connecting ... " + svr);
conn =
DriverManager.getConnection(svr);
System.out.println("Ready.");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
import java.sql.*;
public class connectToOracle extends java.applet.Applet {
Driver driver;
Connection conn = null;
static String driverUsed =
"oracle.jdbc.driver.OracleDriver";
static String serverAddress =
"jdbc:oracle:thin:scott/tiger@www.myCompany.com:1243:myInstance";
// jdbc:oracle:thin is the driver used
// scott/tiger is user/password
// www.myServer.com is the same machine from where the Applet was loaded
// 1234 is the port used
// myInstance is where my data is
public void init(){
makeConnection(serverAddress);
}
public void makeConnection(String svr) {
try {
System.out.println("Loading ... " + driverUsed);
driver =
(Driver)Class.forName(driverUsed).newInstance();
System.out.println("Connecting ... " + svr);
conn =
DriverManager.getConnection(svr);
System.out.println("Ready.");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
public class connectToOracle extends java.applet.Applet {
Driver driver;
Connection conn = null;
static String driverUsed =
"oracle.jdbc.driver.OracleDriver";
static String serverAddress =
"jdbc:oracle:thin:scott/tiger@www.myCompany.com:1243:myInstance";
// jdbc:oracle:thin is the driver used
// scott/tiger is user/password
// www.myServer.com is the same machine from where the Applet was loaded
// 1234 is the port used
// myInstance is where my data is
public void init(){
makeConnection(serverAddress);
}
public void makeConnection(String svr) {
try {
System.out.println("Loading ... " + driverUsed);
driver =
(Driver)Class.forName(driverUsed).newInstance();
System.out.println("Connecting ... " + svr);
conn =
DriverManager.getConnection(svr);
System.out.println("Ready.");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
|
好久没来过了,再给你一个oci8的,是8i带的demo:
/*
* This sample shows how to list all the names from the EMP table
*/
// You need to import the java.sql package to use JDBC
import java.sql.*;
class Employee
{
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:oci8:@test", "scott", "tiger");
//test是oracle实例名
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select ENAME from EMP");
// Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1));
// Close the RseultSet
rset.close();
// Close the Statement
stmt.close();
// Close the connection
conn.close();
}
}
/*
* This sample shows how to list all the names from the EMP table
*/
// You need to import the java.sql package to use JDBC
import java.sql.*;
class Employee
{
public static void main (String args [])
throws SQLException
{
// Load the Oracle JDBC driver
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
// Connect to the database
// You can put a database name after the @ sign in the connection URL.
Connection conn =
DriverManager.getConnection ("jdbc:oracle:oci8:@test", "scott", "tiger");
//test是oracle实例名
// Create a Statement
Statement stmt = conn.createStatement ();
// Select the ENAME column from the EMP table
ResultSet rset = stmt.executeQuery ("select ENAME from EMP");
// Iterate through the result and print the employee names
while (rset.next ())
System.out.println (rset.getString (1));
// Close the RseultSet
rset.close();
// Close the Statement
stmt.close();
// Close the connection
conn.close();
}
}
|
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@数据库IP:1521:ORCL",数据库用户名,密码);
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery(sql语句);
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@数据库IP:1521:ORCL",数据库用户名,密码);
Statement state = conn.createStatement();
ResultSet rs = state.executeQuery(sql语句);
|
我想客户端没必要装oracle吧!
在连接oracle时,主要是一个url的设置,如果是jbuilder4可以用Jdbc explore发现这个url,试过可行的
在连接oracle时,主要是一个url的设置,如果是jbuilder4可以用Jdbc explore发现这个url,试过可行的
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。