Hi..
I am doing a project on Project Management System for which i created the user interfaces..
I have a user interface in which i have used JTables..
Now my problem is I dont know how to how to store this JTable content in my database table..
This is a very important table of my priject..
Please help me out..
April 2, 2010 at 10:10 AM
Hi Friend,
Try the following code:
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;
ResultSet rs;
int index=1;
int count=table.getRowCount();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connect =DriverManager.getConnection("jdbc:odbc:access");
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=connect.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);
}
}
Thanks
February 10, 2011 at 5:24 PM
import java.awt.print.*;
import java.awt.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.table.*;
import java.io.*;
import javax.swing.JFileChooser;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
class JT implements ActionListener
{
JButton b=new JButton("Print");
Vector JButton= new Vector();
JFileChooser fc;
static private final String newline = "\n";
JButton b1=new JButton("Save");
JTextArea log;
JTable table ;
File file;
public JT()
{
//***********Button*****************
//b.addActionListener(printAction);
b.setBounds(10,100,100,50);
b1.setBounds(10,200,100,50);
b1.addActionListener(this);
/* ActionListener printAction = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
table.print();
}
catch (PrinterException pe)
{
System.err.println("Error printing: " + pe.getMessage());
}
}
};*/
//************
Vector columnNames = new Vector();
Vector data = new Vector();
JPanel p=new JPanel();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con = DriverManager.getConnection("jdbc:odbc:supermarket.mdb");
String sql = "Select * from bill data";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery( sql );
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 );
}
rs.close();
stmt.close();
}
catch(Exception se)
{
//System.out.println(e);
}
table= new JTable(data, columnNames);
table.setVisible(true);
TableColumn col;
for (int i = 0; i < table.getColumnCount(); i++)
{
col = table.getColumnModel().getColumn(i);
col.setMaxWidth(250);
}
JScrollPane scrollPane = new JScrollPane( table );
p.add( scrollPane );
JFrame f=new JFrame();
f.add(p);
p.add(b);
p.setBackground(Color.pink);
p.add(b1);
f.setSize(600,400);
f.setVisible(true);
fc = new JFileChooser();
}//constructor
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == b1)
{
int returnVal;
int flag;
do
{
flag=0;
final JFileChooser fc = new JFileChooser();
file = fc.getSelectedFile();
//here when i put it in do while loop it opens from MY DOCUMENTS which i dont want..it must retain its previous location
returnVal = fc.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
file = fc.getSelectedFile();
String[] files=new File(file.getParent()).list();
for(int i=0;i
if (returnVal == JFileChooser.APPROVE_OPTION)
{
try
{
String fileName = file.getName();
Writer output=null;
output=new BufferedWriter(new FileWriter(file));
//output.write(textArea.getText());
output.close();
}
catch(Exception e2){};
}
String filename =file.getName();
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename));
oos.writeObject(table);
oos.close();
}
catch(IOException fe)
{
System.out.println("Problem creating table file: " + fe);
return;
}
System.out.println("JTable correctly saved to file " + filename);
}
}
public static void main(String[] args)
{
JT hh=new JT();
}
}
//now you can access definately access database just type your own database name and see magic here 'supermarket' is my database name and 'bill' is my table name
Related Tutorials/Questions & Answers:
Connecting JTable to database - JDBCConnecting JTable to database Hi..
I am doing a project on Project... to store this
JTable content in my
database table..
This is a very important... InsertJTableDatabase{
JTable table;
JButton button;
public static void main(String
Connecting to a database through the Proxy.Connecting to a
database through the Proxy.
Connecting to a
database through the Proxy I want to connect to remote
database using a program that is running in the local network behind the proxy. Is that possible
Advertisements
connecting to database - Strutsconnecting to database Hi
I am having problems with connection to MS SQL Server 2005
database.
My first is what do i write in struts... information via the
database in my web page.
Thanks
Tayo Hi friend
Connecting to MYSQL Database in JavaConnecting to MYSQL
Database in Java I've tried executing the code... to the
database");
conn.close();
System.out.println("Disconnected from
database");
} catch (Exception e) {
System.out.println("Error
connecting with database - Strutsconnecting with database I am creating an application where when jsp page is displayed, it contains the combo box where data is populated from the database.it has 3 buttons and the functionality for all buttons is different
connecting to access databaseconnecting to access database print("code sample");Hi I Write java... this there is no error but my data is not going to my Acess
Database.
There is working...
Add a user DSN
Select Microsoft Access Driver(*.mdb)
Select
database name
Problems connecting to a database. Java/SQLiteProblems
connecting to a
database. Java/SQLite `print("try {
con = DriverManager.getConnection("jdbc:sqlite:db/Freepark.sqlite");
} catch... on an SQL
database but i am having problems
connecting to it, I think the problem
jsp -sevlet connecting to database using dropdownjsp -sevlet
connecting to
database using dropdown How can I get my dropdown list from oracle
database and then submit it to another table in JSP. I... to the
database and fetches an array of strings from a
database table and then sends
JTableJTable i want to delete record from
JTable using a MenuItem DELETE. and values of
JTable are fetched from
database....please reply soon
JTableJTable Hello, i cannot display data from my table in the
database to the cells of my
JTable. please help me
jtablejtable how to get the values from
database into
jtable and also add a checkbox into it and then when selected the checkbox it should again insert into
database the selected chewckbox.plzz help
how update JTable after adding a row into database in
JTable, and it's OK, but after adding a row into
database table does't update.
How update
JTable after adding a row into
database?
package djile pak.java... which shows data in
JTable from
database
import java.awt.*;
import
Show multiple identical rows into JTable from databaseShow multiple identical rows into
JTable from
database
In this tutorial, you will learn how to display the multiple rows from
database to
JTable. Here... rows from
database on clicking search button to
jtable. The given code
accepts
jtablejtable hey i have build a form and i m also able to add data from
database to
jtable along with checkbox.the only problem is that if i select multiple checkboxes the data doesnt get inserted into new
database and if only one
JTableJTable Values to be displayed in JTextfield when Clicked on
JTable Cells
JTable JTable how to select a definite cell which containing a similar text containg to the one which the user entering from a
jtable at runtime in java
Connecting to a MySQL Database in Java Connecting to a MySQL
Database in Java
... on
connecting to a MySQL
database, after going through
this program you... classes and
APIs with which we can make use of the
database as we like.
Database JTableJTable need to add values to a
JTable having 4 coloumns ,2 of them are comboboxes
jtablejtable hi
Sir
i am working netbeans IDE,I have a
jtable when i insert values in
jtable
then i am unable to print all inserted values,For eg if i insert 1,2,3,4,5,6,7,8 values
then , i am getting output
JTableJTable Hi
I have problems in setting values to a cell in
Jtable which is in a jFrame which implements TableModelListener which has a abstract method
tableChanged(TableModelEvent e) .
I'll be loading values from data base when
JTable"};
JTable table=new
JTable(data,labels);
JScrollPane pane=new JScrollPane
jtablejtable i have build an application and i retrieve data from the
database and store it in jtable.now i have to make a checkbox column in each row... {
private Vector<Vector<String>> data; //used for data from
database connecting databasesconnecting databases I need to connect mysql on 2 or more remote pc'c.
how can i giv the ip address for 2 or more systems.
is it possible to connect to the required systems by user specifying the
database and table name
my
JTable Display Data From MySQL DatabaseJTable Display Data From MySQL
Database
In this section we will discuss about how to display data of a
database table
into javax.swing.JTable
This section will describe you the displaying of data of a
database table
into a
JTable jtable queryjtable query I need a syntax...where i could fetch the whole data from the
database once i click the cell in
jtable...and that must be displayed in the nearby text field which i have set in the same frame