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

用Myeclipse写的java程序可以连接数据库而jsp就不行,为什么?
这是java代码可以成功输出:
import java.sql.*;

public class test {

public static void main(String[] srg){

int i=0;
String driverName ="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动

 String dbURL = "jdbc:sqlserver://localhost:1433;DatabaseName=bank"; //连接服务器和数据库test

String userName = "sa"; //默认用户名

String userPwd = "123456"; //密码

Connection con;

Statement sql;

ResultSet rs;
try{
  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  }
 catch(ClassNotFoundException e){
  System.out.println("没有找到数据库!");
  }
 try{
  con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=bank","sa","123456");
  sql=con.createStatement();
  rs=sql.executeQuery("SELECT * FROM customer");
  System.out.println("下面分别代表:"+" "+"customer_name"+" "+"customer_street"+" "+"customer_city");
  while(rs.next())
  {
i++;
  System.out.println("第"+i+"个用户:"); 
  System.out.println(rs.getString("customer_name")+" "+rs.getString("customer_street")+" "+rs.getString("customer_city"));
  }
  con.close();
  }
  catch(SQLException e){
System.out.println("SQL异常!!!");
  }  
}
}




下面是jsp的,输出没有找到数据库和SQL异常:
%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
  <title>BankDataBase</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">


  </head>
  
  <body> 
   
  <% Connection con;
  Statement sql;
  ResultSet rs;
  try{
  Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  }
  catch(ClassNotFoundException e){
  out.println("没有找到数据库!");
  }
  try{
  con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=bank","sa","123456");
  sql=con.createStatement();
  rs=sql.executeQuery("SELECT * FROM customer");
  out.println("<table border>");
  out.println("<tr><td colspan=3 align=center>用户数据</td></tr>");
  out.println("<tr>"); 
  out.println("<td>customer_name</td>"); 
  out.println("<td>customer_