最近在写一个程序,需要操作数据库的表之前,先探查是否存在该表否则就建立该表,请问在程序中如何来判断,是用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表不存在的

解决方案 »

  1.   

    try 

    Statement   stmt   =   connect.createStatement(); 
    stmt.executeQuery( "select   count(*)   from   yourTable "); } 
    catch(SQLException   e) 

          System.out.print( "not exits "+   e.getMessage()); 
    }
      

  2.   

    //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;
        } 
      

  3.   

    java.sql.Connection con = getYourConnection(); 
       
    ResultSet rs  = con.getMetaData().getTables(null, null,  "yourTable", null );
      if (rs.next())  {
     //yourTable exist
      }else  {
     //yourTable not exist
     }