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



Home Answers Viewqa Swing-AWT swing with jdbc
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


prashant
swing with jdbc
2 Answer(s)      5 years and 6 months ago
Posted in : Swing AWT



Advertisement
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  How to make swing component auto-resizable when JFrame resize
swing
swing  Write a java swing program to delete a selected record from a table
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
jdbc
jdbc  Hai , Give a steps for jdbc connectivity
jdbc
jdbc  display the records using index in jdbc
JDBC
JDBC  why we use batch in jdbc
JDBC
JDBC  How to add set of queries in a single query in 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
JDBC
JDBC  can u send me the code of jdbc how to join two tables that are in relation
Jdbc
Jdbc  A java program with jdbc connectivity with insert,delete,update options for name,regno,mark1,mark2,total
jdbc
jdbc  is it possible to use doget & dopost method with jdbc to call in a servlet programe
JDBC
JDBC  in class.forname which driver name we are writing for the connection from jdbc to sqlserver 2008
JDBC
JDBC  how to set classpath for eclipse, java, where to copy jdbc connector
jdbc
jdbc  why do we need to load jdbc drivers before connecting to database
jdbc
jdbc   how to write program to save data and retrieve data from the form in Java
jdbc
jdbc  please tell me sir.i dont know JDBC connection and how to create table in database
jdbc
jdbc define batch updates  define batch updates?exp   JDBC... links: http://www.roseindia.net/jdbc/Jdbc-batch-update.shtml http://www.roseindia.net/tutorial/java/jdbc/batchupdateresultsetexample.html
jdbc
jdbc  Write a web based student registration application where the students can register online with their enrollment no. You are required to use JSP, Servelet and JDBC
JDBC
JDBC save a data in the database  I need a code to save a data... between java and mysql using JDBC and saves the data into the database. import... con = null; String url = "jdbc:mysql://localhost:3306/"; String db
JDBC
JDBC code to save a data in the database  I need a code to save... the connection between java and mysql using JDBC and saves the data into the database...!"); Connection con = null; String url = "jdbc:mysql://localhost:3306/"; String db
jdbc
how can we set transaction level through jdbc api  how can we set transaction level through jdbc api   Use Connection.setTransactionIsolation(int) to set the desired tansaction level, which takes one of the 5 arguments
jdbc
how can we call stored procedure using jdbc  how can we call stored procedure using jdbc   Call a Stored Procedure using...(); For more information, visit the following link: JDBC call Stored Procedure
 

 

 

DMCA.com