JSP导出Oracle数据表是一个在开发应用时的常用的操作,那么如何实现呢?现在开始我们的程序介绍,首先下面的程序expddl.jsp可导出Oracle数据表进行数据库的用户下的所有数据表的DDL.
JSP导出的Oracle数据表之expddl.jsp:
- ﹤%@ page contentType="text/html; charset=gb2312" language="java" %﹥
- ﹤%@ page import = "java.sql.*" %﹥
- ﹤%@ page import = "oracle.jdbc.OracleDatabaseMetaData" %﹥
- ﹤%@ page import = "oracle.jdbc.OracleResultSetMetaData" %﹥
- ﹤%@ page import = "javax.sql.*,javax.naming.Context" %﹥
- ﹤%@ page import = "javax.naming.InitialContext" %﹥
- ﹤div align="center"﹥
- ﹤%
- DataSource ds = null;
- Context ic = new InitialContext();
- ds = (DataSource) ic.lookup("java:comp/env/jdbc/mydb");
- Connection conn = ds.getConnection();
- OracleDatabaseMetaData dmd = (OracleDatabaseMetaData) conn.getMetaData();
- String us = odmd.getUserName();
- String[] types = new String[1];
- types[0] = "TABLE";
- ResultSet rs = odmd.getTables(null,us,"%",types);
- while(rs.next()) {
对JSP导出的Oracle数据表进行取表名
- String tname = rs.getString(3);
- %﹥
- ﹤div align="center"﹥表名:﹤%= tname%﹥﹤/div﹥
- ﹤table width="483" height="50" border="1" cellpadding="2" cellspacing="0"﹥
- ﹤tr﹥
- ﹤td﹥﹤div align="center"﹥序号﹤/div﹥﹤/td﹥
- ﹤td﹥﹤div align="center"﹥标识﹤/div﹥﹤/td﹥
- ﹤td﹥﹤div align="center"﹥类型/精度﹤/div﹥﹤/td﹥
- ﹤td﹥﹤div align="center"﹥标识说明﹤/div﹥﹤/td﹥
- ﹤/tr﹥
- ﹤%
- Statement st = conn.createStatement();
- ResultSet rs1 = st.executeQuery("select * from " + tname);
- OracleResultSetMetaData rmd = (OracleResultSetMetaData) rs1.getMetaData();
对JSP导出的Oracle数据表提取取列总数
- int colcnt = ormd.getColumnCount();
对JSP导出的Oracle数据表提取列序号
- int num = 1;
- for(int j=0;j﹤colcnt;j++) {
- String s = ormd.getColumnTypeName(j+1).toLowerCase();
- 对Oracle数据表只处理varchar2 char date number类型(根据需要添加相应的处理)
- if(s.equals("varchar2") || s.equals("char") || s.equals("date") || s.equals("number")) {
- if(s.equals("varchar2") || s.equals("char"))
- ss = s + "(" + ormd.getColumnDisplaySize(j+1) + ")";
- if(s.equals("number"))
- ss = s + "(" + ormd.getPrecision(j+1) + "," + ormd.getScale(j+1) + ")";
- %﹥
- ﹤tr﹥
- ﹤td width="40"﹥﹤div align="center"﹥﹤%= num++ %﹥﹤/div﹥﹤/td﹥
- ﹤td width="98"﹥﹤%=ormd.getColumnName(j+1)%﹥﹤/td﹥﹤td width="144"﹥﹤%= s %﹥﹤/td﹥
- ﹤td width="173"﹥ ﹤/td﹥ ﹤/tr﹥
- ﹤%
- }
- }
- rs1.close();
- st.close();
- %﹥
- ﹤/table﹥
- ﹤br﹥
- ﹤%
- }
- rs.close();
- conn.close();
- %﹥
- ﹤/div﹥
通过上面对JSP导出的Oracle数据表的操作,你是否对于这个程序的实现有了自己的思路呢?
【编辑推荐】