GeeCON Prague 2014*
The moose likes JDBC and the fly likes searching database Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "searching database" Watch "searching database" New topic
Author

searching database

Luke Smtih
Greenhorn

Joined: Jun 01, 2004
Posts: 11
good morning.... could someone please help me with some coding for the following program... i need a text box that i can enter a customers name into that then will search the database and display the appropriate record.. i have written the following code for displaying the whole database but am having trouble with the text box and the searching of the database if anyone could point me to some helpfull links or anything else that would be great.. thanks..

CODE SO FAR

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

public class customer extends JFrame {
private Connection connection;
private JTable table;



public customer()
{
String url = "jdbc dbc:Info";

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

connection = DriverManager.getConnection(url);
}
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();
}
getTable();
setSize(450,200);
show();
}
private void getTable()
{
Statement statement;
ResultSet resultSet;

try{
String query = "Select * from custDetails";

statement = connection.createStatement();
resultSet = statement.executeQuery(query);
displayresultSet(resultSet);
statement.close();

}
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 diaplay");
return;
}

setTitle("Customer Details Table from Customer");

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

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

}while (rs.next());

table = new JTable(rows, columnHeads);
JScrollPane scroller = new JScrollPane(table);
getContentPane().add(
scroller, BorderLayout.CENTER);
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:
currentRow.addElement(rs.getString(i));
break;
case Types.INTEGER:
currentRow.addElement(
new Long(rs.getLong(i)));
break;

default:
System.out.println("Type was:"+
rsmd.getColumnTypeName(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 customer app = new customer();

app.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e)
{
app.shutDown();
System.exit(0);
}
}
);
}
}
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30586
    
154

Luke,
I don't see where you do the rs.next() call for each row. Also, what error are you getting when you run the code?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
GeeCON Prague 2014
 
subject: searching database