-
Hi all,
How to connect java to sql. Can u please send the
script from class.forName script.
I don't know exactly the script.
Very urgent,
Thanks in advance,
Iyyappan.M
-
Hi.
For a crappy example of using JDBC from within the Oracle server see:
http://www.oracle-base.com/Articles/...heDatabase.asp
If you want to connect from an external Java program you can do something like this:
1) Save the following code as TestQuery.java:
import java.lang.*;
import java.sql.*;
public class TestQuery extends Object {
public TestQuery() {
String host = "marge";
String sid = "W2K1";
String username = "scott";
String password = "tiger";
Connection conn;
Statement stmt;
ResultSet rs;
String sql = "SELECT * FROM emp";
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@" + host + ":1521:" + sid, username, password);
try {
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getString(1) + " : " + rs.getString(2));
}
rs.close();
stmt.close();
conn.close();
}
catch(java.sql.SQLException ex) {
System.out.println("Error: " + ex.getLocalizedMessage());
}
}
catch (java.lang.ClassNotFoundException ex) {
System.out.println("Error: Cannot find driver class");
}
catch (SQLException ex) {
System.out.println("Error: Cannot connect to database: " + sid);
}
}
public static void main(String[] args) {
new TestQuery();
}
}
2) Save the following code as CompileAndRun.bat. You will have to amend the paths accordingly:
set BASE=C:\Development\Web\oracle-base.com\Articles\Java
set PATH=C:\Java\jdk1.4\bin;C:\Development\Web\oracle-base.com\Articles\Java
set CLASSPATH=C:\Development\Web\oracle-base.com\Articles\Java;ORACLE_HOME\jdbc\lib\Classes12.zip
javac TestQuery.java
java TestQuery
pause
3) If you don't have the Classes12.zip file on your machine you can find it in the ORACLE_HOME\jdbc\lib directory of your server. It contains all the thin driver code to allow connections to Oracle from Java.
4) Double-click on the batch file and look at the results.
Cheers
Tim...
-
Thanks...
Hi Tim,
Thank u very much,
Regards,
Iyyappan.M
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|