首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

怎么通过JAVA的API得到数据库里面的表

2012-01-29 
如何通过JAVA的API得到数据库里面的表我想通过JAVA的API返回该数据库所有的的表的名称。应该是把“SHOWTABLE

如何通过JAVA的API得到数据库里面的表
我想通过JAVA的API返回该数据库所有的的表的名称。
应该是把“SHOW   TABLES;”这样的命令传给mysql,我应该通过怎样的API去做这个?
Statement.executeQuery嘛还是其他的?

或者有别的方法?

[解决办法]
Connection con = ...;
DatabaseMetaData meta = con.getMetaData();
ResultSet rs = meta.getTables(null, null, null, null);
while(rs.next()) {
  System.out.println(rs.getString( "TABLE_NAME "));
}

rs 有许多列,具体的列说明可以参考 java.sql.DatabaseMetaData.getTables() 的 API 帮助。

[解决办法]
<%@ page language= "java " contentType= "text/html; charset=GB2312 " pageEncoding= "GB2312 "%>
<%@page import= "java.util.Date "%>
<html>
<head>
<title> WEBLOGIC 9 INDEX </title>
</head>
<body>
<P align= "center "> <font size=7> <b> WEBLOGIC 9 INDEX </b> </font>
<%@page import= "java.sql.* " %>
<%@page import= "java.text.* " %>
<%
String time=DateFormat.getDateTimeInstance().format(new java.util.Date());
out.println( " <P> 当前时间: "+time);
//-----------------------------------------------
int i=0;
if(application.getAttribute( "count ")==null){
application.setAttribute( "count ",i);
}else{
i=new Integer(Integer.valueOf(application.getAttribute( "count ").toString()));
i++;
application.setAttribute( "count ",i);
}
out.println( " <P> 浏览次数: "+i);
//------------------------------------------------
String dbname=request.getParameter( "dbname ");
if(dbname==null){
out.println( " <P> 数据库名列表: <P> ");
try{
Class.forName( "com.mysql.jdbc.Driver ");
Connection con=DriverManager.getConnection( "jdbc:mysql://localhost/shujuku?user=root&password=mfditfyu&useUnicode=true&characterEncoding=GB2312 ");
DatabaseMetaData dmd=con.getMetaData();
ResultSet rs=dmd.getCatalogs();
int m=0;
while(rs.next()){
m++;
String dbn=rs.getString( "TABLE_CAT ");
out.println( " <a href=index.jsp?dbname= "+dbn+ "> "+m+ ". "+dbn+ " </a> &nbsp;&nbsp;&nbsp;&nbsp; ");
}
rs.close();
con.close();
}catch(Exception e){
out.println( "数据库服务未启动 "+e);
}
}else{
String tablename=request.getParameter( "tablename ");//获取表名
if(tablename==null){//如果没有表名就把表查出来
out.println( " <P> <b> "+dbname+ "库中表列表: </b> &nbsp;&nbsp; ");
out.println( " <a href=index.jsp> 主页 </a> <P> ");
try{
Class.forName( "com.mysql.jdbc.Driver ");
Connection con=DriverManager.getConnection( "jdbc:mysql://localhost/ "+dbname+ "?user=root&password=mfditfyu&useUnicode=true&characterEncoding=GB2312 ");
DatabaseMetaData dmd=con.getMetaData();
ResultSet rs=dmd.getTables(null,null,null,null);
int x=0;
while(rs.next()){
x++;
String tbname=rs.getString( "TABLE_NAME ");
out.println(x+ ". <a href=index.jsp?dbname= "+dbname+ "&tablename= "+tbname+ "> "+tbname+ " </a> &nbsp;&nbsp;&nbsp;&nbsp; ");
}
rs.close();
con.close();

}catch(Exception e){
out.println(dbname+ "未知错误: "+e);


}
}else{//如果有表名就把表内容显出来
out.println( " <P> <b> <a href=index.jsp?dbname= "+dbname+ "> "+dbname+ " </a> ---> "+tablename+ " </b> &nbsp;&nbsp; ");
out.println( " <a href=index.jsp> 主页 </a> <P> ");
out.println( " <table cellpadding=0 cellspacing=1 border=0 width= '100% ' bgcolor=#334455> ");
try{
Class.forName( "com.mysql.jdbc.Driver ");
Connection con=DriverManager.getConnection( "jdbc:mysql://localhost/ "+dbname+ "?user=root&password=mfditfyu&useUnicode=true&characterEncoding=GB2312 ");
Statement stm=con.createStatement();
ResultSet rs=stm.executeQuery( "select * from "+tablename);
ResultSetMetaData rsmd=rs.getMetaData();
int colCount=rsmd.getColumnCount();//列数
out.println( " <tr> ");
for(int y=1;y <=colCount;y++){
out.println( " <td align=center height=25 bgcolor=#ff9900> <font color=#FFFFFF> <b> &nbsp; "+rsmd.getColumnName(y)+ "&nbsp; </b> </font> </td> ");//输出列标题
}
out.println( " </tr> ");
//下面输出记录
int n=0;
while(rs.next()){
out.println( " <tr> ");
n++;
for(int r=1;r <=colCount;r++){
out.println( " <td bgcolor=#FFFFFF height=22> "+rs.getString(r)+ " </td> ");
}
out.println( " </tr> ");
}
rs.close();
out.println( " <tr> <td bgcolor=#FFFFFF align=center colspan= "+colCount+ "> <font size=2> 共 "+n+ " 条记录 </font> </td> </tr> ");
out.println( " </table> ");

}catch(Exception e){
out.println(dbname+ "未知错误: "+e);
}
}
}
%>


<table cellpadding=0 cellspacing=0> <tr> <td colspan= "1 "> </td> </tr>
</table>
</body>
</html>
[解决办法]

[解决办法]
jsp汇总嘎嘎!!

热点排行