jsp 链接数据库的问题,高手请进

lifeisfirst 2009-09-27 10:05:47
<%@ page contentType="text/html;charset=gb2312"%>

<%@ page import="java.sql.*"%>

<html>

<body>

<%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";

//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 test";

ResultSet rs=stmt.executeQuery(sql);

while(rs.next()) {%>

您的第一个字段内容为:<%=rs.getString(1)%>

您的第二个字段内容为:<%=rs.getString(2)%>

<%}%>

<%out.print("数据库***作成功,恭喜你");%>

<%rs.close();

stmt.close();

conn.close();

%>

</body>

</html>

在浏览器中输入http://localhost:8080/sql.jsp后出现如下问题 !
type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: com.microsoft.jdbc.sqlserver.SQLServerDriver
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.sql_jsp._jspService(sql_jsp.java:105)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root cause

java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1340)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1189)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:148)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:69)
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
java.lang.Class.forName0(Native Method)
java.lang.Class.forName(Class.java:141)
org.apache.jsp.sql_jsp._jspService(sql_jsp.java:53)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note The full stack trace of the root cause is available in the Apache Tomcat/5.0.28 logs.
...全文
50 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lifeisfirst 2009-09-28
  • 打赏
  • 举报
回复
真么解决呀!??? 俺现在自学jsp!!大侠,指点指点!
a404125339 2009-09-28
  • 打赏
  • 举报
回复
没有连接到数据库,给你个例子,Java和其差不多:
//连接数据库
public class ConnectionDB {
public static Connection getConnection()
{
Connection con=null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
con=DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433;databasename=TestUser", "sa", "sa");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
}
、//判断添加用户是否成功
public boolean AddUser(Students s)
{
boolean flag=false;
Connection con=null;
PreparedStatement pstmt=null;
String sql="insert into Students(username,age,sex) values(?,?,?)";
try {
con=ConnectionDB.getConnection();
pstmt=con.prepareStatement(sql);
pstmt.setString(1, s.getUsername());
pstmt.setInt(2, s.getAge());
pstmt.setString(3, s.getSex());
int count=pstmt.executeUpdate();
if(count>0) flag=true;
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
im110 2009-09-28
  • 打赏
  • 举报
回复
你需要下载一个SQL Server数据库的jdbc驱动包,然后将其拷贝到应用的/WebRoot/WEB-INF/lib目录下。
shiyiwan 2009-09-27
  • 打赏
  • 举报
回复
sqlserver的驱动包没找到

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