• JSP 连接 MySQL 数据库


    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <jsp:directive.page import="com.mysql.jdbc.Driver"/>
    <jsp:directive.page import="java.sql.*"/>

    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>

    <%
    String driverName = "com.mysql.jdbc.Driver";
    String userName = "root";
    String userPasswd = "root";
    String dbName = "test";
    String tableName = "username";
    String url="jdbc:mysql://127.0.0.1:3306/"+dbName;

    Class.forName(driverName);
    Class.forName(driverName).newInstance();

    Connection connection=DriverManager.getConnection(url,userName,userPasswd);

    Statement statement = connection.createStatement();

    String sql="SELECT * FROM "+tableName;

    ResultSet rs = statement.executeQuery(sql);

    //获得数据结果集合
    ResultSetMetaData rmeta = rs.getMetaData();

    //确定数据集的列数,亦字段数
    int numColumns=rmeta.getColumnCount();

    // 输出每一个数据值
    out.print("Name");
    out.print("|");
    out.print("Gender");
    out.print("|");
    out.print("Date");
    out.print("<br>");

    while(rs.next()) {

    out.print(rs.getString(1)+" ");
    out.print("|");
    out.print(rs.getString(2));
    out.print("|");
    out.print(rs.getString(3));
    out.print("<br>");
    }

    out.print("<br>");
    out.print("数据库操作成功,恭喜您!");

    rs.close();

    statement.close();

    connection.close();

    %>

    ===========下面是数据库内容========
    mysql> show databases;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | mysql              |
    | performance_schema |
    | test               |
    +--------------------+
    4 rows in set (0.00 sec)

    mysql> use test
    Database changed
    mysql> show tables;
    +----------------+
    | Tables_in_test |
    +----------------+
    | user           |
    | username       |
    +----------------+
    2 rows in set (0.03 sec)

    mysql> describe username;
    +-------+-------------+------+-----+---------+-------+
    | Field | Type        | Null | Key | Default | Extra |
    +-------+-------------+------+-----+---------+-------+
    | name  | varchar(20) | YES  |     | NULL    |       |
    | sex   | char(1)     | YES  |     | NULL    |       |
    | birth | date        | YES  |     | NULL    |       |
    +-------+-------------+------+-----+---------+-------+
    3 rows in set (0.01 sec)

    mysql> select * from username;
    +------+------+------------+
    | name | sex  | birth      |
    +------+------+------------+
    | bob  | M    | 1985-09-09 |
    | bob  | M    | 1985-09-09 |
    +------+------+------------+
    2 rows in set (0.00 sec)

    mysql>
    =========运行结果如下=========
    Name|Gender|Date
    bob |M|1985-09-09
    bob |M|1985-09-09

    数据库操作成功,恭喜你

  • 相关阅读:
    Mysql复制表格
    MySql的导入与导出
    jQuery语法
    JavaScript中的test()方法
    Android解决java.lang.OutOfMemoryError: bitmap size exceeds VM budget(转)
    Android图片压缩方法总结
    Android实现app长时间未操作时自动退出app
    银行卡号的检测
    Android大图片裁剪终极解决方案(上:原理分析)
    Windows环境下Android Studio v1.0安装教程
  • 原文地址:https://www.cnblogs.com/backpacker/p/2725367.html
Copyright © 2020-2023  润新知