package com.tsi.constants; public class Constant { public static final String DRIVER = "oracle.jdbc.driver.OracleDriver"; public static final String DBURL = "jdbc:oracle:thin:@localhost:1521:XE"; public static final String DBUSER = "system"; public static final String DBPASSWORD = "ora10g"; //public static final String DRIVER = null; }
Follow these steps:
1) Import the following packages in your servlet 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 Data { public static void main(String[]args) { String msg=req.getParameter("msg"); res.setContentType("text/html"); PrintWriter out = res.getWriter(); System.out.println(msg); try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection("jdbc:oracle:thin: @localhost:3306:Oracle", "rose", "rose"); Statement stmt=con.createStatement(); ResultSet rs=stmt.executeQuery("select * from data"); while(rs.next()){ out.println(rs.getString("name")+" "+rs.getString("address")+"<br>"); } con.close(); } catch(Exception e){ System.out.println(e); } } }
Ads