美文网首页
连接数据库,并获取数据库表名、以及表的列名

连接数据库,并获取数据库表名、以及表的列名

作者: 罗宏宇 | 来源:发表于2017-12-27 16:45 被阅读0次

    表名

    try {
            String driver = "oracle.jdbc.driver.OracleDriver"; 
            String strUrl = "jdbc:oracle:thin:@59.203.12.206:1521:orcl"; 
            Connection con = null; 
            Class.forName(driver); 
            con = DriverManager.getConnection(url, "fysjjh", "pass"); 
            String sql="select table_name from USER_ALL_TABLES";
            PreparedStatement stmt = con.prepareStatement(sql);
            Statement stmt1 = con.createStatement(); 
            // 返回结果集 
            ResultSet rs = stmt1.executeQuery(sql); 
            List list = new ArrayList();
            if(rs != null){ 
                while (rs.next()) { 
                String table_name = rs.getString("table_name");
                list.add(table_name);
                } 
            }
            con.close();
           req.setAttribute("tableList", list);
           req.setAttribute("url", url);
      } catch (Exception e) { 
             System.err.println("ERROR! Adding Salgrade: " + e.getMessage());
      }
    

    列名

    try {  
                String driver = "oracle.jdbc.driver.OracleDriver";  
                String strUrl = "jdbc:oracle:thin:@59.203.12.206:1521:orcl";  
                Connection con = null;  
                Class.forName(driver);  
                con = DriverManager.getConnection(url, "fysjjh", "pass");  
                String sql="select column_name from user_tab_columns where table_name='"+ tableName + "'";
                PreparedStatement stmt = con.prepareStatement(sql); 
                Statement stmt1 = con.createStatement();  
                // 返回结果集  
                ResultSet rs = stmt1.executeQuery(sql);  
                List list = new ArrayList();
                if(rs != null){  
                    while (rs.next()) {  
                         String column_name = rs.getString("column_name"); 
                        list.add(column_name);
                    }  
                } 
                con.close();
                returnresult.put("list", list);
                JSONObject resultjson = JSONObject.fromObject(returnresult);
                return resultjson.toString();
            } catch (Exception e) {  
                System.err.println("ERROR! Adding Salgrade: " + e.getMessage());
                returnresult.put("msg", e.getMessage());
                JSONObject resultjson = JSONObject.fromObject(returnresult);
                return resultjson.toString();
            }
    

    相关文章

      网友评论

          本文标题:连接数据库,并获取数据库表名、以及表的列名

          本文链接:https://www.haomeiwen.com/subject/criugxtx.html