0

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

public class DisplayQueryResults extends JFrame {
private Connection connection;
private Statement statement;
private ResultSet resultSet;
private ResultSetMetaData rsMetaData;

private JTable table;
private JTextArea inputQuery;
private JButton submitQuery;

public DisplayQueryResults()
{
super( "Enter Query.Click Submit to See Results." );

String url = "jdbc:odbc:books";
String username = "anonymous";
String password = "guest";

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

connection = DriverManager.getConnection(
url, username, password);
}
catch ( ClassNotFoundException cnfex) {
System.err.println(
"Failed to load JDBC/ODBC driver.");
cnfex.printStackTrace();
System.exit(1);
}
catch (SQLException sqlex) {
System.err.println("Unable To Connect");
sqlex.printStackTrace();
System.exit(1);
}
inputQuery = new JTextArea("SELECT * FROM Authors", 4, 30);
submitQuery = new JButton("Submit Query");
submitQuery.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == submitQuery)
getTable();
}
}
);
JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
topPanel.add( new JScrollPane(inputQuery),
BorderLayout.CENTER);
topPanel.add(submitQuery, BorderLayout.SOUTH);
table = new JTable( 4, 4 );
Container c = getContentPane();
c.setLayout(new BorderLayout() );
c.add(topPanel, BorderLayout.NORTH);
c.add(table, BorderLayout.CENTER);
getTable();
setSize( 500, 500);
show();
}
private void getTable()
{
try {
String query = inputQuery.getText();

statement = connection.createStatement();
resultSet = statement.executeQuery( query );
displayResultSet( resultSet );
}
catch( SQLException sqlex) {
sqlex.printStackTrace();
}
}

private void displayResultSet( ResultSet rs )
throws SQLException
{
boolean moreRecords = rs.next();

if(!moreRecords) {
JOptionPane.showMessageDialog(this, "ResultSet contained no records");
setTitle("No Records To Display");
return;
}

Vector columnHeads = new Vector();
Vector rows = new Vector();

try {

ResultSetMetaData rsmd = rs.getMetaData();

for(int i=1; i<=rsmd.getColumnCount(); ++i)
columnHeads.addElement(rsmd.getColumnName(i));

do {
rows.addElement(getNextRow(rs, rsmd));
}while(rs.next());

table = new JTable(rows,columnHeads);
JScrollPane scroller = new JScrollPane(table);
Container c = getContentPane();
c.remove(1);
c.add(scroller, BorderLayout.CENTER);
c.validate();
}
catch(SQLException sqlex) {
sqlex.printStackTrace();
}
}
private Vector getNextRow(ResultSet rs, ResultSetMetaData rsmd) throws SQLException
{
Vector currentRow = new Vector();

for(int i = 1; i <= rsmd.getColumnCount(); ++i)
switch(rsmd.getColumnType(i)) {
case Types.VARCHAR:
case Types.LONGVARCHAR:
currentRow.addElement(rs.getString( i ));
break;
case Types.INTEGER:
currentRow.addElement(
new Long( rs.getLong(i)));
break;
default:
System.out.println("Type was:" + rsmd.getColumnType(i));

}
return currentRow;
}
public void shutDown()
{
try{
connection.close();
}
catch(SQLException sqlex) {
System.err.println("Unable to disconnect");
sqlex.printStackTrace();
}
}
public static void main(String args)
{
final DisplayQueryResults app = new DisplayQueryResults();

app.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
app.shutDown();
System.exit(0);
}

}

);

}
}


I Have a problem in this program... When I'm Starting To Run This GUI Application... There's Alaways Flashing On The Screen That There Is No Any Main Mathod In The Class... Guys What Does It Mean... Help Me Please...


And Another Thing Is What Is The Best Database Software... That I Can Use In My Program...

Teinx A Lot...

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by iamthwee
0

Well do you have a main method? If you are asking this question you need to step back to the newbie tutorials.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.