----------------解决方案--------------------------------------------------------
如果是直连的话,就需要导入SQL的那三个JAR包,不需要设置数据源,但是你要是桥连的话,就必须设置数据源
----------------解决方案--------------------------------------------------------
Class.forName的参数取决于你用什么方式连接数据库了 比如ODBC技术(实际开发中并不常用) 那需要先配置数据源 参数就该是"sun.jdbc.odbc.JdbcOdbcDriver" 如果是纯java驱动那参数就是"com.microsoft.jdbc.sqlserver.SQLServerDriver"并要下载驱动
----------------解决方案--------------------------------------------------------
我今天晚上试了下,还是不行:
<%@ 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/MyDatabase";
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>
这个是我的程序,我导入了3个jar包的,我自己建了一个MyDatabase数据库,里面有张test表:
然后下面的是出错信息:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: [Microsoft][SQLServer 2000 Driver for JDBC]Unable to connect. Invalid URL.
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.testsqlserver_jsp._jspService(testsqlserver_jsp.java:85)
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.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Unable to connect. Invalid URL.
com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.apache.jsp.testsqlserver_jsp._jspService(testsqlserver_jsp.java:51)
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.
请教各位!究竟是怎么回事啊???
----------------解决方案--------------------------------------------------------
谁说要配置数据源了,按你写法就不需要配置数据源
----------------解决方案--------------------------------------------------------
使用〔控制面版〕〔管理工具〕〔数据源 (ODBC)〕创建新的MSSQL数据源,(添加时选择sql server)把数据库弄上去就可以
我晕......
还有谁用这个连接数据库的啊, 我想死的心都有了!!
哈哈..............
----------------解决方案--------------------------------------------------------
我今天晚上试了下,还是不行:
<%@ 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/MyDatabase";
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>
这个是我的程序,我导入了3个jar包的,我自己建了一个MyDatabase数据库,里面有张test表:
然后下面的是出错信息:
HTTP Status 500 -
--------------------------------------------------------------------------------
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: [Microsoft][SQLServer 2000 Driver for JDBC]Unable to connect. Invalid URL.
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:758)
org.apache.jsp.testsqlserver_jsp._jspService(testsqlserver_jsp.java:85)
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.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Unable to connect. Invalid URL.
com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
java.sql.DriverManager.getConnection(Unknown Source)
org.apache.jsp.testsqlserver_jsp._jspService(testsqlserver_jsp.java:51)
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.
请教各位!究竟是怎么回事啊???
你启动Tomcat之后把你上面代码在一个jsp页面里面调试下啊
稍微将上面代码改下
数据库连接字符串改成:
String url ="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=MyDatabase";
Connection conn = DriverManager.getConnection(url,"sa","");
String sql = "select * from test";
PreparedStatement stm = conn.prepareStatement(sql);
ResultSet rss = stm.executeQuery();
while(rss.next())
{
System.out.println("值为:"+rss.getString(1));
}
下面的跟你的都是一样的,先在jsp页面上调式通了在说啊
----------------解决方案--------------------------------------------------------
好,回去先试试,谢谢!
----------------解决方案--------------------------------------------------------
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:driver={sql server};server=.;database=数据库名","用户名","密码");
试试 这个 不用做数据源的桥连
----------------解决方案--------------------------------------------------------
一个问题,开两个帖子干嘛
----------------解决方案--------------------------------------------------------