这里以SQL 2005中的InterLib数据库为例,获取InterLib/tb_booktype表中的内容。效果如下:
其JSP代码如下:
<%@ page language="java" import="java.sql.*" pageEncoding="GB2312"%> <%@ page import="java.util.*" %> <html> <head> <title>应用Statement对象查询数据</title> </head> <body> <% String driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"; String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=InterLib"; String username = "admin"; String password = "123456"; Class.forName(driverClass); Connection conn=DriverManager.getConnection(url, username, password); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from tb_booktype"); out.println("SQL2005连接成功!</p>"); out.println(" --- tb_booktype表中的内容如下:----"); while(rs.next()){ out.println("<br>编号: "+rs.getString(1)+" 书名:"+rs.getString(2)+" 价格:"+rs.getString(3)); } rs.close(); stmt.close(); conn.close(); %> </body> </html>
一个测试SQL2005数据库连接的JSP文件
时间: 2025-01-02 16:12:51