MyEclipse------如何查詢MySQL數據庫里面表的信息


testExecuteQuary.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"
            + request.getServerName() + ":" + request.getServerPort()
            + path + "/";
%>
<%@page import="java.sql.*" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'testExecuteQuery.jsp' starting page</title>
</head>

<body>
    <%
    //student為數據庫的名字 
    String url="jdbc:mysql://localhost:3306/student?useSSL=true";
    String useName="root";
    String password="2277092";
    String sql=null;
    Connection conn=null;
    Statement stmt=null;
    
    //加載驅動類
    try{
        Class.forName("com.mysql.jdbc.Driver");
        out.print("加載驅動類成功");
    }
    catch(ClassNotFoundException e){
        out.print("加載驅動類時出現異常");
    }
    
    try{
        conn=DriverManager.getConnection(url,useName,password);
        
        stmt=conn.createStatement();
        sql="select * from info";//info為表名
        
        ResultSet rs=stmt.executeQuery(sql);
    %>
    
    <center>
    <table border="1" width="270" cellspacing="0" cellpadding="0">
        <tr>
            <td width=50 align="center" valign="middle">學號</td>
            <td width="80" align="center">姓名</td>
            <td width=140 align="center">電話</td>
        </tr>
        
        <%
            while(rs.next()){
                int id=rs.getInt(1);
                String name=rs.getString(2);
                String phone=rs.getString(3);
         %>
         
         <tr>
             <td height="40" align="center" valign="middle"><%=id %></td>
             <td align="center" valign="middle"><%=name %></td>
             <td align="center" valign="middle"><%=phone %></td>
         </tr>
         
         <%
             }
             rs.close();
             stmt.close();
          %>    
    </table>
    </center>
    
    <%
    }
    catch(SQLException e){
        out.print("出現SQLException異常");
    }
    finally{
        try{
            if(conn!=null){
                conn.close();
            }
            }
            catch(SQLException e){
                out.print("關閉數據庫連接時出現異常");
            }
        }
    %>
</body>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM