Exporting Oracle data table DDL with JSP

Source: Internet
Author: User
Tags date table name
js|oracle| data these days busy sorting out ISO documents, also do not know their own maintenance of the database manual is complete, forget simply write a program all to export to forget; The following program expddl.jsp the DDL for all data tables under the user who is currently connected to the database.

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 ODMD = (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 ()) {
Fetch table Name
String tname = rs.getstring (3);
%>
<div align= "center" > table name: <%= tname%></div>
<table width= "483" height= "border=" 1 "cellpadding=" 2 "cellspacing=" 0 ">
<tr>
<td><div align= "center" > Serial number </div></td>
<td><div align= "center" > Logo </div></td>
<td><div align= "center" > type/Precision </div></td>
<td><div align= "Center" > Logo Description </div></td>
</tr>
<%
Statement st = Conn.createstatement ();
ResultSet rs1 = St.executequery ("SELECT * from" + tname);
Oracleresultsetmetadata ORMD = (oracleresultsetmetadata) rs1.getmetadata ();
Number of columns taken
int colcnt = Ormd.getcolumncount ();
Column ordinal
int num = 1;
for (int j=0;j<colcnt;j++) {
String s = ormd.getcolumntypename (j+1). toLowerCase ();
Process only VARCHAR2 char date number type (add appropriate processing as needed)
if (S.equals ("varchar2") | | | s.equals ("char") | | | s.equals ("date") | | s.equals ("number") {
if (S.equals ("varchar2") | | | s.equals ("char")
s = s + "(" + ormd.getcolumndisplaysize (j+1) + ")";
if (S.equals ("number"))
s = s + "(" + ormd.getprecision (j+1) + "," + Ormd.getscale (j+1) + ")";
%>
<tr>
&LT;TD width= "><div" align= "center" ><%= num++
&LT;TD width= "><%=ormd.getcolumnname" (j+1)%></td><td width= "144" ><%= s%></td>
&LT;TD width= "173" > </td> </tr>
<%
}
}
Rs1.close ();
St.close ();
%>
</table>
<br>
<%
}
Rs.close ();
Conn.close ();
%>
</div>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.