-
JSP Page
I have one problem that i dont know anything about Java or JSP or any web technology and my boss has given me this code to use in JSP page and test whether its working or not. I know this will be quite easy for you guys so pls help me out how to use this code and test it ..
Code:
public Connection getCon() throws java.sql.SQLException{
Connection connection = null;
String driver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dburl = "jdbc:microsoft:sqlserver://elogix:1433;DatabaseName=DSI";
String uid="sa";
String pwd="ipnc0176";
try{
Class.forName(driver).newInstance();
}catch(Exception cnfe){
System.out.println("Unable to load Driver class");
throw new SQLException("Unable to Load Driver:"+cnfe.getMessage());
}
try{
System.out.println("Getting connection...");
connection=DriverManager.getConnection(dburl,uid,pwd);
}catch(java.sql.SQLExcetion sqle){
System.out.println("SQL Exception (Connection):");
throw new SQLException("SQL Exception (Connection):"+sqle.getMessage());
}
return connection;
}
-
Re: JSP Page
Code:
String uid="sa";
String pwd="ipnc0176";
I do hope you changed these before posting.
Except for the typo here:
Code:
}catch(java.sql.SQLExcetion sqle){
I see no reason why the code shouldn't work. It's very poorly written code, yes, with some bad and some strange practices, but it's correct. You will need this at the top of the JSP page:
Code:
<% @page import="java.sql.Connection"
import="java.sql.DriverManager"
import="java.sql.SQLException" %>
-
Re: JSP Page
I am now getting error "Unable to load driver class" wat could be the reason ?
"Pls excuse i m asking basic question as i dont have any knowledge of java"
-
Re: JSP Page
It means, most likely, that the database driver is not installed correctly to be found by the webapp.
There should be a JAR containing the driver somewhere. Best method is to put it into the servlet container's "common" directory, where libraries shared by the container and the webapps reside, but you could also put it into the webapp's WEB-INF/lib directory.