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?
-
couldn't connect to database? (5 messages)
- Posted by: p s
- Posted on: April 28 2004 02:21 EDT
Threaded Messages (5)
- ... by Jari Rantala on April 28 2004 02:34 EDT
- couldn't connect to database? by stephen smithstone on April 28 2004 02:35 EDT
-
...[ Go to top ]
- Posted by: Jari Rantala
- Posted on: April 28 2004 02:34 EDT
- in response to p s
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 -
...[ Go to top ]
- Posted by: p s
- Posted on: April 28 2004 05:56 EDT
- in response to Jari Rantala
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. -
...[ Go to top ]
- Posted by: stephen smithstone
- Posted on: April 28 2004 07:47 EDT
- in response to p s
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 -
...[ Go to top ]
- Posted by: p s
- Posted on: April 30 2004 03:46 EDT
- in response to stephen smithstone
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. -
couldn't connect to database?[ Go to top ]
- Posted by: stephen smithstone
- Posted on: April 28 2004 02:35 EDT
- in response to p s
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