環境設置:jdbc驅動程序
1.msbase.jar,mssqlserver.jar,msutil.jar復制到tomcat目錄commonlib
2.設置環境變量classpath
數據庫環境
sql-server2000(sp4補丁)
服務器環境
tomcat5.0虛擬服務器設置
1.編輯tomcat目錄confserver.XML,添加
<Context path="/friends" reloadable="true" docBase="E:eclipseworkspacefriends" workDir="E:eclipseworkspacefrIEndswork"> </Context>
path:虛擬服務器名
docBase:物理路徑
workDir:工作目錄
reloadable="true"默認
2.啟動tomcat服務
編寫JSP文件
<%@ page language="java" contentType="text/Html;charset=gb2312"%> <%@ page import="Java.sql.*"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <meta http-equiv="Content-Type" content="text/Html; charset=GB2312"> <title>Insert title here</title> <body> <% //變量聲明 Connection conn; ResultSet rs; Statement st; //變量聲明結束 Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");//jdbc驅動 //數據庫連接字符串 String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=zy"; conn= DriverManager.getConnection(url,"sa",""); //設置conn //設置st st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); rs=st.executeQuery("select * from zyInfo");//sql查詢語句 while(rs.next()){ out.println(rs.getString("zyInfoID"));//循環讀取字段 } %> </body> </Html>