Servlet连接MSSQL的问题(100分)

  • 主题发起人 主题发起人 balaschen
  • 开始时间 开始时间
B

balaschen

Unregistered / Unconfirmed
GUEST, unregistred user!
我安装完驱动程序后,在JSP中能连接上数据库,但同样的代码在Servlet文件编译出错。
出错的地方在Class.forName;DriverManager.getConnection等地方,我的完整代码如下:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class connDB extends HttpServlet {
static final private String CONTENT_TYPE = "text/html;
charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public voiddo
Get(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>connDB</title></head>");
out.println("<body>");
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
String user="sa";
String password="";
Connection conn=DriverManager.getConnection(url,user,password);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="Select * from authors";
ResultSet rs=stmt.executeQuery(sql);
out.println("<table border=1 width=100%>");
while(rs.next()){
out.println("<tr><td>");
out.println(rs.getString(1));
out.println("</td><td>");
out.println(rs.getString(2));
out.println("</td>");
out.println("</tr>");
}
out.println("</table>");
out.println("</body></html>");
}
//Clean up resources
public void destroy() {
}
}
服务器是Tomcat4.0.6
请各位大虾诊断诊断,谢了!
 
you must throw SQLException.
 
谢谢jeoky,问题解决了,加了SQLException、ClassNotFoundException、InstantiationException和IllegalAccessException
请问能不能简化一点,比如就一个语句捕获所有的异常?
 
问题彻底解决,结贴!
 
就用
try{
.....
}
catch(Exception e){
e.printStackTrace();
...........
}
 
后退
顶部