java实现输出数据表的所有字段
近期在做sql语句优化,需要获取表的所有字段,遇到表字段多的时候,一个个字段拷贝太麻烦,于是做了简单的实现,这样搞着好多了。代码如下:
package net.test; import java.sql.*; public class TestSQL { static String url = "jdbc:postgresql://192.168.1.20:5432/db"; static String usr = "postgres"; static String psd = "123456"; public static void main(String args[]) { Connection conn = null; try { Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection(url, usr, psd); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT attname FROM pg_class c,pg_attribute a " + "WHERE c.relname = 'tb_Student' " + "AND c.oid = a.attrelid AND a.attnum>0;"); while (rs.next()) { System.out.println(rs.getString(1)); } rs.close(); st.close(); conn.close(); } catch (Exception e) { e.printStackTrace(); } } }