当前位置: 代码迷 >> J2SE >> 获得数据库和表的元数据 求解解决办法
  详细解决方案

获得数据库和表的元数据 求解解决办法

热度:256   发布时间:2016-04-24 01:32:30.0
获得数据库和表的元数据 求解
Java code
package book.database;import java.sql.Connection;import java.sql.DatabaseMetaData;import java.sql.ResultSet;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.sql.Statement;public class GetMetadata {    public static void showDatabaseMetadata(Connection con){        try{            DatabaseMetaData md=con.getMetaData();            System.out.println("数据库"+md.getURL()+"的元数据如下:");                        }catch (SQLException e){                e.printStackTrace();            }    }    public static void showTableMetaData(Connection con,String tableName){        String sql="SELECT * FROM"+tableName;        Statement sm=null;        try{            sm=con.createStatement();            ResultSet rs= sm.executeQuery(sql);                        ResultSetMetaData md = rs.getMetaData();                        System.out.println("数据表"+tableName+"的元数据如下:");            int columnCount = md.getColumnCount();            System.out.println("column count:"+columnCount);            System.out.println();            StringBuffer sb =new StringBuffer("");            sb.append("sn\tname\t\t").append("type\t\t");            sb.append("scale\t").append("isNullable");            System.out.println(sb);            sb.delete(0, sb.length());            for(int i=1;i<=columnCount;i++){                sb.append(i).append("\t");                sb.append(md.getColumnName(i)).append("\t\t");                sb.append(md.getColumnTypeName(i)).append("\t\t");                sb.append(md.getScale(i)).append("\t");                sb.append(md.isNullable(i));                System.out.println(sb);                sb.delete(0,sb.length());            }            rs.close();        }catch(SQLException e){            e.printStackTrace();        }finally{            if (sm!= null){                try{                    sm.close();                }catch(SQLException e1){                    e1.printStackTrace();                }            }        }    }    public static void main(String[] args) throws ClassNotFoundException,SQLException{        String dbName = "books";        String tableName = "book";        String userName = "root";        String password = "123456";        Connection con = null;        try{            con = DBConnector.getMySQLConnection(null,null,null,dbName,userName,password);            GetMetadata.showDatabaseMetadata(con);            System.out.println();            GetMetadata.showTableMetaData(con,tableName);        }catch (ClassNotFoundException e1){            throw e1;        }catch (SQLException e2){            throw e2;        }finally {            if (con != null){                try{                    con.close();                }catch(SQLException e1){                    e1.printStackTrace();                }            }        }    }}


获得数据库和表的元数据
在eclipse上运行这样子
求解
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROMbook' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1631)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3277)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3206)
at com.mysql.jdbc.Statement.executeQuery(Statement.java:1232)
at book.database.GetMetadata.showTableMetaData(GetMetadata.java:23)
at book.database.GetMetadata.main(GetMetadata.java:68)
  相关解决方案