import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestDerbyServer 
{
    public static void main(String[] args) 
    {
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver");//加载驱动
            Connection conn = DriverManager.getConnection("jdbc:derby:derbyDB;create=true","user","pwd");
            Statement st = conn.createStatement();
            ResultSet rs=st.executeQuery(" select  count(*)  from SYS.SYSTABLES where tablename='DBINFO' ");
            //注意derby数据库中的表明不存在小写,所以如果此处查询结果为小写那就查不到
            int k=0;
            while(rs.next())
            {
                if("0".equals(rs.getObject(1).toString()))
                    k=-1;
            }
            if(k==-1)
            {
                st.execute("create table DBINFO(xmname varchar(50),dbip varchar(50))");
                System.out.println("创建新表");
                k=0;
            }
          
            rs=st.executeQuery(" select max(xmname)  from DBINFO ");
            while(rs.next())
            {
                if(null!=rs.getObject(1))
                    k=Integer.parseInt(String.valueOf(rs.getObject(1)));
            }   
            st.execute("insert into DBINFO values('"+(k+1)+"','sinboy')");
            st.execute("insert into DBINFO values('"+(k+2)+"','Tom')");
            rs=st.executeQuery(" select * from DBINFO ");
            while(rs.next())
            {
                System.out.println(rs.getObject(1)+"????"+rs.getObject(2));
            }
           // st.execute("drop table DBINFO");
            rs.close();
        } catch (ClassNotFoundException | SQLException ex) {
            Logger.getLogger(TestDerbyServer.class.getName()).log(Level.SEVERE, null, ex);
        }finally{
                try {
                DriverManager.getConnection("jdbc:derby:;shutdown=true");
                } catch (SQLException ex) {
                System.out.println("关闭数据库!");
                //     Logger.getLogger(TestDerbyServer.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
    }


}