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

如何样用java语句判断表已经存在

2012-06-11 
怎么样用java语句判断表已经存在最近在写一个程序,需要操作数据库的表之前,先探查是否存在该表否则就建立

怎么样用java语句判断表已经存在
最近在写一个程序,需要操作数据库的表之前,先探查是否存在该表否则就建立该表,请问在程序中如何来判断,是用resultset结果集吗?

为更形象的表述,我贴一下代码:
  Connection con=DriverManager.getConnection(conURL);
Statement s=con.createStatement();
  if(VIP表不存在)
  {
String r1="create table "+r2+"(" +
"name varchar(20) not null," +
"sex char(1) not null," +
"telephone varchar(20)," +
"officephone varchar(20)," +
"Email varchar(20)," +
"suoying integer primary key)";
s.executeUpdate(r1);
  }
求问怎么写试探该VIP表不存在的

[解决办法]

Java code
try { Statement   stmt   =   connect.createStatement(); stmt.executeQuery( "select   count(*)   from   yourTable "); } catch(SQLException   e) {       System.out.print( "not exits "+   e.getMessage()); }
[解决办法]
Java code
//name 表名 public boolean HasTable(String name) {        //判断某一个表是否存在        boolean result = false;        try {            DatabaseMetaData meta = sqlConn.getMetaData();//sqlConn 数据库连接            ResultSet set = meta.getTables (null, null, name, null);            while (set.next()) {                result = true;            }        } catch (Exception e) {            System.err.println(e);            e.printStackTrace ();        }        return result;    } 

热点排行