swing with jdbc

swing with jdbc

View Answers

May 16, 2009 at 4:25 PM

Hi Friend,

To insert the table values, 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"}};
String col[] = {"Name","Address"};
DefaultTableModel model = new DefaultTableModel(data, col);
table = new JTable(model);
JScrollPane pane = new JScrollPane(table);
button=new JButton("Submit");
Object obj1 = GetData(table, 0,0);
Object obj2= GetData(table, 0,1);

final String value1=obj1.toString();
final String value2=obj2.toString();
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
PreparedStatement pstm;
ResultSet rs;
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connect =DriverManager.getConnection("jdbc:odbc:access");
pstm=connect.prepareStatement("insert into data values(?,?)");
pstm.setString(1,value1);
pstm.setString(2,value2);
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 16, 2009 at 4:26 PM

continue.......................

Now to retrieve the database value in JTable, try the following code:

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

public class JTableDatabase extends JFrame{
public JTableDatabase() {
Vector columnNames = new Vector();
Vector data = new Vector();

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connect =DriverManager.getConnection("jdbc:odbc:access");

String sql = "Select * from data";
Statement stmt = connect.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 e) {
System.out.println( e );
}
JTable table = new JTable(data, columnNames);

JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );

JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
}
public static void main(String[] args)
{
JTableDatabase frame = new JTableDatabase();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
}

Thanks









Related Tutorials/Questions & Answers:
swing with jdbc - Swing AWT
have some problems with swing using jdbc. 1. the values entered in table are not stored in DB. 2. is there is any solution that uses jtable with JDBC? kindly...swing with jdbc  Hi, i m developing the desktop application using
jdbc and swing problem in netbeans
jdbc and swing problem in netbeans  i reteived the table from database in a jdbc program. next i want to do is place the table as it is in a jpanel.. i am using netbeans IDE can u tel me how to do that one?? urgent
Advertisements
jdbc and swing problem in netbeans
jdbc and swing problem in netbeans  i reteived the table from database in a jdbc program. next i want to do is place the table as it is in a jpanel.. i am using netbeans IDE can u tel me how to do that one?? urgent
jdbc &swing - Java Beginners
jdbc &swing  I've used a progress bar in a database file upload window using swing.. But I can't set the uploading processing time with the progress bar.. please help me as early as possible.. How can I use progress bar against
java swing jdbc - Java Beginners
java swing jdbc  hello, i want to populate the data(ms access) to the jtable component and want to perform some operations on it like add,edit,delete. Again i want to store the updated data to the database. can you help me out
jdbc,odbc,MySQL,swing - Java Beginners
jdbc,odbc,MySQL,swing  write a code that displays a menu list of all available database drivers in a window,and allows the user to select any driver... to the selected database driver.It is very well known as jdbc front end of a database
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... in a table. Use JDBC
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... in a table. Use JDBC
SWING
SWING  A JAVA CODE OF MOVING TRAIN IN SWING
swing
swing  Write a java swing program to delete a selected record from a table
swing
swing  How to make swing component auto-resizable when JFrame resize
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... will be displayed in a table, and also delete a selected record from a table. Use JDBC
regarding jdbc - JDBC
regarding jdbc  is there any way in jdbc to retieve data from a table and show it by using swing concept i mean to say data from the table can be shown in swing interface with tabular form
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
What is Swing?
What is Swing?  What is Java Swing? Where to get the tutorials of Swing? I am beginner in Java Swing programming and trying to find the tutorials of Swing. Tell me the urls to learn swing. Thanks   Hi, Swing is Java
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
JDBC
JDBC  why we use batch in jdbc
jdbc
jdbc  display the records using index in jdbc
jdbc
jdbc  Hai , Give a steps for jdbc connectivity
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 sms
swing sms  HOW TO SEND SMS MESSAGE FROM SWING USING SQL DATABASE
Swing Program
Swing Program  Write a java swing program to calculate the age from given date of birth
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 to applet
swing to applet  hi how i can isplay a java swing into applet java 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  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
JDBC
JDBC  How to add set of queries in a single query in JDBC
Jva swing
Jva swing   How to create the model form like "Notepad
Java swing
Java swing  what are the root classes of all classes in swing
Java swing
Java swing  Does Swing contains any heavy weight component
AWT & SWING
AWT & SWING  What is diffennce between AWT & SWING
java swing
java swing  view the book details using swing
printing in swing
printing in swing  How can i print payslip 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 - 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
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 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 - 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 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.
jdbc
jdbc  please tell me sir.i dont know JDBC connection and how to create table in database
jdbc
jdbc   how to write program to save data and retrieve data from the form in Java
jdbc
jdbc  why do we need to load jdbc drivers before connecting to database
JDBC
JDBC  how to set classpath for eclipse, java, where to copy jdbc connector
JDBC
JDBC  in class.forname which driver name we are writing for the connection from jdbc to sqlserver 2008
jdbc
jdbc  is it possible to use doget & dopost method with jdbc to call in a servlet programe
Jdbc
Jdbc  A java program with jdbc connectivity with insert,delete,update options for name,regno,mark1,mark2,total
JDBC
JDBC  can u send me the code of jdbc how to join two tables that are in relation
jdbc - JDBC
Why JDBC   JDBC used for what

Ads