Discussions

Web tier: servlets, JSP, Web frameworks: couldn't connect to database?

  1. couldn't connect to database? (5 messages)

    Hello
     
    I am trying to connect to sqlserver2000 database thru JDBC & JSP. I think it loaded the driver but cannot connect to database. I have installed sqlserver2000 in my machine, web server is tomcat 4.1.

    This is the code.

    <%@ page language="java" %>
    <%@ page import = "java.sql.*" %>

    <HTML>
    <BODY>
    <%
        try {
             // Load the JDBC driver
    Class.forName ("com.jnetdirect.jsql.JSQLDriver");

             // Create a connection to the database
            Connection connection = DriverManager.getConnection("jdbc:JSQLConnect://admin:1433","sa","");
    Statement stmt=connection.createStatement();
    }
    catch (ClassNotFoundException e)
    {
       System.out.println("Could not find the database driver");
         }
    catch (SQLException e)
    {
        System.out.println("Could not connect to the database");
    }
     %>
    </BODY>
    </HTML>


    It displays "Could not connect to the database" but "Could not find the database driver" is not display that means database driver is loaded. So what wrong?

    Threaded Messages (5)

  2. ...[ Go to top ]

    Please, don't do any database activity in jsp!!

    But back to your problem, instead of "System.out.println("Could not connect to the database")", do "e.printStackTrace();" or "System.out.println(e.getMessage());", that could give you more information of the problem.

    Jari
  3. ...[ Go to top ]

    When I gave System.out.println(e.getMessage());, driver expiry date is over message was dispalyed, its confusing. Let's start from basic, from where I will get driver to be loaded in class.forName().
    Why I shouldn't do JDBC connectivity thru JSP?
    Awaiting reply at the earliest.
  4. ...[ Go to top ]

    because its bad practise , you should try and follow the MVC pattern some papers are around on the internet not sure where frameworks like struts , webwork , tapesty , spring follow the pattern well

    if your the driver expiry message download the driver again or purchase it
  5. ...[ Go to top ]

    I loaded new driver jar files in one of the tomcat directory & its better to add jar files in classpath also.
    Thanks for the info, I could connect to database.
  6. couldn't connect to database?[ Go to top ]

    Hello I am trying to connect to sqlserver2000 database thru JDBC & JSP. I think it loaded the driver but cannot connect to database. I have installed sqlserver2000 in my machine, web server is tomcat 4.1.This is the code.<%@ page language="java" %><%@ page import = "java.sql.*" %><HTML><BODY><% try { // Load the JDBC driver Class.forName ("com.jnetdirect.jsql.JSQLDriver"); // Create a connection to the database Connection connection = DriverManager.getConnection("jdbc:JSQLConnect://admin:1433","sa",""); Statement stmt=connection.createStatement(); } catch (ClassNotFoundException e) { System.out.println("Could not find the database driver"); } catch (SQLException e) { System.out.println("Could not connect to the database"); } %></BODY></HTML>It displays "Could not connect to the database" but "Could not find the database driver" is not display that means database driver is loaded. So what wrong?
    make sure you machine can access the server via dns else replace admin with the ip address and try adding a database to the connection string

    admin:1433/database=dbname