Need to write ResultSet string output of one class to the JTextArea of a GUI class
Here is my code: Any help would be appreciated. I do not want to add another class if it is possible..
//****** I NEED TO SOMEHOW HAVE THE STRING OUTPUTS OF THE METHODS IN THE CDDATABASE CLASS DISPLAY IN THE JTEXTAREA JTA
//************OF THE CDFRAME CLASS, BUT THE CATCH IS, THIS NEEDS TO BE DONE WHEN THE BUTTONS ARE PRESSED (SEE //**********CDLISTENER ACTIONEVENT)
//*****************************This is the CDFrame class with Main()***********************//
Code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class CDFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private JTextArea jta;
private JTextField jtf1, jtf2;
private JButton first, prev, next, last;
private JLabel jl1, jl2;
public CDFrame(String title) //*** CDFrame constructor that adds the JPanel ***//
{
super(title);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
first=new JButton("First"); //***Assigns each button to a variable with a String identifier***//
prev=new JButton("Previous");
next=new JButton("Next");
last=new JButton("Last");
JPanel south = new JPanel();
south.add(first); //***Adds each button to the south panel of the frame***//
south.add(prev);
south.add(next);
south.add(last);
this.add(south, BorderLayout.SOUTH);
jtf1 = new JTextField();
jtf2 = new JTextField();
jtf1.setPreferredSize(new Dimension(200, 35));
jtf2.setPreferredSize(new Dimension(200, 35));
jl1 = new JLabel("Search Artist Name:");
jl2 = new JLabel("Search Album Title:");
JPanel north = new JPanel();
north.add(jl1);
north.add(jtf1);
north.add(jl2);
north.add(jtf2);
this.add(north, BorderLayout.NORTH);
jta = new JTextArea();
this.add(jta, BorderLayout.CENTER);
CDListener cd= new CDListener(jtf1, jtf2, jta); //***Instantiates CDListener and registers with each button***//
first.addActionListener(cd);
prev.addActionListener(cd);
next.addActionListener(cd);
last.addActionListener(cd);
jtf1.addActionListener(cd);
jtf2.addActionListener(cd);
}
public static void main(String [] args) throws ClassNotFoundException
{
JFrame frame=new CDFrame("My Database GUI");
frame.setSize(800, 550);
frame.setVisible(true);
String url = "jdbc:odbc:" +args[0];
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection connection = DriverManager.getConnection(url);
CDDatabase db = new CDDatabase(connection);
}catch(SQLException s)
{
s.printStackTrace();
}
//************************End of CDFrame class*********************//
}
}
//*******************END of CDFrame class******************************//
//****This is my CDDatabase class with the SQL query statements and ResultSet methods******//
import java.io.PrintStream;
import java.sql.*;/**
*
*/
/**
*
*
*/
public class CDDatabase {
private static Connection connection;
public CDDatabase(Connection connection) throws SQLException
{
this.connection=connection;
}
public void insertCD(CD cd) //***Method for inserting CDs by artist name and album title into the database***//
{
System.out.println("Inserting CD.");
try
{
Statement insertCD=connection.createStatement();
String sql = "INSERT INTO CDs VALUES (" +cd.getArtistName()+ ", " +cd.getAlbumName()+")";
System.out.println("Executing statement: " +sql);
insertCD.executeUpdate(sql);
insertCD.close(); //***Closes the insertCD connection***//
System.out.println("CD inserted successfully!");
}catch(SQLException e)
{
e.printStackTrace();
}
}
public void removeCD(CD cd) //***Method for removing CDs by artist name and album title from the database***//
{
System.out.println("Removing CD.");
try
{
Statement removeCD=connection.createStatement();
String sql = "DELETE FROM CDs WHERE artist =" +cd.getArtistName()+ " AND title=" +cd.getAlbumName()+" ";
System.out.println("Executing statement: " +sql);
removeCD.executeUpdate(sql);
removeCD.close(); //***Closes the removeCD connection***//
System.out.println("CD removed successfully!");
}catch(SQLException e)
{
e.printStackTrace();
}
}
public static void findByTitle(String s) //***Method for finding CDs by album title and displaying the query results at the command line***//
{
System.out.println("Searching CD's by title.");
try
{
Statement findByTitle=connection.createStatement();
String sql = "SELECT * FROM CDs WHERE title = '" +s+ "' ";
System.out.println("Executing statement: " +sql);
ResultSet results = findByTitle.executeQuery(sql);
while(results.next())
{
String query=(results.getString("artist")+","+results.getString("title"));
}
results.close(); //***Closes the results set***//
findByTitle.close(); //***Closes the findByTitle connection***//
}catch(SQLException e1)
{
e1.printStackTrace();
}
}
public static void findByArtist(String a) //***Method for finding CDs by artist name and displaying the query results at the command line***//
{
System.out.println("Searching CD's by artist.");
try
{
Statement findByArtist=connection.createStatement();
String sql = "SELECT * FROM CDs WHERE artist ='" +a+ "' ";
System.out.println("Executing statement: " +sql);
ResultSet results =findByArtist.executeQuery(sql);
while(results.next())
{
String artist=results.getString("artist");
String title=results.getString("title");
System.out.println("QUERY: "+artist+", " +title);
}
results.close(); //***Closes the result set connection***//
findByArtist.close(); //***Closes the findByArtist connection***//
}catch(SQLException e2)
{
e2.printStackTrace();
}
}
public static void showFirstRow()
{
try
{
Statement selectAll=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM CDs";
System.out.println("Executing statement: " +sql);
ResultSet results = selectAll.executeQuery(sql);
while(results.first())
{
String text=(results.getString("artist"));
PrintStream jta = new PrintStream(text);
jta .append(text);
}
results.close();
selectAll.close();
}catch(SQLException e3)
{
e3.printStackTrace();
}
}
public static void showPrevRow()
{
try
{
Statement selectAll=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM CDs";
System.out.println("Executing statement: " +sql);
ResultSet results = selectAll.executeQuery(sql);
while(results.first())
{
String artist=results.getString("artist");
String title=results.getString("title");
System.out.println("First row contains: "+artist+", " +title);
}
results.close();
selectAll.close();
}catch(SQLException e3)
{
e3.printStackTrace();
}
}
public static void showNextRow()
{
try
{
Statement selectAll=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM CDs";
System.out.println("Executing statement: " +sql);
ResultSet results = selectAll.executeQuery(sql);
while(results.first())
{
String artist=results.getString("artist");
String title=results.getString("title");
System.out.println("First row contains: "+artist+", " +title);
}
results.close();
selectAll.close();
}catch(SQLException e3)
{
e3.printStackTrace();
}
}
public static void showLastRow()
{
try
{
Statement selectAll=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM CDs";
System.out.println("Executing statement: " +sql);
ResultSet results = selectAll.executeQuery(sql);
while(results.first())
{
String artist=results.getString("artist");
String title=results.getString("title");
System.out.println("First row contains: "+artist+", " +title);
}
results.close();
selectAll.close();
}catch(SQLException e3)
{
e3.printStackTrace();
}
}
}
//**************END of CDDatabase class*******************************//
//************Begin CDListener class *******************************//
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CDListener implements ActionListener {
private JTextField jtf1, jtf2;
private JTextArea jta;
public CDListener(JTextField jt1, JTextField jt2, JTextArea ja)
{
this.jtf1=jt1;
this.jtf2=jt2;
this.jta=ja;
}
public void actionPerformed(ActionEvent a)
{
Object component = a.getSource();
String textEnter = a.getActionCommand();
if(component instanceof JTextField || textEnter.equals("Enter"))
{
String b=jtf1.getText();
String c=jtf2.getText();
CDDatabase.findByArtist(b);
//CDDatabase.findByTitle(c);
jta.setText("");
}
String label = a.getActionCommand();
if(label.equals ("First"))
{
CDDatabase.showFirstRow();
}
else if(label.equals("Previous"))
{
CDDatabase.showPrevRow();
}
else if(label.equals("Next"))
{
CDDatabase.showNextRow();
}
else if(label.equals("Last"))
{
CDDatabase.showLastRow();
}
}
}
//******************* END CDListener class ****************************//
Re: Need to write ResultSet string output of one class to the JTextArea of a GUI clas
In your first thread, no less than 3 members advised you about the code tags, and I even provided a link to the relevant FAQ. So, are you lazy or stubborn?
db
Re: Need to write ResultSet string output of one class to the JTextArea of a GUI clas
Code tags.
Without them that slab of code is unreadable.
With them:
Code:
public class NicelyFormatted {
private int niceAttribute;
public int getNiceAttribute() {
return niceAttribute;
}
}
you get nicely formatted code that allows us to follow the flow.
Re: Need to write ResultSet string output of one class to the JTextArea of a GUI clas
You still haven't used code tags.