谢谢楼上的朋友!我运行了,不知为什么就是运行不出来!
<%@ 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:8081;DatabaseName=tempdb";
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 sysusers";
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:8081/helloworldweb/MyJsp.jsp
默认sql用户名“sa”密码为空!如果用上面的代码测试,报错:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /MyJsp.jsp at line 6
3: <html>
4: <body>
5: <%
6: Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
7: String url="jdbc:microsoft:sqlserver://localhost:8081;DatabaseName=tempdb";
8: String user="sa";
9: String password="";
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:515)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:408)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
root cause