Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


dipanjan goswami
swing
3 Answer(s)      3 years and 2 months ago
Posted in : Java Beginners


How to fetch data from a row of a JTable and save the data in a database table dynamically? How to fetch data from database table and set the values in a Jtbale rows dynamically? How to set the no. of rows of a JTable dynamically? Please give me solutions? Thanks in advance...


Advertisement
View Answers

May 5, 2011 at 1:22 PM


1)Fetch data from the row of jtable and save it into database:

import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;
import java.awt.*;
import java.awt.event.*;

public class InsertJTableDatabase{
JTable table;
JButton button;
public static void main(String[] args) {
new InsertJTableDatabase();
}

public InsertJTableDatabase(){
JFrame frame = new JFrame("Getting Cell Values in JTable");
JPanel panel = new JPanel();
String data[][] = {{"Angelina","Mumbai"},{"Martina","Delhi"}};

String col[] = {"Name","Address"};
DefaultTableModel model = new DefaultTableModel(data, col);
table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
button=new JButton("Submit");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
PreparedStatement pstm=null;
ResultSet rs;
int index=1;
int count=table.getRowCount();
try{
 Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
for(int i=0;i<count;i++){
Object obj1 = GetData(table, i, 0);
Object obj2 = GetData(table, i, 1);
String value1=obj1.toString();
String value2=obj2.toString();
System.out.println(value1);
System.out.println(value2);

pstm=con.prepareStatement("insert into data values(?,?)");
pstm.setString(1,value1);
pstm.setString(2,value2);

index++;
}
pstm.executeUpdate();
}
catch(Exception e){}
}
});
panel.add(pane);
panel.add(button);
frame.add(panel);
frame.setSize(500,250);
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public Object GetData(JTable table, int row_index, int col_index){
return table.getModel().getValueAt(row_index, col_index);
}
}


May 5, 2011 at 1:22 PM


2)Fetch data from database table and set the values in a JTable rows:

import java.io.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;

class JTableDatabase extends JFrame{
    ResultSet rs;
    JTableDatabase(){
    final Vector columnNames = new Vector();
    final Vector data = new Vector();
    JPanel panel=new JPanel();
    try{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
    Statement st = con.createStatement();
    ResultSet rs= st.executeQuery("Select * from employee");
    ResultSetMetaData md = rs.getMetaData();
    int columns = md.getColumnCount();
    for(int i = 1; i <= columns; i++){
    columnNames.addElement(md.getColumnName(i));
    }
    while(rs.next()){
    Vector row = new Vector(columns);
    for (int i = 1; i <= columns; i++){
    row.addElement(rs.getObject(i));
    }
    data.addElement(row);
    }
   }
catch(Exception e){}
JTable table = new JTable(data, columnNames);
JScrollPane scrollPane = new JScrollPane(table);
panel.add(scrollPane);
add(panel);
}
 public static void main(String arg[])
  {
    try
    {
    JTableDatabase frame=new JTableDatabase();
    frame.setSize(550,200);
    frame.setVisible(true);
    }
  catch(Exception e)
    {}
  }
}


May 5, 2011 at 1:24 PM


For more information, visit the following link:

Swing Tutorials



