Você está na página 1de 7

Home (/index.shtml) / Answers (/answers/) / Viewqa (/answers/viewqa/) / JDBC (/answers/viewqa/JDBC/) / JDBC Connection code to connect servlet with oracle.

Questions: Ask (/answers/askquestions/askquestion.html) / Latest (/answers/questions/) / Tutorials:Latest (/latest.shtml) / Topics (/programming-tutorial/)

JDBC Connection code to connect servlet with oracle.


Search

Share on Google+ (https://plus.google.com/share?url=http://www.roseindia.net/answers/viewqa/JDBC/14936-JDBC-Connection-code-to-connect-servlet-with-oracle..html)

JDBC Connection code to connect servlet with oracle.


1 Answer(s) 7 years ago
Posted in : JDBC (/answers/viewqa/JDBC/)
Abhay kathwate

Ads

JDBC Connection code to connect servlet with oracle

Ads

View Answers

January 5, 2011 at 12:54 PM

Hi Friend,

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:

Chatbox
Live help is offline...
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:3


"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.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.driver.*;

public class Data extends HttpServlet {


public void doPost(HttpServletRequest req,HttpServletResponse res) throws
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: @localho
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);
}
}
}

Thanks

Chatbox
Live help is offline...
Ads

Arduino Project Builder


Control sensors, motors & actuators. Drag &
drop project builder. Free download.
Cayenne

Post Answer

(http://www.roseindia.net/answers/questionposthelp.shtml)

Preview:

Post Answer

Related Tutorials/Questions & Answers:

JDBC Connection code to connect servlet with oracle. (http://www.roseindia.net/answers/viewqa/JDBC/14936-JDBC-Connection-code-to-connect-servlet-with-oracle..html)

JDBC Connection code to connect servlet with oracle. JDBC Connection code to connect servlet with oracle

JDBC Connection code to connect servlet with SQL Server 2008 (http://www.roseindia.net/answers/viewqa/JSP-Servlet/27299-JDBC-Connection-code-to-connect-servlet-with-SQL-Server-

2008.html)

JDBC Connection code to connect servlet with SQL Server 2008 Please ans me why it is not connect i use netbeans IDE7.0 Jdk 1.7 i also add... connection code is static Connection con;
static Statement st; static

Chatbox
Live help is offline...
Advertisements

Free IT Training
Perform Basic Computer
Tasks Improve Your Career
Prospects

Could not establish the connection to oracle - JDBC (http://www.roseindia.net/answers/viewqa/JDBC/7739-Could-not-establish-the-connection-to-oracle.html)

to use: Connection conn = DriverManager.getConnection("jdbc:oracle:oci8... to use: Connection conn = DriverManager.getConnection ("jdbc:oracle:thin... oracle.jdbc.driver.OracleDriver());
Connection conn = DriverManager.getConnection("jdbc:oracle

REQ for connection b/w jdbc and oracle database (http://www.roseindia.net/answers/viewqa/JDBC/27860--REQ-for-connection-b-w-jdbc-and-oracle-database-.html)

REQ for connection b/w jdbc and oracle database REQ for connection b/w jdbc and oracle database The Java classes to connect...()); Connection conn =
DriverManager.getConnection("jdbc:oracle:thin: @localhost

java to oracle code - JDBC (http://www.roseindia.net/answers/viewqa/JDBC/10679-java-to-oracle-code.html)

java to oracle code please suggest m calculate the database performance between scaled images and unscaled images in the database

jdbc connection to java program code - JDBC (http://www.roseindia.net/answers/viewqa/JDBC/6104-jdbc-connection-to-java-program-code.html)

jdbc connection to java program code i want a simple java program that which communicates with oracle database like creating table,insert values and update records in database and
show in program output? please take a simple

if my database(oracle) connection failure means wat is the code to write in exception ? (http://www.roseindia.net/answers/viewqa/Java-Interview-Questions/23476-if-my-database-oracle-

connection-failure-means-wat-is-the-code-to-write-in-exception-.html)

if my database(oracle) connection failure means wat is the code to write in exception ? if my database(oracle) connection failure means wat is the code to write in exception

jsp connection with oracle. (http://www.roseindia.net/answers/viewqa/JSP-Servlet/17586-jsp-connection-with-oracle..html)

jsp connection with oracle. I want to connect with jsp to oracle,,,plz send me the codes

jsp connection with oracle. (http://www.roseindia.net/answers/viewqa/JSP-Servlet/17585-jsp-connection-with-oracle..html)

jsp connection with oracle. I want to connect with jsp to oracle,,,plz send me the codes

how to write a code for oracle database connection in asp.net for validation. (http://www.roseindia.net/answers/viewqa/JavaScriptQuestions/15684-how-to-write-a-code-for-oracle-database-

connection-in-asp.net-for-validation..html)

how to write a code for oracle database connection in asp.net for validation...:\oracle\product\10.2.0\client_1\BIN directory. This directory may be different based on your oracle
configuration. using Oracle.DataAccess.Client; // C

code for jsp to db connection using jdbc (http://www.roseindia.net/answers/viewqa/HTML/14026-code-for-jsp-to-db-connection-using-jdbc.html)

code for jsp to db connection using jdbc please send me the code Hi Friend, Try the following code: 1)form.html: <html> <...("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con =
DriverManager.getConnection("jdbc:odbc

ex. connect to Oracle - Java Beginners (http://www.roseindia.net/answers/viewqa/Java-Beginners/5553-ex.-connect-to-Oracle.html)

ex. connect to Oracle dear sir, I want to ask how to connect java to oracle, please give me a details tutorial with example code how to connect..."; String db = "XE"; String url =
"jdbc:oracle:thin:@" + serverName

Chatbox
Live help is offline...
jdbc oracle connectivity problem (http://www.roseindia.net/answers/viewqa/JDBC/21817-jdbc-oracle-connectivity-problem.html)

jdbc oracle connectivity problem Hi All, I am trying to connect my swing application to oracle DB . but class.forname is giving error. Please suggest me if there is some different way to
connect swing application to oracle

android connection to database oracle 10g (http://www.roseindia.net/answers/viewqa/Java-Interview-Questions/30537-android-connection-to-database-oracle-10g.html)

android connection to database oracle 10g Hello, How i can connect my android application to my oracle 10g database

Connect GWT application with Oracle (http://www.roseindia.net/answers/viewqa/JDBC/32082-Connect-GWT-application-with-Oracle.html)

Connect GWT application with Oracle Can anyone tell me how to connect gwt with oracle database...??? I tried but, I failed every time. If possible please rply with a sample code in while
database is connected in gwt application

Java Connection to Oracle database (http://www.roseindia.net/answers/viewqa/Java-Beginners/19037-Java-Connection-to-Oracle-database.html)

Java Connection to Oracle database HI I am ubable to connnect jdbc to Oracle database.i had gone to control panel >>Administrative tool>>>Data source,in that forDNS name i typed
Oracle and in TNS i culdn't

error oracle database connection? (http://www.roseindia.net/answers/viewqa/JSP-Servlet/21388-error-oracle-database-connection-.html)

error oracle database connection? hi now i am trying to connect oracle database and also insert my data into table, but it's not working.. I created... in oracle table. my table name is
logininfoclient_sla. this is my coding

connect netbeans with oracle (http://www.roseindia.net/answers/viewqa/JSP-Servlet/14569-connect-netbeans-with-oracle.html)

connect netbeans with oracle how to connect netbeans with oracle Hi Friend, Please visit the following link: http://www.roseindia.net/jsf/visual-jsf.shtml Here you will find the steps to
connect netbeans

JDBC connection (http://www.roseindia.net/jdbc/Jdbc-connection.shtml)

to understand JDBC connection. The code explains you how... Jdbc Connection, We have a list of method required to connect and close... JDBC connection The JDBC

oracle - JDBC (http://www.roseindia.net/answers/viewqa/JDBC/5070-oracle.html)

oracle sir iam hari iam doing the project using oracle with jdbc in xp when iam inserting the data it is giving error like ora-01034:oracle not available sir plz.......give me the solution it is
urgent sir thanku

oracle - JDBC (http://www.roseindia.net/answers/viewqa/JDBC/4620-oracle.html)

oracle HI i am using ORACLE 10g. How to i find "TNSNAMES.ORA" file in my system after installation of ORACLE 10g succussfully. please send... that depends on the actual network
implementation used. Oracle recommends that SDU

how can i create a mysql database to connect to this code - JDBC (http://www.roseindia.net/answers/viewqa/JDBC/7851-how-can-i-create-a-mysql-database-to-connect-to-this-code.html)

"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register...("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc..."); Connection


con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register

how can i create a mysql database to connect to this code - JDBC (http://www.roseindia.net/answers/viewqa/JDBC/7852-how-can-i-create-a-mysql-database-to-connect-to-this-code.html)

"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register...("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc..."); Connection


con = DriverManager.getConnection("jdbc:mysql://localhost:3306/register

JDBC Oracle program (http://www.roseindia.net/answers/viewqa/JDBC/18935-JDBC-Oracle-program.html)

JDBC Oracle program Here is my code: import java.sql.*; public...("oracle.jdbc.driver.OracleDriver"); Connection conn =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:8080:oracle", "System", "System

Hibernate- Oracle connection - Hibernate (http://www.roseindia.net/answers/viewqa/Hibernate/10042-Hibernate--Oracle-connection.html)

to make a connection to oracle DB from eclipse. Thanks...Hibernate- Oracle connection In Eclipse I tried Windows -->... on databaseconnection --> New Oracle Added ojdbc14 Jar file
path UID

Oracle - JDBC (http://www.roseindia.net/answers/viewqa/JDBC/240-Oracle.html)

Oracle fetch variables i wanted to fetch variables from database using Oracle Chatbox
Live help is offline...
About connection database oracle to java (http://www.roseindia.net/answers/viewqa/Java-Beginners/18970-About-connection-database-oracle-to-java.html)

About connection database oracle to java Hi I culdn't connect oracle datebase to java.when i compile the program it is ok.but when i clicked... Oracle and then i clicked add button.Then
Dialog box came indicating DNS and TNS

About connection database oracle to java (http://www.roseindia.net/answers/viewqa/Java-Beginners/18971-About-connection-database-oracle-to-java.html)

About connection database oracle to java Hi I culdn't connect oracle datebase to java.when i compile the program it is ok.but when i clicked... Oracle and then i clicked add button.Then
Dialog box came indicating DNS and TNS

About connection database oracle to java (http://www.roseindia.net/answers/viewqa/Java-Beginners/18972-About-connection-database-oracle-to-java.html)

About connection database oracle to java I culdn't connect oracle... Panel>>Administrative Tools>>Data Sources in that i selected Oracle... just typed DNS as Oracle and for TNS i clicked
on the list of TNS. But it has

About connection database oracle to java (http://www.roseindia.net/answers/viewqa/Java-Beginners/18973-About-connection-database-oracle-to-java.html)

About connection database oracle to java I culdn't connect oracle... Panel>>Administrative Tools>>Data Sources in that i selected Oracle... just typed DNS as Oracle and for TNS i clicked
on the list of TNS .But it has

About connection database oracle to java (http://www.roseindia.net/answers/viewqa/Java-Beginners/18974-About-connection-database-oracle-to-java.html)

About connection database oracle to java **Hi I culdn't connect oracle datebase to java.when i compile the program it is ok.but when i clicked... Oracle and then i clicked add button.Then
Dialog box came indicating DNS and TNS

Search
Ads

KAYAK® Hotel Search

Hotels in Dubai

Book Now

Hotels in Ras Al Khaimah

Book Now

Hotels in Barcelona

Book Now

Advertisement

null

Java Tutorials (http://www.roseindia.net/java/)


Java Code example
(http://www.roseindia.net/javacodeexamples/index.shtml)
Java Programming
(http://www.roseindia.net/java/java- Chatbox
Live help is offline...
programming.shtml)
Java Beginners Examples
(http://www.roseindia.net/java/beginners/)
Applet Tutorials
(http://www.roseindia.net/java/example/java/applet/index.shtml)
Awt Tutorials
(http://www.roseindia.net/java/example/java/awt/index.shtml)
Java Certification
(http://www.roseindia.net/javacertification/)
Interview Question
(http://www.roseindia.net/interviewquestions/)
Java Servlets Tutorial
(http://www.roseindia.net/servlets/servlets.shtml)
Jsp Tutorials
(http://www.roseindia.net/jsp/jsp.shtml)
Java Swing Tutorials
(http://www.roseindia.net/java/example/java/swing/)
JDBC Tutorial
(http://www.roseindia.net/jdbc/jdbc.shtml)
EJB Tutorials (http://www.roseindia.net/ejb/)
Java Server Faces (JSF) Tutorial
(http://www.roseindia.net/jsf/)
WAP Tutorial
(http://www.roseindia.net/wap/index.shtml)
Struts Tutorial
(http://www.roseindia.net/struts/)
JAXB Tutorial
(http://roseindia.net/jaxb/r/jaxb.shtml)
Spring FrameWork Tutorial
(http://www.roseindia.net/spring/index.shtml)
SOA&Web Services Tutorials
(http://www.roseindia.net/webservices/)
Bioinformatics Tutorials
(http://www.roseindia.net/bioinformatics/bioinformatics_resources.shtml)
MySQL Tutorials
(http://www.roseindia.net/mysql/)
JAVA DOM Tutorial
(http://www.roseindia.net/xml/dom/)
XML Tutorial
(http://www.roseindia.net/xml/dom/xml-
tutorial.shtml)
EAI Articles (http://www.roseindia.net/eai/)
Many Programming Tutorials Links
(http://www.roseindia.net/programming/tutorials/index.shtml)
Tutorials Books
(http://roseindia.net/programming/books/index.shtml)
Java Script Tutorial
(http://www.roseindia.net/javascript)
Ajax Tutorial (http://www.roseindia.net/ajax/)
Dojo Tutorials
(http://www.roseindia.net/dojo/)
Programming Books
(http://www.roseindia.net/programming/books/)
Trainings
(http://www.roseindia.net/training/index.shtml)
Flex (http://www.roseindia.net/flex/)
Ant (http://www.roseindia.net/tutorials/ant/)
RDF (http://www.roseindia.net/tutorials/rdf/)

Ads

Chatbox
Live help is offline...

Você também pode gostar