File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Problem connecting to database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem connecting to database" Watch "Problem connecting to database" New topic
Author

Problem connecting to database

shobha devi
Greenhorn

Joined: Mar 11, 2009
Posts: 11
<%@ page import="java.sql.*" %>
<%
Connection con;
ResultSet rs;
Statement st;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:oradsn","system","manager");
out.println("Connected ...");
st=con.createStatement();
rs=st.executeQuery( "select * from test");
while(rs.next())
rs.getString(1);
}
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
Marshal

Joined: Jan 10, 2002
Posts: 61198
    
  66

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
Greenhorn

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
Bartender

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: http://aspose.com/file-tools
 
subject: Problem connecting to database