大家好,问题一个,能否请指教:是JSP的 JDBC-ODBC桥连接SQLSERVER的
TOMCAT,SQL SERVER2000 FOR JDBC,SQL SERVER2000 SP3都已安装,ODBC设好了,CLASSPATH环境变
量也设好了
代码是:
<%@ page import="java.sql.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.util.*"%>
<html>
<head><title>sdf</title></head>
<body>
sdfasdfadfsdfasdfadfsdfasdfadfsdfasdfadf
<%
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();
Connection con=DriverManager.getConnection("jdbc:odbc:ggy","gg","gg");
Statement sql=con.createStatement();
ResultSet rs=sql.executeQuery("SELECT * FROM student");
%>
<%=rs.getString("sd")%>
<%
con.close();
%>
</body>
</html>
但还是出错,提示是:
javax.servlet.ServletException: [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指
定默认驱动程序
org.apache.jasper.runtime.PageContextImpl.doHandlePageException
(PageContextImpl.java:825)
org.apache.jasper.runtime.PageContextImpl.handlePageException
(PageContextImpl.java:758)
org.apache.jsp.text_jsp._jspService(text_jsp.java:160)
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)
同样的设置我用驱动连接又成功,驱动代码是:
<%@ page contentType="text/html; charset=GBK" language="java"
import="java.sql.*,java.io.*"%>
<html>
<body>
以下是从Ms sql server数据库读取的数据:<hr>
<table border=1>
<tr><td>id</td><td>书名</td><td>出版社</td><td>价格</td></tr>
<%! String trans(String chi)
{
String result = null;
byte temp [];
try
{
temp=chi.getBytes("iso-8859-1");
result = new String(temp);
}
catch(UnsupportedEncodingException e)
{
System.out.println (e.toString());
}
return result;
}
%>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
Connection con=java.sql.DriverManager.getConnection
("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=MJ","gg","gg");
Statement stmt=con.createStatement();
ResultSet rst=stmt.executeQuery("select * from student;");
while(rst.next())
{
out.println("<tr>");
out.println("<td>"+trans(rst.getString("sd"))+"</td>");
out.println("<td>"+trans(rst.getString("sdf"))+"</td>");
out.println("<td>"+trans(rst.getString("ae"))+"</td>");
out.println("</tr>");
}
rst.close();
stmt.close();
con.close();
%>
</table>
</body>
</html>