techpreparation-homepage

Home  Interview Questions  Aptitude Questions  Tutorials  Placement Papers  Search  Resume Guide  Soft Skills  Video  Forum  Blog


Technical Interview Questions
Javascript Interview Questions
Oracle Interview Questions
J2EE Interview Questions
C++ Interview Questions
XML Interview Questions
EJB Interview Questions
JSP Interview Questions
                              .........More

Programming Source Codes
Java Source Codes
Html Source Codes
CSS Source Codes
C Source Codes
                              .........More

Soft Skills
Communication Skills
Leadership Skills
                              .........More

Subscribe to our Newsletters
Name:
Email:

 

 

  

Java Source Codes

Java Microsoft Access Database Interrogator

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

public class AccessApp extends JFrame implements ActionListener
{
private JPanel panel = new JPanel();
private Container container = this.getContentPane();

private JList jList = new JList();
private JScrollPane jScrollPane = new JScrollPane(jList);

private JFileChooser jfChooser = new JFileChooser();
private File dbFile = null;

private DefaultTableModel model = new CustomerDefaultTableModel();
private JTable jTable = new JTable(model);
private JScrollPane jSjTable = new JScrollPane(jTable);

private JMenuBar menu = new JMenuBar();
private JMenu mnuFile = new JMenu("File");
private JMenuItem miOpen = new JMenuItem("Open");
private JMenuItem mExit = new JMenuItem("Exit");

private Vector tableNames = new Vector();
private String dbFileName = "";
private Connection connection = null;
private boolean firstRun = true;

private chooser mChooser = new chooser();
String dbUrl = "";

public AccessApp()
{
jScrollPane.getViewport().setView(jList);
jScrollPane.setPreferredSize(new Dimension(100,403));
panel.add(jScrollPane);
panel.add(jSjTable);

container.setLayout(new BorderLayout());
this.setContentPane(container);
container.add(panel);

jList.setFixedCellWidth(100);
jTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

this.setJMenuBar(menu);
menu.add(mnuFile);
mnuFile.add(miOpen);
mnuFile.add(mExit);

//Mouse listener for the table jList.
MouseListener mouseListener = new MouseAdapter()
{
//The mouse listener for the click of the jList items.
public void mouseClicked(MouseEvent e)
{
if (e.getClickCount() == 2)
{
if (( jTable.getColumnCount() > 0 ) '' ( firstRun ))
{
while(jTable.getColumnCount() != 0 )
{
removeColumn(jTable, 0);
}

Object index = jList.getSelectedValue();
displayTableData(index.toString());
firstRun = false;
}
}
}
};

//Window listener.
this.addWindowListener(new WindowAdapter()
{
public void windowActivated(WindowEvent e)
{
jList.setListData((tableNames));
jList.setSelectedIndex(tableNames.size()-1);
}
}
);

//listeners.
jList.addMouseListener(mouseListener);
mExit.addActionListener(this);
miOpen.addActionListener(this);

//JFrame specific stuff.
this.setTitle("Access Database Interrogation");
this.setBounds(150, 150, 580, 460);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
this.setResizable(false);
this.setVisible(true);
}

//The button listener actions.
public void actionPerformed(ActionEvent e)
{
if ( e.getSource() == mExit )
{
int opt = JOptionPane.showConfirmDialog(null, "Fo' real?", "Quit", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);

if ( opt == 0 )
{
System.exit(0);
}
}
else if ( e.getSource() == miOpen )
{
jfChooser.setFileFilter(mChooser);
int result = jfChooser.showOpenDialog(this);

if ( result == JFileChooser.APPROVE_OPTION )
{
dbFile = jfChooser.getSelectedFile();

//Just test that its a normal file.
if ( dbFile.isFile() )
{
displayTableNames();
}
}
}
}

//This is executed when the user double clicks a table name in the jList. All the data
//in that table, is printed out in the jTable.
public void displayTableData(String tableName)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(dbUrl);

Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT * from " + tableName);

ResultSetMetaData rsmd = resultSet.getMetaData();
int numberOfColumns = rsmd.getColumnCount();

for ( int i = 1; i < numberOfColumns + 1; i++ )
{
Statement statement1 = connection.createStatement();
ResultSet resultSet1 = statement1.executeQuery("SELECT " +  rsmd.getColumnName(i) + " from " + tableName);

Vector store = new Vector();
while(resultSet1.next())
{
store.addElement(resultSet1.getString(rsmd.getColumnName(i)).toString());
}

model.addColumn(rsmd.getColumnName(i), store);
}

model.fireTableStructureChanged();
}
catch(NullPointerException ex)
{
//This is being thrown occasionally, I'll fix it soon :)
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
}
}

// Loads up all the table names in the database, and prints them into the JList.
public void displayTableNames()
{
dbFileName = dbFile.getAbsolutePath();
String[] types = {"TABLE"};
dbUrl = "jdbc:odbc:Driver=
{Microsoft Access Driver (*.mdb)};DBQ="+dbFileName.trim()+";}";

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
connection = DriverManager.getConnection(dbUrl);
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet resultSet = dbmd.getTables(null, null, "%", types);

while ( resultSet.next() )
{
tableNames.addElement(resultSet.getString(3));
}
}
catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex.getMessage(), "Error", JOptionPane.OK_OPTION);
}
}

public static void main(String args[])
{
AccessApp newInstance = new AccessApp();
}

//Executed when the user clicks a table name, and if there is data in the jTable, this
//removes it.
public void removeColumn(JTable table, int vColIndex)
{
CustomerDefaultTableModel model = (CustomerDefaultTableModel) table.getModel();
TableColumn col = table.getColumnModel().getColumn(vColIndex);
int columnModelIndex = col.getModelIndex();
Vector dataVector = model.getDataVector();
Vector colIds = model.getColumnIdentifiers();

table.removeColumn(col);

colIds.removeElementAt(columnModelIndex);

for (int i = 0; i < dataVector.size(); i++)
{
Vector row = (Vector)dataVector.get(i);
row.removeElementAt(columnModelIndex);
}

model.setDataVector(dataVector, colIds);

Enumeration eNum = table.getColumnModel().getColumns();
for (; eNum.hasMoreElements(); )
{
TableColumn column = (TableColumn) eNum.nextElement();

if (column.getModelIndex() >= columnModelIndex)
{
column.setModelIndex(column.getModelIndex() - 1);
}
}
model.fireTableStructureChanged();
}

class CustomerDefaultTableModel extends DefaultTableModel
{
public Vector getColumnIdentifiers()
{
return columnIdentifiers;
}
}


/*
* This class is the chooser class to make it so that only certiain files
* with certain extensions are seen.
*/
class chooser extends javax.swing.filechooser.FileFilter
{
public boolean accept(File file)
{
String filename = file.getName();

//Enables that only jpegs and gifs are only returned.
return filename.endsWith(".mdb")''file.isDirectory();
}

public String getDescription()
{
return "*.mdb";
}
}
}


<<<----- Return to Java Source Code Questions Page.


 

Have a Question ? post your questions here. It will be answered as soon as possible.

Check Java Interview Questions for more Java Interview Questions with answers

Check Servlet Interview Questions for more Servlet Interview Questions with answers

Check Structs Interview Questions for more Structs Interview Questions with answers