dcsimg
connecting to database through servlets
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   jilla_yuvaraj
Posted On:   Thursday, November 29, 2001 11:04 PM

i tried to connect to databases ms-access through servlets but i didn't get the correct answer, when i used the same program as application it was working properly, i have a database in which i have a table called emp which has two fieds one is eno and another one is ename , both are of type text. The actual code is import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class Sample extends HttpServlet{ public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException{ Connection con=null; Statement stmt=null; ResultSet rs=null; res.setContentType("text/html"); Print   More>>

i tried to connect to databases ms-access through servlets but i didn't get the correct answer, when i used the same program as application it was working properly,
i have a database in which i have a table called emp which has two fieds one is eno and another one is ename , both are of type text.
The actual code is

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Sample extends HttpServlet{

public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException{
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
res.setContentType("text/html");
PrintWriter out = res.getWriter();

try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:test");
stmt=con.createStatement();
rs=stmt.executeQuery("select * from emp");
out.println(" welcome

    ");
    while(rs.next())
    {
    out.println("
  • "+rs.getString("eno")+" "+rs.getString("ename"));
    }
    out.println("
");
} catch(Exception e){out.println(e);}

}
}

i got the following exception when i create the dsn as system dsn

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key 'DriverId'.

i got the following exception when i create the dsn as user dsn


java.sql.SQLException: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified


i tried the same with username and password for oracle
but i got the same exception as i got for ms-access.

can anybody help me out ?? do i need to do any modification in creating the dsn name??

Yuvaraj

.    <<Less

Re: connecting to database through servlets

Posted By:   jawwad_ahmed  
Posted On:   Friday, November 30, 2001 04:21 AM

Well i think u should make seperate class for the data base class and the display page i usually do like this and make connection in the system dns.
Here i use somet thing like this way


class InitializeDatabase implements EstablishConnection

{
Connection con;

static Statement stmt;

ResultSet rs;

ItemsDataBase load;

public boolean makeConnection()

{

try

{

Class.forName
("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:first","scott","tiger");
}

catch(ClassNotFoundException e)
{

System.out.println("Driver can not be loaded");

return false;
}

catch(SQLException e)
{

System.out.println("The Sql En caught is");

return false;
}

return true;

}

public void loadDatabase()
{


load=new ItemsDataBase();

int a=0;

try{

stmt=con.createStatement();

rs=stmt.executeQuery("select * from Books");

while(rs.next()){a++; }

rs=stmt.executeQuery("select * from garments");

while(rs.next()){a++;}
rs=stmt.executeQuery("select * from medicens");

while(rs.next()){a++; }


ItemsDataBase.setNoOfRecords(a);


rs=stmt.executeQuery("select * from books");

while(rs.next())
{

String id=rs.getString("id");

String description=rs.getString("description");
double
price=rs.getDouble("price");
String
category=rs.getString("category");
long stock=rs.getLong("stock");

String bookname=rs.getString("bookname");
String
authorname=rs.getString("authorname");

load.setBooks(id,description,price,category,stock,bookname,authorname);
}

}
}

I jope now u can understand

About | Sitemap | Contact