java文件
package xscj_bean;
import java.sql.*;
public class SQLServerConnBean {
private Statement stmt = null;
private Connection conn = null;
ResultSet rs = null;
//构造函数
public SQLServerConnBean () { }
public void OpenConn( ) throws Exception
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
String url = "jdbc:sqlserver://localhost:1433;databaseName=classcircle";
String user = "sa";
String password = "123456789123456";
conn = DriverManager.getConnection( url, user, password );
}
catch(SQLException e)
{
System.err.println("Data.executeQuery: " + e.getMessage());
}
}
//执行查询类的SQL语句,有返回集
public ResultSet executeQuery(String sql)
{
rs = null;
try
{
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE
,ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
}
catch(SQLException e)
{
System.err.println("Data.executeQuery: " + e.getMessage());
}
return rs;
}
//关闭对象
public void closeStmt()
{
try
{
stmt.close();
}
catch(SQLException e)
{
System.err.println("Date.executeQuery: " + e.getMessage());
}
}
public void closeConn()
{
try
{
conn.close();
}
catch(SQLException e)
{
System.err.println("Data.executeQuery: " + e.getMessage());
}
}
}
jsp文件
<%@ page contentType="text/html; charset=utf-8"%>
<%@ page language="java" import="java.sql.*"%>
<jsp:useBean id="SqlBean" scope="page" class="xscj_bean.SQLServerConnBean" />
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>JDBC连接SQL Server数据库</title>
<style type="text/css">
<!--
.style1{
color:#ff0000;
font-size:24px;
}
-->
</style>
</head>
<body>
<div align="center">
<span class="style1">JDBC连接SQL Server数据库</span><br><hr><br>
</div>
<table border="2" bordercolor="#ffcccc" align="center">
<tr bgcolor="cccccc" align="center">
<td>学号</td>
<td>姓名</td>
<td>专业</td>
<td>总学分</td>
</tr>
<%
//查询XSB表中XH,XM,ZY和ZXF字段的前10条记录
String sql="select top 10* From student";
SqlBean.OpenConn( ); //调用SqlBean中加载JDBC驱动的成员函数
ResultSet rs=SqlBean.executeQuery(sql); //取得结果集
while( rs.next( ) )
{
%>
<tr>
<td><%=rs.getString("username")%></td>
<td><%=rs.getInt("password")%></td>
<td><%=rs.getInt("schoolnumber")%></td>
<td><%=rs.getInt("ZXF")%></td>
</tr>
<%
}
%>
<%
out.print("数据库操作成功,恭喜你!");
rs.close();
SqlBean.closeStmt();
SqlBean.closeConn();
%>
</table>
</body>
</html>
结果却是这样的
小白求助啊!!!!