Thursday, 8 September 2011

Database Program

 import java.sql.*;
 import java.io.*;
 class database
 {
        public static void main(String args[])throws IOException
        {
        String str;
        int ch;
                BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                try
                {
                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                Connection c=DriverManager.getConnection("jdbc:odbc:master");
                Statement s=c.createStatement();
               
do
{
System.out.println("\n1->create\n2->insert or update or modify or alter\n3->view\n4->exit\n");
ch=Integer.parseInt(br.readLine());

                switch(ch)
                {
                case 1:
                        {
                        str=br.readLine();
                        boolean a=s.execute(str);
                        if(!a)
                        System.out.println("Table created");
                        }
                        break;
                case 2:
                        {
                        str=br.readLine();
                        int r=s.executeUpdate(str);
                        }
                        break;
                case 3:
                        {
                        str=br.readLine();
                        ResultSet rs=s.executeQuery(str);
                        ResultSetMetaData rsmd = rs.getMetaData();
                        int x=rsmd.getColumnCount();
                        for(int j=1;j<=x;j++)
                        {
                        System.out.print(rsmd.getColumnName(j)+"\t\t");
                        }
                        while(rs.next())
                        {
                        System.out.println("");

                                for(int i=1;i<=x;i++)
                                {
                                System.out.print(rs.getObject(i)+"\t");
                                }

                        }
                        }
                        break;
                }
}while(ch!=4);


                }catch(Exception e)
                {
                System.out.println(e);
                }
          }
}

No comments:

Post a Comment