Add, deleting, updating records from jtable to mysql
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   jas_singh
Posted On:   Saturday, November 25, 2006 07:04 PM

Hi, im looking to Add, delete, update records from jtable to mysql by clicking on buttons but im having trouble doing this,Ive managed to get my data to appear from mysql to the jtable can anyone plz help? thanks in advance here's my code so far. import java.awt.*; import java.awt.event.*; import java.io.*; import java.sql.*; import java.util.*; import javax.swing.*; import javax.swing.table.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; public class UserDatabase extends JFrame implements ActionListener { public UserDatabase() { Vector columnNames = new   More>>

Hi, im looking to Add, delete, update records from jtable to mysql by clicking on buttons but im having trouble doing this,Ive managed to get my data to appear from mysql to the jtable can anyone plz help? thanks in advance here's my code so far.

			

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


public class UserDatabase extends JFrame implements ActionListener

{
public UserDatabase()
{
Vector columnNames = new Vector();
Vector data = new Vector();

try
{
// Connect to the Database

// String driver = "com.mysql.jdbc.Driver";

// String url = "jdbc:mysql:///users";
// String Username = "";
// String Password = "";

// Class.forName( driver );
// Connection connection = DriverManager.getConnection( url, Username, Password );

Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql:///users", "Username", "Password");



// Read data from a table

String sql = "Select * from users";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery( sql );
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();

// Get column names

for (int i = 1; i <= columns; i++)
{
columnNames.addElement( md.getColumnName(i) );
}

// Get row data

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 );
}

// Create table with database data

JTable table = new JTable(data, columnNames);

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

JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );




JButton jb = new JButton ("Add User");
jb.addActionListener (this);
buttonPanel.add (jb);

jb = new JButton ("Delete User");
jb.addActionListener (this);
buttonPanel.add (jb);

jb = new JButton ("Edit User");
jb.addActionListener (this);
buttonPanel.add (jb);

// sets size of chat window
this.setSize(1000,500);
this.setTitle("User Database");
}
public void actionPerformed (ActionEvent e)
{

//code for jbuttons go here

}


public static void main(String[] args)
{
TableFromDatabase frame = new TableFromDatabase();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);

}
}

   <<Less
About | Sitemap | Contact