zoukankan      html  css  js  c++  java
  • jsp查询数据库

    <%@ page import="java.sql.Statement" %>
    <%@ page import="java.sql.ResultSet" %>
    <%@ page import="java.sql.Connection" %>
    <%@ page import="java.sql.DriverManager" %><%--
    Created by IntelliJ IDEA.
    User: Administrator
    Date: 2019/9/24
    Time: 9:30
    To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>把数据库的表显示在页面</title>
    </head>
    <body>
    -%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>mysql测试</title>
    </head>
    <body>
    -%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
    <title>mysql测试</title>
    </head>
    <body>

    <%
    try {
    Class.forName("com.mysql.jdbc.Driver"); ////加载数据库驱动
    String url = "jdbc:MySQL://localhost:3306/kh87?&useSSL=false&serverTimezone=UTC"; //指向数据库table
    String username = "root"; //数据库用户名
    String password = "ROOT"; //数据库用户密码
    Connection conn = DriverManager.getConnection(url, username, password);
    if(conn != null){
    out.print("数据库连接成功!");
    out.println("<br />");
    out.println("-------------------------------");
    out.print("<br />");
    Statement stmt = null;
    ResultSet rs = null;
    String sql ="select * from user"; //查询语句
    stmt = conn.createStatement();
    rs = stmt.executeQuery(sql);
    out.println("执行结果如下所示:");
    out.println("<br />");
    out.println("-------------------------------");
    out.println("<br />");
    out.println("id" + " " + "name"+" "+"age"+" ");
    out.println("<br />");
    while (rs.next()) {
    out.println(rs.getString("id")+" &nbsp "+rs.getString("name")+" &nbsp "+rs.getString("age")+" &nbsp "); //将数据库表格查询结果输出
    out.print("<br />");
    }
    }
    else{
    out.print("连接失败!");
    }
    }
    catch (Exception e) {
    out.print("数据库连接异常!");
    }
    %>
    </body>
    </html>
  • 相关阅读:
    Linux脚本文件注释
    Linux三剑客之grep命令
    Linux获取本机IP
    Linux的cut命令详解
    Linux的wc命令详解
    Linux的uniq命令详解
    Linux的sort命令详解
    shell之a+b求和脚本的三种写法
    shell的文件比较运算符和字符串比较运算符
    shell中变量$系列的含义
  • 原文地址:https://www.cnblogs.com/tiantongtong/p/13226778.html
Copyright © 2011-2022 走看看