This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Problem connecting to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Problem connecting to database" Watch "Problem connecting to database" New topic

Problem connecting to database

shobha devi

Joined: Mar 11, 2009
Posts: 11
<%@ page import="java.sql.*" %>
Connection con;
ResultSet rs;
Statement st;
con = DriverManager.getConnection("jdbc:odbc:oradsn","system","manager");
out.println("Connected ...");
rs=st.executeQuery( "select * from test");
catch(Exception ee)
out.println("Error While Connecting DataBase" + ee);


I cannot connect to database.
oradsn is my dsn name and system,manager are user id and password respectively.
anyone please help me if there is any problem with code so that i can proceed.
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63355

The major problem with the code is that it is in a JSP! Move the code out of a JSP and into a Java class where it belongs.

As this is not a JSP question, it has been moved to the JDBC forum.

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
shobha devi

Joined: Mar 11, 2009
Posts: 11
Ok. But if we need to retrieve data from database through Jsp what necessary corrections i need to make.
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Why are you using the JDBC-ODBC bridge? Unless your database is MS Access (in which case you really don't have much choice) you should not be using it. As its docs say, its not really production code.

I cannot connect to database.

JDBC will be throwing an exception. What does that exception say?

(BTW: I have to second what Bear says - code directly in JSPs is not a good thing. Its hard to maintain, and hard to develop).

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
I agree. Here's the link:
subject: Problem connecting to database
It's not a secret anymore!