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



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


edviges fernandes
database
1 Answer(s)      3 years and 7 months ago
Posted in : Swing AWT


Im doing my project in netbeans..swings I want to display the names of the databases present in mysql database on a Jlist that is already present on the gui and when the user selects a particular database,the tables present under that database should be displayed on another Jlist which is present on the gui.And finally display the column names present in the selected table on another jlist which is present on the gui. Kindly help me.....Thank You....


Advertisement
View Answers

February 10, 2011 at 4:11 PM


Java dependent JList

The given code display the names of the databases present in mysql database on a Jlist and when the user selects a particular database,the tables present under that database should be displayed on another Jlist.And finally display the column names present in the selected table on another jlist

import java.awt.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.event.*;

public class JListExample extends JFrame{

JPanel panel = new JPanel();
public JListExample(){
panel.setLayout(null);
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("show databases");
String data="";
while ( rs.next() ) {
data+= rs.getString("Database")+" "; 
}
String db[]=data.split(" ");
final JList list1=new JList(db);
JScrollPane pane1=new JScrollPane(list1);
final DefaultListModel model = new DefaultListModel();
final JList list2=new JList(model);
JScrollPane pane2=new JScrollPane(list2);
final DefaultListModel model2 = new DefaultListModel();
final JList list3=new JList(model2);
JScrollPane pane3=new JScrollPane(list3);
list1.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
    model.clear();
String st = (String) list1.getSelectedValue();
System.out.println(st);
try{
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+st, "root", "root");
DatabaseMetaData meta = conn.getMetaData();
      ResultSet res = meta.getTables(null, null, "%", null);
      String tableNames = "";
      while (res.next()) {
      tableNames= res.getString(3)+" ";
      model.addElement(tableNames);
    }
}
catch(Exception ex){}
      }
   }
});
list2.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!e.getValueIsAdjusting()) {
    model2.clear();
String str = (String) list2.getSelectedValue();
String s = (String) list1.getSelectedValue();
try{
    Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/"+s, "root", "root");
    Statement stmt=conn.createStatement();
    ResultSet res = stmt.executeQuery("SELECT * FROM "+str+"");
        ResultSetMetaData md = res.getMetaData();
        int col = md.getColumnCount();
        for (int i = 1; i <= col; i++){
        String col_name = md.getColumnName(i);
        model2.addElement(col_name);
        }
}
catch(Exception ex){}
   }
 }
});
pane1.setBounds(10,10,100,100);
pane2.setBounds(120,10,100,100);
pane3.setBounds(230,10,100,100);
panel.add(pane1);
panel.add(pane2);
panel.add(pane3);
add(panel);
setSize(400,200);
setVisible(true);
}
catch(Exception e){}
}
public static void main(String[] args){
new JListExample();
}
}


Related Tutorials/Questions & Answers:
database
database  database
database
database  database application
Advertisements
database
database  the code for initializing the database connection
database
database  i want to let the user select the name of database and then delete that database.......im doing project in swings netbeans
database
database  use of hyperlink to show the data from database
database
database  i need to insert time in my database. i need a code to insert time in my database. its very urgent
database
database  tell me use about database and give me a small program.   It is secure and can easily be accessed, managed, and updated. Moreover... links: Connect JSP with database Mysql Connect Java with database Mysql
database
database  tell me use about database and give me a small program.   It is secure and can easily be accessed, managed, and updated. Moreover... links: Connect JSP with database Mysql Connect Java with database Mysql
database
database  im doing my project in netbeans swings...wn a user wants to create a database from the gui...i want to display an error msg if a database with that name already exists and if it does not exist new database should
database
database  tell me use about database and give me a small program.   It is secure and can easily be accessed, managed, and updated. Moreover... links: Connect JSP with database Mysql Connect Java with database Mysql
Database
Database  from java code i have to retrieve some data from a database, which is present in a different timezone. Scenario :- My database... in database timezone is 9PM 15APR2012. I want to retrieve something from database
Database
Database  I am working in jsf2.Fro a demo i created database in ms access.no wi want to retrive a record from database for a particular data.I need help as I am stuck
Database
Database  How to represent data from database in a tree?   Please visit the following link: Retrieve data from database in tree using Java Swing
DATABASE
DATABASE  How can i get combo box values from database?? or how can i get values in the drop down menu of the html which is similar to dat of combo box in java - from database
database
database  I wanted to know if it is possible to establish database connection on a remote pc with mysql using java swings from netbeans and then create a database on the remote pc.... Kindly help me
database
database  I wanted to know if it is possible to create a database in mysql by letting the user enter the name of the database in swing gui..im doing my project in netbeans...Thank You
database
database  hi i want to develop one patient database can any one provide me the guide
database
database  sir.. how we can uplaod the database in the data file(ms access)please send me a solution
database
database  how we ca upload the data in the database file(ms access
database
database  dateofbirth is not save into the database while i am enter from html browser.it shows as null.whats the reason
Database
Database  Can i get a code for database connectivity Myeclipse 3.3 to MySql manager 2005
DATABASE
DATABASE  I can't send different information from different form into a single table in my database
database
database  Im doing my project in java swings...netbeans I wanted java source code to establish connection to postgresql_8.1 using jdbc and create a user defined database and a table in that database and insert values Plz help me
database
database  In my project,i want the user to specify the name of the database he wants to create in mysql and then also the name of the table along with the column names he wants in that database....Im doing my project in netbeans
database
database  In my project where u helped me to display the databases present im mysql database on the Jlist and then the respective tables in that database on another jlist and then the column names on another jlist I dont want
database
database  i need optimised searching algorithm to retrieve the data from the database and optimised ranking algorithm to display the reults into the user
database
database  Can you please help me to establish database connection to ms sql 2000 from java swing in netbeans
database
the names of the databases present in mysql database on a Jlist that is already present on the gui and when the user selects a particular database,the tables present under that database should be displayed on another Jlist which is present
database
database  I want to let the user select one of the database type... give the name of the database name that he want to create in a textfield... database..im doing my project in java swings.....netbeans Kindly help me ThankYou
Database
Database   select * from student i am not asking this.i am asking about that the command line code like create table student(..........) like this way
Database
Database  In MYSQL database i m given as the email id as the char but i enter the email id it gives manually error.how can i recover that error   For email, use the data type varchar
 

 

 

DMCA.com