trying to query mssql database using JSP. connected the database using ODBC
but this code is unable to print the result on screen .am using tomcat as a server.i don’t know where am doing mistake. please help me to solve in this.
<%@page import="java.sql.*"%>
<%@ page import="java.util.*" %>
<table border=6>
<tr><th>fname</th><th>lname</th></tr>
<%
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:ServerDB","","");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("select * from dbo.company");
while(rs.next()){
%>
<tr><td><%=rs.getString("fname")%></td><td><%=rs.getString("lname")%></td></tr>
<%
}
rs.close();
st.close();
con.close();
}
catch(Exception e){e.printStackTrace();
System.out.println("Exception in connecting to DB"+e.getMessage());}
%>
</table>
Test the query independently i.e on database command line to make sure if error is in database environment,if it works fine
else i guess problem would be
Connection con = DriverManager.getConnection("jdbc:odbc:ServerDB","","");i never worked with MSSQL but in any database u gotta specify username and password which u left alone
Every thing in your code is just fine i guess problem is in connectivity!
Revise your connectivity code or reinstall driver