cannot connect servlet cu mssql database
I`m trying to create an on-line booking application for a restaurant. An html file invokes a servlet. This servlet uses a MSSQL database to keep track of the bookings. The problem is that the servlet throws an exception when trying to connect to the database(the getMessage() method returns this: "com.microsoft.jdbc.sqlserver.SQLServerDriver"). I have included msbase.jar, mssqlserver.jar and msutil.jar for the jdbc driver. I have done this way every time I used MSSQL and it always worked Is there something else I need to do to get a servlet to connect to MSSQL?
Here is the servlet`s code:
public class AddReservation extends HttpServlet{
public void doGet(HttpServletRequest cerere, HttpServletResponse raspuns)
throws ServletException, IOException{
Pr开发者_运维问答intWriter out = raspuns.getWriter();
String nume,zi,luna,an,data,ora,minute;
//get parameters from html
raspuns.setContentType("text/html");
out.println("<HTML><HEAD></HEAD><BODY>");
if(nume.equals("")||zi.equals("")||luna.equals("")||an.equals("")||ora.equals("")||minute.equals("")){
out.println("<H3>NU S-AU COMPLETAT TOATE CAMPURILE</H3>");
}
else{
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://LAPTOP-TITI;DatabaseName=Restaurant_Servlet","sa","pass313307");
Statement st = con.createStatement();
data = zi+"/"+luna+"/"+an;
ora = ora +":"+minute;
ResultSet rs;
rs = st.executeQuery("select count(*) from Rezervari where Data ='"+data+"' and Ora ='"+ora+"'");
rs.next();
int n = rs.getInt(1);
if(n==2){
out.println("<H3>NU EXISTA LOCURI LIBERE</H3>");
}
else{//add to db
st.executeUpdate("insert into Rezervari(Nume, Data, Ora) values ('"+nume+"','"+data+"','"+ora+"')");
out.println("<H3>REZERVARE ADAUGATA</H3>");
}
rs.close();
st.close();
con.close();
}
catch(Exception e){
out.println("<H3>NU S-A REALIZAT CONEXIUNE LA BAZA DE DATE<br><br>"+
e.getMessage()+"</H3>");
}
}
out.println("</BODY></HTML>");
out.close();
}
public void doPost(HttpServletRequest cerere, HttpServletResponse raspuns)
throws ServletException, IOException{
doGet(cerere,raspuns);
}
}
p.s. I am using apache tomcat server on windows Xp
the getMessage() method returns this: "com.microsoft.jdbc.sqlserver.SQLServerDriver"
First of all, don't print only the exception message. Print the whole exception and the trace to the server logs as well. This give more valuable information.
e.printStackTrace();
The exception message which only contains an full qualified classname is however typical for a ClassNotFoundException
. This in turn means that the JDBC driver isn't in the classpath. You need to put the JAR file(s) in /WEB-INF/lib
folder of your webapp. This folder is part of the webapp's runtime classpath.
Unrelated to the problem: a servlet is not the right place to output HTML. There the JSP is for. It's actually also not the right place to write DB connectivity code straight in, but that's a whole story apart :)
精彩评论