JDBC连接数据库,无法连接,求助

来源:百度知道 编辑:UC知道 时间:2024/06/27 06:12:29
我已将SQL打包SP4,而且也安装了JDBC驱动,也将那三个包导入,也设置了环境变量。但是,就是无法显示成功连接。
这是连接代码:
<!--connectSqlServer.jsp-->
<% page import="java.sql.*" contentType="text/html;charset=gb2312"%>
<html>
<head>
<title>连接数据库</title>
</head>
<body>
<%
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs";
//pubs为要连接SQL 的数据库
//SQL为数据库用户名
String user="sa";
//SQL数据库用户密码
String password="";
Connection conn=DriverManager.getConnection(url,user,password);
if(!conn.isClosed())
out.print("连接成功");
conn.close();
%>
</body>
</html>
但是,运行后,出错如下:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 2 in the jsp file: /te

呵呵。。是你粗心了。。
你把<% page import="java.sql.*" contentType="text/html;charset=gb2312"%>
改为
<%@ page import="java.sql.*" contentType="text/html;charset=gb2312"%>
就可以了。。我已经测试成功了

<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.sql.*" %>
<html>
<head>
<title>连接数据库</title>
</head>
<body>
<%
Class.forName

("com.microsoft.jdbc.sqlserver.SQLServerDriver").newInstance();
String

url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=hello";
//hello为要连接SQL 的数据库
Connection conn=DriverManager.getConnection(url,"nihao","hao");
//nihao为数据库用户名,hao用户密码
if(!conn.isClosed())
out.print("连接成功");
conn.close();
%>
</body>
</html>
这样就行了 用原