解释及说明在代码的注释中即可查看
<span style="font-size:12px"><span style="font-size:14px"><%@ page language="java" import="java.sql.*,java.io.*,java.util.*"%>
<%@ page contentType="text/htmlcharset=utf-8"%>
<html>
<head>
<style type="text/css">
table {
border: 2px #CCCCCC solid
width: 360px
}
td,th {
height: 30px
border: #CCCCCC 1px solid
}
</style>
</head>
<body>
<%
//驱动程序名
String driverName = "com.mysql.jdbc.Driver"
//数据库用户名
String userName = "root"
//密码
String userPasswd = "szy"
//数据库名
String dbName = "studentmanage"
//表名
String tableName = "student"
//连接字符串
String url = "jdbc:mysql://数据库地址:端口号/" + dbName + "?user="
+ userName + "&password=" + userPasswd
Class.forName("com.mysql.jdbc.Driver").newInstance()
Connection connection = DriverManager.getConnection(url)
Statement statement = connection.createStatement()
String sql = "SELECT * FROM " + tableName
ResultSet rs = statement.executeQuery(sql)
%>
<table align="center">
<tr>
<th>
<%
out.print("学号")
%>
</th>
<th>
<%
out.print("姓名")
%>
</th>
<th>
<%
out.print("专业")
%>
</th>
<th>
<%
out.print("班级")
%>
</th>
</tr>
<%
while (rs.next()) {
%>
<tr>
<td>
<%
out.print(rs.getString(1))
%>
</td>
<td>
<%
out.print(rs.getString(2))
%>
</td>
<td>
<%
out.print(rs.getString(3))
%>
</td>
<td>
<%
out.print(rs.getString(4))
%>
</td>
</tr>
<%
}
%>
</table>
<div align="center">
<%
out.print("数据查询成功,恭喜你")
%>
</div>
<%
rs.close()
statement.close()
connection.close()
%>
</body>
</html></span><span style="font-size:24pxcolor: rgb(255, 0, 0)">
</span></span>
显示结果如下所示:
在页面中写Java片段 比如:<%
//驱动程序名
String driverName = "com.mysql.jdbc.Driver"
//数据库用户名
String userName = "自己的"
//密码
String userPasswd = "自己的"
//数据库名
String dbName = "自己的"
//表名
String tableName = "自己的"
//联结字符串
String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+ userName + "&password=" + userPasswd
Class.forName("com.mysql.jdbc.Driver").newInstance()
Connection connection = DriverManager.getConnection(url)
Statement statement = connection.createStatement()
String sql = "SELECT * FROM " + tableName
ResultSet rs = statement.executeQuery(sql)
%>
jsp从mysql数据库读取数据,并填充到树形结构菜单并展现出来的实现方法:
1、引入jquery.treeview.js树控件
<script type="text/javascript" src="jquery/easyui/jquery.min.js"></script>
<script type="text/javascript" src="jquery/easyui/jquery.easyui.min.js"></script>
2、jsp页面中获取后台mysql数据,并传到jsp页面来
<%
// 数据库的名字
String dbName = "zap"
// 登录数据库的用户名
String username = "sa"
// 登录数据库的密码
String password = "123"
// 数据库的IP地址,本机可以用 localhost 或者 127.0.0.1
String host = "127.0.0.1"
// 数据库的端口,一般不会修改,默认为1433
int port = 1433
String connectionUrl = "jdbc:sqlserver://" + host + ":" + port + "databaseName=" + dbName + "user=" + username
+ "password=" + password
//
//声明需要使用的资源
// 数据库连接,记得用完了一定要关闭
Connection con = null
// Statement 记得用完了一定要关闭
Statement stmt = null
// 结果集,记得用完了一定要关闭
ResultSet rs = null
try {
// 注册驱动
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
// 获得一个数据库连接
con = DriverManager.getConnection(connectionUrl)
String SQL = "SELECT * from note"
// 创建查询
stmt = con.createStatement()
// 执行查询,拿到结果集
rs = stmt.executeQuery(SQL)
while (rs.next()) {
%>
<tr>
3、填充树形菜单:
{
id : "string" // will be autogenerated if omitted
text : "string" // node text
icon : "string" // string for custom
state : {
opened : boolean // is the node open
disabled : boolean // is the node disabled
selected : boolean // is the node selected
},
children : [] // array of strings or objects
li_attr : {} // attributes for the generated LI node
a_attr : {} // attributes for the generated A node
}
$('#tree').jstree({
'core' : {
'data' : function (obj, cb) {
cb.call(this,
['Root 1', 'Root 2'])
}
}})
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)