日期:2014-05-17  浏览次数:20780 次

我要哭了啊,求大神啊,帮助啊,jsp连接SQL server 2008的问题
<%@ page language="java" import="java.util.*" import="java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <base href="<%=basePath%>">
   
  <title>商品</title>
   
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">  
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

  </head>
  
  <body>
  <table border="1">
  <tr>
  <td>编号</td>
  <td>名称</td>
  <td>生产商</td>
  <td>销售价格</td>
  <td>生产日期</td>
  </tr>
  <%
  String url="jdbc:sqlserver://localhost:1433;databaseName=Market";
  String user="sa";
  String password="database";
  Connection con=null;Statement stmt=null;
  String sql="select * from 商品";
  try{
  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
  con=DriverManager.getConnection(url,user,password);
  stmt=con.createStatement();
  ResultSet rs=stmt.executeQuery(sql);
   
  while(rs.next()){
  String id=rs.getString("商品代码");
  String name=rs.getString("商品名称");
  String product=rs.getString("生厂商");
  float price=rs.getFloat("销售价格");
  String date=rs.getString("生产日期");
  out.println("<tr><td>"+id+"</td><td>"+name+"</td><td>"+product+"</td><td>"+price+"</td><td>"+date+"</td></tr>");} 
  }catch(ClassNotFoundException e){
  out.print("类没有找到");
  out.println(e.getMessage());
  } catch(SQLException e)
  {
  out.println("SQL异常"+e.getMessage());
  }finally{
  if(stmt!=null){
  try{
  stmt.close();
  } catch(SQLException e) {}
  stmt=null;
  }
  if(con!=null){
  try{
  con.close();
  } catch(SQLException e){
  }
  con=null;
  }
  }
  %>
  </table>
  </body>
</html>


------解决方案--------------------
你把jar包手动拷贝到tomcat中的你的项目下面
------解决方案--------------------
Java code
Class.forName("com.microsoft.jdbc.sqlserver.