JSP连接数据库的简单查询

需求:测试人员偶尔会需要查询某些不会显示在项目中的数据,但又不想开数据库查。

功能:JSP直连数据库,进行简单查询并将返回结果组合成table

代码:

<%@ page contentType="text/html; charset=utf-8" %>   
<%@ page language="java" %>   
<%@ page import="com.mysql.jdbc.Driver" %>   
<%@ page import="java.sql.*" %>   
<%   
    String url="jdbc:mysql://localhost/storageplus?user=root&password=root;   
    Class.forName("com.mysql.jdbc.Driver").newInstance();   
    Connection conn=DriverManager.getConnection(url);   
    Statement stmt = conn.createStatement();   
    String sql="SELECT STORAGE_ID,STORAGE_NAME,MGMT_IP FROM ST_STORAGE";   
    ResultSet rs = stmt.executeQuery(sql); 
    out.print("

阵列信息查询

"); out.print(""); out.print(""); out.print(""); out.print(""); out.print(""); while(rs.next()) { out.print(""); out.print(""); out.print(""); out.print(""); out.print(""); } out.print("
阵列ID阵列名称管理IP
"+rs.getString(1)+""+rs.getString(2)+""+rs.getString(3)+"
"); rs.close(); stmt.close(); conn.close(); %>
这是查了总集,单独一条的话,连table都省了。


你可能感兴趣的:(java,web)