Question: database

Ads

 
 
 

Share on Google+Share on Google+

edviges fernandes
database
2 Answer(s)      6 years and 3 months ago
Posted in : Swing AWT

Im doing project in netbeans swings......you helped me to create a table with user defined column names.....i wanted to know if it is possible to let the user insert his data into that table.....Plz help me if it is possible

Ads
View Answers

February 18, 2011 at 5:01 PM


Java JDBC create table

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class CreateTable
{
    public static void main(String[] args) 
    {
        JFrame f=new JFrame();
        JLabel lab=new JLabel("Enter Table name: ");
        final JTextField text=new JTextField(20);
        JButton b=new JButton("Create");
        f.setLayout(null);
        lab.setBounds(10,10,150,20);
        text.setBounds(180,10,150,20);
        b.setBounds(180,40,100,20);
        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            String st=text.getText();
            String num=JOptionPane.showInputDialog(null, "How many fields do you want to add in table "+st); 
            int no=Integer.parseInt(num);
            String field[]=new String[no];
            String type[]=new String[no];
            for(int i=0;i<no;i++){
                field[i]=JOptionPane.showInputDialog(null,"Enter field "+i);
                type[i]=JOptionPane.showInputDialog(null,"Enter its datatype");
            }
            try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
            Statement stmt=con.createStatement();
            String table = "CREATE TABLE "+st+"(id integer)";
            stmt.executeUpdate(table);
            stmt.close();
            Statement sta=con.createStatement();
            for(int i=0;i<no;i++){
            System.out.println(field[i]+" "+type[i]);
            sta.executeUpdate("ALTER TABLE "+st+" ADD "+field[i]+" "+type[i]);
            }
            int id=Integer.parseInt(JOptionPane.showInputDialog(null, "Enter value for field1"));
            String name=JOptionPane.showInputDialog(null, "Enter value for field2");
            String address=JOptionPane.showInputDialog(null, "Enter value for field3");

            Statement statement=con.createStatement();
            sta.executeUpdate("insert into "+st+" values("+id+",'"+name+"','"+address+"'");
            JOptionPane.showMessageDialog(null, "Table is created successfully");
            }
            catch(Exception ex){
            System.out.println(ex);
            }
            }
        });
        f.add(lab);
        f.add(text);
        f.add(b);
        f.setVisible(true);
        f.setSize(350,100);
    }
}


February 18, 2011 at 5:02 PM


Java JDBC create table

import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
class CreateTable
{
    public static void main(String[] args) 
    {
        JFrame f=new JFrame();
        JLabel lab=new JLabel("Enter Table name: ");
        final JTextField text=new JTextField(20);
        JButton b=new JButton("Create");
        f.setLayout(null);
        lab.setBounds(10,10,150,20);
        text.setBounds(180,10,150,20);
        b.setBounds(180,40,100,20);
        b.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
            String st=text.getText();
            String num=JOptionPane.showInputDialog(null, "How many fields do you want to add in table "+st); 
            int no=Integer.parseInt(num);
            String field[]=new String[no];
            String type[]=new String[no];
            for(int i=0;i<no;i++){
                field[i]=JOptionPane.showInputDialog(null,"Enter field "+i);
                type[i]=JOptionPane.showInputDialog(null,"Enter its datatype");
            }
            try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
            Statement stmt=con.createStatement();
            String table = "CREATE TABLE "+st+"(id integer)";
            stmt.executeUpdate(table);
            stmt.close();
            Statement sta=con.createStatement();
            for(int i=0;i<no;i++){
            System.out.println(field[i]+" "+type[i]);
            sta.executeUpdate("ALTER TABLE "+st+" ADD "+field[i]+" "+type[i]);
            }
            int id=Integer.parseInt(JOptionPane.showInputDialog(null, "Enter value for field1"));
            String name=JOptionPane.showInputDialog(null, "Enter value for field2");
            String address=JOptionPane.showInputDialog(null, "Enter value for field3");

            Statement statement=con.createStatement();
            statement.executeUpdate("insert into "+st+" values("+id+",'"+name+"','"+address+"'");
            JOptionPane.showMessageDialog(null, "Table is created successfully");
            }
            catch(Exception ex){
            System.out.println(ex);
            }
            }
        });
        f.add(lab);
        f.add(text);
        f.add(b);
        f.setVisible(true);
        f.setSize(350,100);
    }
}

Ads