Related Tutorials/Questions & Answers:
SWING
SWING  A JAVA CODE OF MOVING TRAIN IN SWING
swing
swing  How to make swing component auto-resizable when JFrame resize
Advertisements
swing
swing  Write a java swing program to delete a selected record from a table
Swing
Swing  Write a java swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
Swing
Swing  Write a java swing program to search the details of the students. Searching is performed on studentā??s first name. The details of all those students having same name as in given in searching criterion will be displayed
swing-awt - Swing AWT
swing-awt  Hi, Thanks for replying to my question...I'm getting some confusion to add action events in my application(Rich Text Editor).How to add action events? Thank U
java swing - Swing AWT
java swing  how i can insert multiple cive me exampleolumn and row in one JList in swing?plz g  Hi Friend, Please clarify your question. Thanks
swing/awt - Swing AWT
swing/awt  How to create richtexteditor using swings...?I'm very much new to swings....It's urgent.....Thank u...   hello read this book you get idea; JFC Swing Tutorial, The: A Guide to Constructing GUIs, Second
Swing paint - Swing AWT
Swing paint  hi, i want to print something on window using swing applet.. m doing dis.. protected void paintComponent(Graphics g... the Swing Applet, use html file with the following code: Java Applet Demo
SWING FRMES
SWING FRMES  hai SIR? HOW TO DESIGN swing Frames send source code
SWING FRMES
SWING FRMES  hai SIR? HOW TO DESIGN swing Frames send source code
Swing Program
Swing Program  Write a java swing program to calculate the age from given date of birth
swing sms
swing sms  HOW TO SEND SMS MESSAGE FROM SWING USING SQL DATABASE
java swing - Swing AWT
java swing  how to add image in JPanel in Swing?  Hi Friend, Try the following code: import java.awt.*; import java.awt.image....: http://www.roseindia.net/java/example/java/swing/ Thanks
java swing - Swing AWT
java swing   how i can insert in JFrame in swing?  Hi Friend, Try the following code: import java.awt.*; import javax.swing.*; import java.awt.event.*; class FormDemo extends JFrame { JButton ADD; JPanel
java swing - Swing AWT
java swing   Iam developing a java web browser.Actually my code works fine ie. i can load a web page without proxy.But in my place i have only proxy servers through which i should visit any web site.So, i want my browser also
SWING - Swing AWT
SWING  how can i insert image in Jpanel by extending class with JFrame in swing?  Hi Friend, Try the following code: import java.awt.*; import java.awt.image.*; import java.io.*; import javax.imageio.*; import
Jva swing
Jva swing   How to create the model form like "Notepad
printing in swing
printing in swing  How can i print payslip in swing
java swing
java swing  view the book details using swing
AWT & SWING
AWT & SWING  What is diffennce between AWT & SWING
Java swing
Java swing  Does Swing contains any heavy weight component
Java swing
Java swing  what are the root classes of all classes in swing
swing program
swing program  Write a java swing program to getname and email id. Display the message if email id contains the name of theuser
Swing question - Swing AWT
Swing question  I want to move a string in JFrame similarily as done by marquee tag in HTML.The string should move from right to left. How can I do this?  Hi Friend, Try the following code: import java.awt.
SWING - Swing AWT
SWING  how to insert image and components such as JLabel,JButton,JTextfield in JFrame in swing?  Hi Friend, Try the following code: import java.io.*; import java.awt.*; import javax.swing.*; import
swing - Swing AWT
swing   how to create JTabbedPane.when i choose one jtabbedpane it will appear JTextField,resultMessages.pls give me any examples  Hi Friend, Try the following code: import javax.swing.*; import java.awt.
swing with jdbc - Swing AWT
swing with jdbc  Hi, i m developing the desktop application using swing.i want to use table structure for that.also its DB specific application. i have some problems with swing using jdbc. 1. the values entered in table
java swing
java swing  what is java swing   Swing is a principal GUI toolkit for the Java programming language. It is a part of the JFC (Java... the following link: Swing Tutorials
Swing application
Swing application  Hello, I want to develop a Swing application similar to J2EE application.The only difference is that, the users will access the application not through web browsers, instead through a Swing desktop application
Swing Program
Swing Program  write a java swing program to get user name and password. The name contains only characters while password is masked with the character "*". Apply checks for all validation
Swing Program
Swing Program  Write a java swing program that takes name and marks as input with all validation. The name and marks will be displayed in message box when user clicks on a button
java swing
java swing  how to connect database with in grid view in java swing   Hi Friend, Please visit the following link: Grid view in java swing Thanks
Swing - Swing AWT
/java/swing/get_jtextfield_value_from_anothe.shtml Thanks
Java swing
Java swing  Write a java swing program to calculate the age from given date of birth
Swing EJB
Swing EJB  Hi everyone !!! I tried to find wether EJB architecture can operate with swing, because I am trying to make application which would work as with swing client side, instead jsp or other web page page(s). I wondered
java swing
java swing  add two integer variables and display the sum of them using java swing
swing question
swing question  how to write multiple line text area in a single frame
Swing Program
Swing Program  Write a java swing program to search the details of the students. Searching is performed on student's first name. The details of all those students having same name as in given in searching criterion
java swing.
java swing.  Hi How SetBounds is used in java programs.The values in the setBounds refer to what? ie for example setBounds(30,30,30,30) and in that the four 30's refer to what
Swing Application
Swing Application  hello, i am making swing application in java i am unable to run prog......when my rite clikcing on my project name and clicking thn run it is not showing output .....and when i am cliking on individual java
Java swing
to the database using java swing
Java swing
are displayed in the table..I need the source code in java swing
Use of Hyperlink in Swing - Swing AWT
Use of Hyperlink in Swing  Hi, We are developing the UI using the Java Swing and I have to create Hyperlink in the JTable cell so that if the user... is: Is it possible to use Hypelink in java Swing. Please Help. Regards
swing program
swing program  Iam using one JFrame and 4 JPanels one JTabbedPane. my problem is next, previous buttons. when click next button visible some JPanels,again press visble another JPanels. also previous button. how to write the logic
swing program for decimal to binary
swing program for decimal to binary  swing program for decimal to binary
java - Swing AWT
What is Java Swing AWT  What is Java Swing AWT
swing - Java Beginners
Swing application testing  What are the conventions for testing a Java swing application
Swing program
Swing program  Write a java swing program to get name and email id. Display the error message if email id is not in xxxxx@xxxx.xxx format.   import java.awt.*; import java.sql.*; import javax.swing.*; import
Basic Diffence between Swing-AWT - Swing AWT
Basic Diffence between Swing-AWT  What is Basic Diffence between Swing-AWT
 

 

 

DMCA.com