JSP之连接SQL Server

1、在SQL Server中启用sa(请参考本人博客:http://www.cnblogs.com/zhouhb/archive/2011/02/15/1955324.html)
2、在SQL Server配置管理器中启用TCP/IP

3、双击TCP/IP,在弹出的窗口中选择“IP地址选项卡”,将IP1【IP地址】设为127.0.0.1

4、在上一步的窗口中将滚动条拖到最下方,将IPAll中的【TCP端口】设置成【1433】

5、重启SQL Server服务
6、下载数据库厂商提供的驱动程序包sqljdbc4.jar,拷贝到web项目的WEB-INF\lib目录下
7、编程,通过纯Java驱动方式与数据库建立连接

<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<%@ page import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
<%
String URL = "jdbc:sqlserver://127.0.0.1:1433;databaseName=addresslist";
Connection con = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection(URL, "sa", "123");
out.print("连接成功<br/>");
} catch (Exception e) {
    e.printStackTrace();
  } finally {
  try {
      con.close();
    } catch (Exception e2) {
  }
}
%>
</body>
</html>

页面上显示“连接成功”,则表示配置正确。

 

posted @ 2017-04-28 08:24  zhouhb  阅读(9503)  评论(0编辑  收藏  举报