2013-06-04 81 views
0

這就是我需要創建的。 我有一個網站,用戶可以根據自己的需要/要求來設計申請表。基本上,網站上有一個HTML設計者可以執行此操作。用戶可以拖放HTML組件,然後用純HTML創建應用程序表單。例如,要創建「個人部分」,用戶可以拖放標籤組件和TextBox組件,並將標籤文本設置爲「FirstName」等。我需要根據一個用戶創建的表單創建一個數據庫。如果他創建的只有FirstName和LastName的個人部分,創建的表應該只有這兩個列。 (也就是說,在創建表單時由用戶決定col名稱,插入和更新數據庫字段的SQL查詢都應該動態執行)。請幫我解決這個問題。任何建議的模式適用? (Web應用程序是使用Java創建)動態數據庫和SQL查詢

任何幫助,將不勝感激

感謝

回答

0

您可能需要爲某種形式的建設者,其建立在飛的形式,可以採取幫助代碼從下面的代碼創建動態SQL表。這僅僅是一個暗示不是你的問題的完美解決方案

嘗試{

Class.forName("com.mysql.jdbc.Driver"); 

    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbctest", "root", "123"); 
    Statement st = con.createStatement(); 

    System.out.println("connection has been made "); 

    Scanner scanner = new Scanner(System.in); 
    System.out.println("please enter table name "); 
    String tableName = scanner.next(); 

    String strr = null; 

    System.out.println("Enter no of columns you like to use: "); 
    int noc = scanner.nextInt(); 

    for (int i = 1; i <= noc; i++) { 
    BufferedReader bf = new BufferedReader(new InputStreamReader( 
     System.in)); 
    System.out.println("Enter column name with type "); 
    String s1 = bf.readLine(); 
    if (strr == null) { 
    strr = s1; 

    } else { 
    strr = strr + s1; 

    } 

    } 

    st.executeUpdate("create table " + tableName + "(" + strr + ")"); 
    System.out.println("your table " + tableName 
    + " has been created!!!"); 

    ResultSet rs = st.executeQuery("SELECT * FROM " + tableName); 
    ResultSetMetaData rsmd = rs.getMetaData(); 
    int NumOfCol = rsmd.getColumnCount(); 
    System.out.println("Number of Columns of your table =" + tableName 
    + " " + NumOfCol); 
    System.out.println("Column names are "); 
    for (int i = 1; i <= NumOfCol; i++) { 
    System.out.println(rsmd.getColumnName(i)); 
    } 
    String strn = null; 
    System.out.println("please enter values you need to insert"); 

    for (int i = 1; i <= NumOfCol; i++) { 
    String s5 = scanner.next(); 
    if (strn == null) { 
    strn = s5; 

    } else 
    strn = strn + s5; 

    } 
    System.out.println(strn); 

    st.executeUpdate("insert into " + tableName + " values" + "(" 
    + strn + ")"); 
    System.out.println("your table " + tableName 
    + " has been filled!!!"); 

    con.close(); 
    System.out.println("connection has been colsed "); 

    } 

    catch (Exception e) { 

    System.out.println(e.getMessage()); 
    } 
+0

在這裏,在我的問題,db表山坳的是根據正在由用戶設計的表格類型生成。他只是拖放HTML組件來創建設計。 – Anish

+0

我得到了你的觀點,這就是爲什麼我建議你尋找某種表單生成器工具,它可以動態創建HTML Web表單並將從這些動態Web表單發佈的值存儲到數據庫中。您可能會發現以下鏈接有用https://code.google.com/p/jquery-form-b​​uilder-plugin/ – Pawan