Thursday, 3 April 2014

program to creating table and inserting records and retriving records dynamically

/*
 * program to create table and insert values dynamically and retrieve all records from table with any   *  number of columns
 */
package jdbcdemos;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 *
 * @author Ramarao
 */
public class JdbcDemo {

    public static void main(String[] args) throws SQLException {
        System.out.println("enter choice");
        Scanner sc = new Scanner(System.in);
        int choice = sc.nextInt();

        switch (choice) {
            case 1:
                System.out.println("creating table");
                System.out.println("enter table name");
                String tablename = sc.next();

                System.out.println("how many cloumns u want");

                int colnum = sc.nextInt();
                StringBuffer str = new StringBuffer("create table " + tablename + "(");
                while (colnum != 0) {
                    System.out.println("enter column name");
                    String colname = sc.next();
                    System.out.println("enter column data type with size");
                    String colsize = sc.next();
                    String column = new String(colname + " " + colsize);
                    if (colnum != 1) {
                        //StringBuffer sb= new StringBuffer(column+",");
                        str.append(column + ",");
                    } else {
                        str.append(column);
                    }
                    colnum--;
                }

                str.append(");");
                String sql = new String(str);
                System.out.println(sql);
                //creating dtabase connection
                try {

                    Connection con = DriverManager.getConnection("jdbc:mysql://db4free.net:3306/sriniamz", "srinidb", "test321");
                    System.out.println(con);
                    System.out.println("connection success....");
                    Statement st = con.createStatement();
                    st.execute(sql);
                    System.out.println("table created successfully....");
                    st.close();
                    con.close();
                } catch (Exception e) {
                    System.out.println(e);
                }

                break;
            case 2:
                System.out.println("inserting data");

                try {
                    Connection con = DriverManager.getConnection("jdbc:mysql://db4free.net:3306/sriniamz", "srinidb", "test321");
                    System.out.println(con);
                    System.out.println("connection success....");
                    Statement st = con.createStatement();
                    System.out.println("enter table name");
                    String tblname = sc.next();
                    String selectsql = "select * from " + tblname + ";";
                    System.out.println(selectsql);
                    ResultSet rs = st.executeQuery(selectsql);
                    ResultSetMetaData rsmd = rs.getMetaData();
                    int totalcolnum = rsmd.getColumnCount();
                    System.out.println(totalcolnum);

                    System.out.println("how many records you want to enter");
                    int record = sc.nextInt();
                    while (record != 0) {

                        StringBuffer insstr = new StringBuffer("insert into " + tblname + " values(");
                        for (int i = 1; i <= totalcolnum; i++) {
                            System.out.println("enter value for " + rsmd.getColumnName(i));
                            String value = sc.next();
                            if (rsmd.getColumnTypeName(i) == "VARCHAR") {
                                value = "'" + value + "'";
                                System.out.println(value);
                            }
                            StringBuffer colval = new StringBuffer(value);
                            System.out.println(rsmd.getColumnTypeName(i));

                            if (i != totalcolnum) {
                                insstr.append(colval + ",");
                            } else {
                                insstr.append(colval);
                            }

                        }

                        insstr.append(");");
                        System.out.println(insstr);
                        String insertsql = new String(insstr);
                        st.executeUpdate(insertsql);
                        record--;

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

                break;

            case 3:
                System.out.println("update values");

                break;
            case 4:
                System.out.println("delete values");
                break;
            case 5:
                System.out.println("display table");

                String url = "jdbc:mysql://db4free.net:3306/sriniamz";
                String usrname = "srinidb";
                String pass = "test321";
                StringBuffer sb = new StringBuffer();
                try {
                    Connection con = DriverManager.getConnection(url, usrname, pass);
                    System.out.println(con);
                    Statement st = con.createStatement();
                    Scanner sc1 = new Scanner(System.in);
                    System.out.println("enter tabel name");
                    String table = sc.next();
                    ResultSet rs = st.executeQuery("Select * from " + table);
                    ResultSetMetaData rsmd = rs.getMetaData();
                    String[] strcol = new String[rsmd.getColumnCount()];
                    System.out.println(rsmd.getColumnCount());
                    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                        strcol[i - 1] = rsmd.getColumnName(i);

                    }
                    for (int i = 0; i < strcol.length; i++) {
                        System.out.println(strcol[i]);
                        if (i == strcol.length - 1) {
                            sb = sb.append(strcol[i]);
                        } else {
                            sb = sb.append(strcol[i] + ",");
                        }

                    }

                    String colnames = new String(sb);
                    String sqlquery = "select " + colnames + " from " + table;
                    System.out.println(sqlquery);
                    Statement st1 = con.createStatement();
                    ResultSet rs1 = st1.executeQuery(sqlquery);
                    StringBuffer sub = new StringBuffer();
                    while (rs1.next()) {
                        String concat = null;
                        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                            String record = rs1.getString(i);
                            if (i == rsmd.getColumnCount()) {
                                System.out.print(record);
                            } else {
                                System.out.print(record + ",");
                            }

                        }
                        System.out.println();
                    }
                    rs.close();
                    st.close();
                    con.close();

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

                break;

            default:
                System.exit(0);
        }

    }

}

No comments:

Post a Comment