Related Tutorials/Questions & Answers:
database
database  database
database
database  database application
Advertisements
database
database  the code for initializing the database connection
database
database  i want to let the user select the name of database and then delete that database.......im doing project in swings netbeans
database
database  use of hyperlink to show the data from database
database
database  i need to insert time in my database. i need a code to insert time in my database. its very urgent
database
database  tell me use about database and give me a small program.   It is secure and can easily be accessed, managed, and updated. Moreover... links: Connect JSP with database Mysql Connect Java with database Mysql
database
database  tell me use about database and give me a small program.   It is secure and can easily be accessed, managed, and updated. Moreover... links: Connect JSP with database Mysql Connect Java with database Mysql
database
database  im doing my project in netbeans swings...wn a user wants to create a database from the gui...i want to display an error msg if a database with that name already exists and if it does not exist new database should
database
database  tell me use about database and give me a small program.   It is secure and can easily be accessed, managed, and updated. Moreover... links: Connect JSP with database Mysql Connect Java with database Mysql
Database
Database  from java code i have to retrieve some data from a database, which is present in a different timezone. Scenario :- My database... in database timezone is 9PM 15APR2012. I want to retrieve something from database
Database
Database  I am working in jsf2.Fro a demo i created database in ms access.no wi want to retrive a record from database for a particular data.I need help as I am stuck
Database
Database  How to represent data from database in a tree?   Please visit the following link: Retrieve data from database in tree using Java Swing
DATABASE
DATABASE  How can i get combo box values from database?? or how can i get values in the drop down menu of the html which is similar to dat of combo box in java - from database
database
database  I wanted to know if it is possible to establish database connection on a remote pc with mysql using java swings from netbeans and then create a database on the remote pc.... Kindly help me
database
database  I wanted to know if it is possible to create a database in mysql by letting the user enter the name of the database in swing gui..im doing my project in netbeans...Thank You
database
database  hi i want to develop one patient database can any one provide me the guide
database
database  sir.. how we can uplaod the database in the data file(ms access)please send me a solution
database
database  how we ca upload the data in the database file(ms access
database
database  dateofbirth is not save into the database while i am enter from html browser.it shows as null.whats the reason
DATABASE
DATABASE  I can't send different information from different form into a single table in my database
Database
Database  Can i get a code for database connectivity Myeclipse 3.3 to MySql manager 2005
database
database  Im doing my project in java swings...netbeans I wanted java source code to establish connection to postgresql_8.1 using jdbc and create a user defined database and a table in that database and insert values Plz help me
database
database  In my project,i want the user to specify the name of the database he wants to create in mysql and then also the name of the table along with the column names he wants in that database....Im doing my project in netbeans
database
database  In my project where u helped me to display the databases present im mysql database on the Jlist and then the respective tables in that database on another jlist and then the column names on another jlist I dont want
database
database  i need optimised searching algorithm to retrieve the data from the database and optimised ranking algorithm to display the reults into the user
database
database  Can you please help me to establish database connection to ms sql 2000 from java swing in netbeans
database
the names of the databases present in mysql database on a Jlist that is already present on the gui and when the user selects a particular database,the tables present under that database should be displayed on another Jlist which is present
database
database  I want to let the user select one of the database type... give the name of the database name that he want to create in a textfield... database..im doing my project in java swings.....netbeans Kindly help me ThankYou
Database
Database  In MYSQL database i m given as the email id as the char but i enter the email id it gives manually error.how can i recover that error   For email, use the data type varchar
Database
Database  is it possible to view the query code after executing it.if yes tell me the procedure.   Yes, if you want to retrieve the data that is stored in database, then use the given query: select * from student
Tutorials   
Java Spring Hibernate Struts Training Change JPanel background color from mouse entered/exit event JTable Pagination Jcombobox JAVA SWING how can i display a pdf file in a jtextarea Crop Image Code in Applet Crop Image Code in Applet Image Manipulation in Swing AWT Applet Ho to get virtual key code. how to show set of images continously using JScrollpane in java how to show set of images continously using JScrollpane in java How to connect on screen keyboard to system. How to connect on screen keyboard to system. query java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings java client server program for playing video file(stored in folder in the same workspace) using swings Download. how to open one Jframe from main method call Urgent requirement display shapes i am getting an error in adding a menu item to a menu. I am trying to develop a Image Sliding application but unfortunately its not working... Can any one help me in this Sliding window protocol how to update the value of jslider how to update the value of jslider how to update the value of jslider how to update the value of jslider how to update the value of jslider Jbutton in JTable cells program code java textfield problem swing retrieve data from database in java swing form using prev n next buttons retrieve data from database in java swing form using prev n next buttons How to close a frame on action event of a button from a different class ? searching the data swings for webnms Jpanel allignment pass data from java frame to file code for traffic control system simulation Bank Management System how to maKE Jcombox editable after saveing value init. i havei) aJTextField and one button. when place the cursor or enter anything in the test field then only button should be enabled.by default the button should be disabled. i havei) a jtable with few cloumns and few rows andii) one button. when place the cursor in any of the table's column cell. i need to enable the button.by default the button should be disabled. PDF Comparator i have JTextfield and JComboBox. there are several values in combobox.when i select a value from combobox how to make textfiled as a combobox. issue on jcombobox

Ads

 
Advertisement null

Ads