jsp怎么从mysql数据库把树形结构展现出来

jsp怎么从mysql数据库把树形结构展现出来,第1张

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'])

       }

   }})

‘不用外部数据库,添加一个数据库连接1控件:

’假设库文件名为“产品数据库.mdb”,库中的表名为“产品表”,产品字段名“类别”

全局变量:a 文本型

如果真(数据库连接1.连接Access(取运行目录()+“\产品数据库.mdb”,“”)=假)

          信息框(“连接数据库失败!”,0,)

返回()

’添加一个记录集控件;

记录集1.置连接(数据库连接1)

‘上面我们已经打开并且连接好了MDB数据库,接下来我们做读取和显示;

记录集1.打开(“select * from 产品表”,#SQL语句,#服务器端游标)

记录集1.到首记录()

判断循环首(记录集1.尾记录后=假)

      记录集1.读文本(“类别”,a)

      如果真(a=“日用品”)

        树型框1.加入项目(0,a,,,,,)

      如果真(a=“洗化用品”)

        树型框1.加入项目(1,a,,,,,)

      如果真(a=“洗发水” 或 a=“洗衣粉”)

        树型框1.加入项目(2,a,,,,,)

      记录集1.到下一条()

判断循环尾()

记录集1.关闭()


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/9632720.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存