My program code is-----
import java.sql.*;
import java.lang.* ;
import java .io.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;
class Jdbcthin { public static void main(String arr[]) throws SQLException, ClassNotFoundException { try { Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin@localhost:1521:XE", "system", "oracle"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select*from Emp"); System.out.println("Following Records are fetched"); while(rs.next()) {
System.out.println(rs.getInt(1) +"\t\t"+ rs.getString(2)+"\t\t"+ rs.getString(3)+"\t\t"+ rs.getInt(4)); } con.close(); } catch(Exception e) { System.out.println(e); } } }
It is giving an error like--
import java.sql.*;
import java.lang.* ;
import java .io.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;
class Jdbcthin { public static void main(String arr[]) throws SQLException, ClassNotFoundException { try { Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin@localhost:1521:XE", "system", "oracle"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select*from Emp"); System.out.println("Following Records are fetched"); while(rs.next()) {
System.out.println(rs.getInt(1) +"\t\t"+ rs.getString(2)+"\t\t"+ rs.getString(3)+"\t\t"+ rs.getInt(4)); } con.close(); } catch(Exception e) { System.out.println(e); } } }
It giving an error like this----
import java.sql.*;
import java.lang.* ;
import java .io.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleDataSource;
class Jdbcthin { public static void main(String arr[]) throws SQLException, ClassNotFoundException { try { Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:thin@localhost:1521:XE", "system", "oracle"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select*from Emp"); System.out.println("Following Records are fetched"); while(rs.next()) {
System.out.println(rs.getInt(1) +"\t\t"+ rs.getString(2)+"\t\t"+ rs.getString(3)+"\t\t"+ rs.getInt(4)); } con.close(); } catch(Exception e) { System.out.println(e); } } }
it producing an error like---
java.sql.SqlException : Invalid Oracle URL Specified
Please tell me its solution..
Thanks..
Follow these steps:
1) Import the following packages in your java file:***
import java.sql.*; import oracle.jdbc.driver.*; import oracle.sql.*;
2) Load and Register the JDBC driver:***
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
or you can use
Class.forName("oracle.jdbc.driver.OracleDriver");
3) Connect to database:***
a) If you are using oracle oci driver,you have to use:
Connection conn = DriverManager.getConnection("jdbc:oracle:oci8: @oracle.world", "root", "root");
where oracle.world is the TNSNAMES entry and root is the username and password.
b) If you are using oracle thin driver,you have to use:
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:3306:roseindia", "root", "root");
where localhost is the host,3306 is the port, roseindia is the database and root is the username and password.
4) Querying the database:**
a)create statement:
Statement st = conn.createStatement();
b)write query and execute the query:
ResultSet rs = st.executeQuery("SELECT * from student");
5) Close the statement,resultset and connection:**
rs.close(); st.close(); conn.close();
Try the following code:
import java.sql.*; import oracle.sql.*; import oracle.jdbc.driver.*; public class OracleExample { public static void main (String[] args) { try{ DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:thin: @localhost:3306:Oracle", "rose", "rose"); Statement st = conn.createStatement(); ResultSet rs = sql_stmt.executeQuery("SELECT * from student"); String str = ""; while (rs.next()) { System.out.println(rset.getInt(1)+" "+ rs.getString(2)+" "+ rset.getFloat(3)+"\n"; } rs.close(); st.close(); conn.close(); } catch(Exception e){} } }