<%@ 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
数据库操作成功,恭喜你