jsp下直连SQLServer的代码
<%@page contentType=\"text/html; charset=GBK\" import=\"java.sql.*\" %>
<html>
<head>
<title>jsp1</title>
</head>
<body bgcolor=\"#ffffff\">
<%
Class.forName(\"com.microsoft.jdbc.sqlserver.SQLServerDriver\");
/*String url=\"jdbc:microsoft:sqlserver://localhost:8080;databasename=pubs\";
Connection conn=DriverManager.getConnection(url,\"sa\",\"\");
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery(\"select * from jobs\");
*/
//2.定义URI字符串
String sqlserverURL = \"jdbc:microsoft:sqlserver://localhost:1433;databaseName=pubs\";
//3.建立连接
//Connection conn = DriverManager.getConnection(\"jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;databaseName=pubs\");
Connection conn = DriverManager.getConnection(sqlserverURL, \"sa\", \"\");
//4.准备 SQL 语句容器
Statement stmt = conn.createStatement();
//5.执行 SQL 语句
ResultSet rs = stmt.executeQuery(\"select * from jobs\");
ResultSetMetaData rsmd = rs.getMetaData();
int cols = rsmd.getColumnCount();
%>
<table border=1 cellpadding=\"0\" cellspacing=\"0\">
<tr>
<%for(int i=1;i<=cols;i++){ %>
<th><%=rsmd.getColumnName(i) %></th>
<%} %>
</tr>
<%while (rs.next()) {%>
<tr>
<% for(int i=1;i<=cols;i++){%>
<td> [Page]
<%out.print(rs.getString(i));%>
</td>
<%} %>
</tr>
<%}%>
</table>
<%
rs.close();
stmt.close();
conn.close();
%>
</body>
</html>