• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JDBC

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to connect swing frame to MySQL database?
 
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am sending some sample code to connect with mysql

U need a jdbc driver jar to connect to mysql

u can get that by searchin internet


Before seeing this code ........

better to go

http://java.sun.com/docs/books/tutorial/jdbc/

and then check this code


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

public class DatabaseInformation extends JFrame implements ActionListener
{
DatabaseMetaData dmd;
ResultSet rs;
ResultSetMetaData md;
int columns;
JComboBox comboBox;
JTable table;
String catalog;

public DatabaseInformation()
{
comboBox = new JComboBox();
Vector columnNames = new Vector();
Vector data = new Vector();

try
{
// Connect to the Database

String driver = "org.gjt.mm.mysql.Driver";
String url = "jdbc:mysql ataBaseNname";
String userid = "";
String password = "";

Class.forName( driver );
Connection connection = DriverManager.getConnection( url, userid, password );
dmd = connection.getMetaData();

// Get the first Catalog
// Note: the result set can contain multiple catalogs, we are just looking at the first one

rs = dmd.getCatalogs();

if (rs.next())
{
catalog = rs.getObject(1).toString();
System.out.println( catalog );
}

rs.close();

// Get Tables

rs = dmd.getTables(catalog, null, null, new String[] { "TABLE" });
md = rs.getMetaData();
columns = md.getColumnCount();

while (rs.next())
{
comboBox.addItem( rs.getObject(3) );
}

rs.close();

}
catch(Exception e)
{
System.out.println( e );
}

comboBox.addActionListener( this );
getContentPane().add(comboBox, BorderLayout.NORTH);

// Create table with database data

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

public void actionPerformed(ActionEvent e)
{
String table = (String)comboBox.getSelectedItem();
displayTableColumns( table );
}

private void displayTableColumns(String tableName)
{
try
{
// Get Columns

rs = dmd.getColumns(catalog, null, tableName, null);
md = rs.getMetaData();
int columns = md.getColumnCount();
Vector columnNames = new Vector(columns);
Vector data = new Vector();

// 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();

// Reset Table

DefaultTableModel model = new DefaultTableModel(data, columnNames);
table.setModel( model );
}
catch(Exception e)
{
System.out.println( e );
}

}

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


enjoy ................................
 
Sunil Kumar Gupta
Ranch Hand
Posts: 824
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct this line

String url = "jdbc:mysql ataBaseNname";

with


String url = "jdbc:mysql:\\localhost:3306/dataBaseName";

enjoy
 
We don't have time for this. We've gotta save the moon! Or check this out:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic