日期:2014-05-19  浏览次数:20681 次

怎么样用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;
    }