Currently have JTextArea, but would like the output to be in a JTable...

Hi there. I asked this question in the database section, but I ended up getting more questions and separating code, etc. Needless to say, my initial question went unanswered. So, for the purposes of this question, let me get this one thing out of the way:
Yes, I know this is one big file, and that I should have the GUI and DB separated. However, this is for a small project that I'm working on, and for the moment, I'm not too worried about separating class files. As long as the program works as I want it too, so much the better for me.
Now, on to the question at hand. Currently, I have a project that connects to a MySQL DB, and it displays the output from an SQL command in a JTextArea. It looks horribly ugly, as all the columns are not formatted properly. Take a look:
http://img508.imageshack.us/img508/2193/testxe4.jpg
Sure I can see columns, but I would love for the output to be displayed in a neat JTable, which is also much easier on the eyes.
Here is the current code:
package classes;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.sql.*;
import java.util.*;
public class SQLClient extends JApplet {
     private Connection connection;
     private Statement statement;
     private JTextArea jtasqlCommand = new JTextArea();
     private JTextArea jtaSQLResult = new JTextArea();
     JTextField jtfUsername = new JTextField();
     JPasswordField jpfPassword = new JPasswordField();
     JButton jbtExecuteSQL = new JButton("Execute SQL Command");
     JButton jbtClearSQLCommand = new JButton("Clear");
     JButton jbtConnectDB1 = new JButton("Connect to Database");
     JButton jbtClearSQLResult = new JButton("Clear Result");
     Border titledBorder1 = new TitledBorder("Enter a SQL Command");
     Border titledBorder2 = new TitledBorder("SQL Execution Result");
     Border titledBorder3 = new TitledBorder("Enter Database Information");
     JLabel jlblConnectionStatus1 = new JLabel("");
     JLabel jlblConnectionStatus2 = new JLabel("Not Connected");
     public void init() {
          JScrollPane jScrollPane1 = new JScrollPane(jtasqlCommand);
          jScrollPane1.setBorder(titledBorder1);
          JScrollPane jScrollPane2 = new JScrollPane(jtaSQLResult);
          jScrollPane2.setBorder(titledBorder2);
          JPanel jPanel1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
          jPanel1.add(jbtClearSQLCommand);
          jPanel1.add(jbtExecuteSQL);
          JPanel jPanel2 = new JPanel();
          jPanel2.setLayout(new BorderLayout());
          jPanel2.add(jScrollPane1, BorderLayout.CENTER);
          jPanel2.add(jPanel1, BorderLayout.SOUTH);
          jPanel2.setPreferredSize(new Dimension(100, 100));
          JPanel jPanel3 = new JPanel();
          jPanel3.setLayout(new BorderLayout());
          jPanel3.add(jlblConnectionStatus1, BorderLayout.CENTER);
          jPanel3.add(jbtConnectDB1, BorderLayout.EAST);
          JPanel jPanel4 = new JPanel();
          jPanel4.setLayout(new GridLayout(4, 1, 10, 5));
          jPanel4.add(jtfUsername);
          jPanel4.add(jpfPassword);
          JPanel jPanel5 = new JPanel();
          jPanel5.setLayout(new GridLayout(4, 1));
          jPanel5.add(new JLabel("Username"));
          jPanel5.add(new JLabel("Password"));
          JPanel jPanel6 = new JPanel();
          jPanel6.setLayout(new BorderLayout());
          jPanel6.setBorder(titledBorder3);
          jPanel6.add(jPanel4, BorderLayout.CENTER);
          jPanel6.add(jPanel5, BorderLayout.WEST);
          JPanel jPanel7 = new JPanel();
          jPanel7.setLayout(new BorderLayout());
          jPanel7.add(jPanel3, BorderLayout.SOUTH);
          jPanel7.add(jPanel6, BorderLayout.CENTER);
          JPanel jPanel8 = new JPanel();
          jPanel8.setLayout(new BorderLayout());
          jPanel8.add(jPanel2, BorderLayout.CENTER);
          jPanel8.add(jPanel7, BorderLayout.WEST);
          JPanel jPanel9 = new JPanel();
          jPanel9.setLayout(new BorderLayout());
          jPanel9.add(jlblConnectionStatus2, BorderLayout.EAST);
          jPanel9.add(jbtClearSQLResult, BorderLayout.WEST);
          this.add(jPanel8, BorderLayout.NORTH);
          this.add(jScrollPane2, BorderLayout.CENTER);
          this.add(jPanel9, BorderLayout.SOUTH);
          jbtExecuteSQL.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
          executeSQL();
          jbtConnectDB1.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
          connectToDB();
          jbtClearSQLCommand.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
          jtasqlCommand.setText(null);
          jbtClearSQLResult.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent e) {
          jtaSQLResult.setText(null);
     private void connectToDB() {
          String driver = "com.mysql.jdbc.Driver";
          String url = "jdbc:mysql://localhost:3306/petstore2002";
          String username = jtfUsername.getText().trim();
          String password = new String(jpfPassword.getPassword());
          try {
               Class.forName(driver);
               connection = DriverManager.getConnection(url, username, password);
               jlblConnectionStatus2.setText("Connected To Database");
          catch (java.lang.Exception ex) {
               ex.printStackTrace();
     private void executeSQL() {
          if (connection == null) {
               jtaSQLResult.setText("Please connect to a database first");
               return;
          else {
               String sqlCommands = jtasqlCommand.getText().trim();
               String[] commands = sqlCommands.replace('\n', ' ').split(";");
               for (String aCommand: commands) {
                    if (aCommand.trim().toUpperCase().startsWith("SELECT")) {
                         processSQLSelect(aCommand);
                    else {
                         processSQLNonSelect(aCommand);
     private void processSQLSelect(String sqlCommand) {
          try {
               statement = connection.createStatement();
               ResultSet resultSet = statement.executeQuery(sqlCommand);
               int columnCount = resultSet.getMetaData().getColumnCount();
               String row = "";
               for (int i = 1; i <= columnCount; i++) {
                    row += resultSet.getMetaData().getColumnName(i) + "\t";
               jtaSQLResult.append(row + '\n');
               while (resultSet.next()) {
                    row = "";
                    for (int i = 1; i <= columnCount; i++) {
                         row += resultSet.getString(i) + "\t";
                    jtaSQLResult.append(row + '\n');
          catch (SQLException ex) {
               jtaSQLResult.setText(ex.toString());
     private void processSQLNonSelect(String sqlCommand) {
          try {
               statement = connection.createStatement();
               statement.executeUpdate(sqlCommand);
               jtaSQLResult.setText("SQL command executed");
          catch (SQLException ex) {
               jtaSQLResult.setText(ex.toString());
     public static void main(String[] args) {
          SQLClient applet = new SQLClient();
          JFrame frame = new JFrame();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setTitle("Interactive SQL Client");
          frame.getContentPane().add(applet, BorderLayout.CENTER);
          applet.init();
          applet.start();
          frame.setSize(800, 600);
          Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
          frame.setLocation((d.width - frame.getSize().width) / 2,
          (d.height - frame.getSize().height) / 2);
          frame.setVisible(true);
}Right now it works fine, and I am planning on using this as an initial prototype for the presentation. Can I get some help on getting the output displayed into a JTable? How would I go abouts doing the conversion? I'm fairly new to Java (taking a course on it), and I'm worried that if I change one line of code the whole thing will get ruined and I'll need to start from scratch.

-> It sounds like you're getting annoyed with the amount of people asking perfectly legitimate questions
Your expectations are not legitimate. We attempt to understand your problem and give you the resources to solve the problem. Many times those resources are in the form of a tutorial or a reference to the API. We are not here to write code for you.
I pointed you in the right direction. Its up to you do to some learning on your own. You have not made the slightest effort to understand how to use a JTable. You have not made the slightest effort to search the forum to see if you question has been asked before (it has).
Your attitude and effort determines how much effort we make. Frankly you have made no effort whatsoever to solve you problem so as far as I am concerned you are on you own. Apparently I am not the only one who thinks you should be making more of an effort based on all the other advice you have received.

Similar Messages

  • I have a iPad and I connect it to my tv through hdmi but would like the sound to come out through the headphone socket, can I make this happen

    I have a iPad and I connect it to my tv through hdmi but would like the sound to come out through the headphone socket, can I make this happen.

    Go to System Preferences>Sound>Output  and make sure that you selected the right output. I think if yo use HDMI you might not be abe to select another output.

  • I have lost my simcard but would like the same number?

    I lost my simcard but would like the same number. I cannot ring up as I live in Sweden currently, but I am coming home for a week later in August. Is there a way I can get a simcard posted to my home address in England with the same number I used to have?  Also is there an email address for EE mobile network? I have looked all over but have not found anything. Thank you! 

    Order it online https://explore.ee.co.uk/Order-a-new-SIM for £10. It will be sent to your registered addy. There is no email for mobile custs.

  • Currently have M276nw and would like to print on transparencies?

    Hello All, I have had an HP M276nw for about a year and unable to find out in how to print to transparencies, been going to Kinkos in doing this, I know that Office copiers you can do this but not sure if i need to upgrade my color laser printer in doing this myself. Currently have an HP M276nw connected to Macs and Windows 8.1  Was looking if i need to upgrade to a different printer was looking at the M476xx model in doing this because the M276nw does not have a manual paper tray feed (2nd tray) Looking to print direct from computer to transparency or copy to transparency and which media is recommended to use a link would be nice. And instructions if any - Greatly Appreciated of you answers and ideas Reason for transparencies I work in education K-12 and this would be useful. ThanksiOSGenius.com  

    Hi , Thanks for getting back to me. Going back to: Checking the paper type setting (Windows). I would try the option you found: Color Laser transparency, that is the correct paper type.
     As long the printer supports the paper type or size, you shouldn't run into issues. I recommend that you use HP paper when possible. It haven't heard of transparencies melting inside of a printer before. HP LaserJet printers are designed to be flexible in the types of print media they can use. Because there are many types of media and variations can occur in the process of manufacturing media, it is important to select the best media for your printing application. Hewlett-Packard LaserJet or Multipurpose paper was designed specifically for use with your printer. If HP paper is available in your area, it is recommended that you use it. Please review this document, HP Printers - Printing and Changing Print Settings (OS X).
    Changing the paper type or size:You can change the paper type or size, such as legal or cardstock, and which paper tray to print from using the print settings.NOTE: Supported paper type, size, and weight vary by printer. For more information, view the printer specifications for your printer.On your Mac, open the photo or document you want to print.Click File, and then click Print.Make sure your printer is selected.If you do not see options on the Print dialog box, click Show Details. Located in the bottom left corner.Select the Paper Size from the drop-down menu.Under Paper Type/Quality, select the Paper Type, such as plain or photo paper.Figure : Example of paper size and paper type menus Select Paper Feed from the drop-down menu, and then select Printer Default, Tray 1, Tray 2, Tray 3, Tray 4, Manual Feed, or Envelope Feed.NOTE: The number of printer tray options varies depending on the printer.Make changes to any other print settings, then click Print.NOTE: To learn how to print on custom paper sizes, go to Creating Custom Paper Sizes.For all sales related inquiries, please contact our sales team. Call: 888-999-4747 (US) or 877-231-4351 (CA).
     Happy printing!

  • Currently using panther but would like to go to tiger but...

    i really like some of the software that came bundled with panther. i also want to update and buy ilife. is there a way to keep some of the programs that they took away such as world book? since it came bundled its not something that can just be reinstalled, but is there a way to grab just that froma back up disc or something?? i like the looks of the new lepoard and will probably get that at some point too. thanks for any help

    Hi again nancy,
    a simple upgrade will only exchange the old system files with new ones. All applications will remain were they were before the upgrade. However, in some cases an upgrade may delete old versions of system utilities and replaces them with new versions. e.g. Disk Copy was replaced because all functions of it were included into disk utility.
    If you prefer to use the older version alongside the new one you can open your backup, navigate to the application you want and simply drag&drop (=copy) it to the same directory on your new system. This will not change your backup.
    Once you have the application were yopu want it, drag it's icon to the dock and you will see it there as well.
    Good luck!

  • I like the persona I have now, but would like to try new ones too. Can I save the one I have now and go back to it someday?

    I don't know if I would be able to find it again if I changed it. Please advise?

    Yes, you can install several Personas. The first one will not be lost until you remove it yourself.
    You can remove and switch themes and Personas in Tools>Addons>Themes.

  • Where is the "Refresh Button" Saw cont+r but would like the buttom.

    As stated I can't find any "refresh / page reload" button.

    It was moved into the right-side of the Location bar, where the web address appears. You can move that button to anywhere you want it by opening the toolbar Customize mode and dragging that button to a different position.
    https://support.mozilla.com/en-US/kb/How+to+customize+the+toolbar

  • I have Photoshop cc on creative cloud but would like to add lightroom. Can I cancel my current membership and get the photography plan which would be cheaper? thanks

    I have Photoshop cc on creative cloud but would like to add lightroom. Can I cancel my current membership and get the photography plan which would be cheaper? thanks

    yes.
    contact adobe support by clicking here and, when available, click 'still need help', https://helpx.adobe.com/contact.html

  • I have a time capsule and two air port expresses. The time capsule is in the middle of my house, the expresses equidistant in opposite directions. I would like the expresses to both connect directly to the capsule, but they are connected in serial. How?

    I have a time capsule and two air port expresses. The time capsule is in the middle of my house, the expresses equidistant in opposite directions. I would like the expresses to both connect directly to the capsule, but they are connected in serial. How to reconfigure.
    I ask because I have music running out of both expresses, and the one that is most distant from the time capsule (routed through the first express) regularly drops the music signal from itunes. And I am guessing the serial configuration of the routers has something to do with this, but my root problem is music dropout - any thoughts would be appreciated.

    If you have the the most current model of the Express devices, the default setup is to "extend" the wireless signal.  In this type of setup, each Express communicates directly to the Time Capsule.
    How did you setup the AirPort Express devices?
    What version of the Mac operating system are you using on your MacBook?   Or, do you have AirPort Utility installed on your iPad?

  • I purchased an iPhone 5S using my EDGE upgrade on September 5, but would like to exchange it for an iPhone 6 within the 14 day period. Other than having to pay the $35 restocking fee, will I have any problems doing so? Thanks

    I purchased an iPhone 5S using my EDGE upgrade on September 5, but would like to exchange it for an iPhone 6 within the 14 day period. Other than having to pay the $35 restocking fee, will I have any problems doing so? Thanks

        Good evening udflyers! You should not have any problem returning/exchanging your current within 14 days http://bit.ly/IeiJI of purchase. However, you may have to preorder the device you want as it is not currently available and preorder start on 9/12/14 at 12:01am, PST. Please process your return/exchange thru the same channel as your original purchase.
    AntonioC_VZW Follow us on Twitter at www.twitter.com/VZWSupport

  • HT204053 I have 4 iphones on the same Apple ID, but would like 4 different accounts and iCloud is this possible?

    I have 4 iphones on the same Apple ID, but would like 4 different accounts and iCloud is this possible?

    Create a new Apple ID for the one you want to change.

  • I just inherited a 64GB ipad. I have passwords for the deceased person but would like to sync it with my mac osx.  Know nothing about ipads. Help please

    I just inherited a 64 GB ipad.  I have passwords for the deceased person but would like to reset everything and start new. I use a mac/0SX but know nothing about an ipad other than turning it on.  I am in the same household as the deceased person but does not seem to be recognizing the wireless setup.  Any help would be appreciated. Any recommendations as to where to start.

    That's really enough information. You can update this iPad to iOS 6.1.2. If there is a microphone icon next to the space bar on the keyboard, you have an iPad 3. But if you are saying that it hasn't been touched for over a year, it probably is not the iPad 3. The iPad 3 is one year old this week IIRC.
    If you want, you can go to Settings >General>About>Model to find the model number and it you Google that, you can find out which version of the iPad it is.
    I just wanted to see what iOS you would be able to update to. I'm not sure what else to tell you other than what we've already stated. You should set this up as new, update the iOS and then start all over again with you own iTunes content.
    The link that Eric posted would be a good place to start so that you can get an idea of what the iPad can do. If you want to restore the iPad back to factory settings, you will need to do this with iTunes on your Mac. You must be running OS X 10.6.8 at minimum and must be using iTunes 10.6.3 (I think) in order to syn with iTunes on the Mac.
    This explains how to restore to factory settings.
    http://support.apple.com/kb/ht1414
    if you are that uncomfortable trying to do this on your own, and if there is an Apple Store near you, make an appointment at an Apple Store Genius Bar and ask an Apple tech to help you with it. That is what they do. There is no charge for an Apple Store Genius Bar visit. The stores also offer classes for the iPad that you might find useful as well.

  • I have Adobe Acrobat XI standard, but would like to trial the "compare documents" feature on Pro. I tried to download a trial version of Pro, but the installer indicated that "it detected a more fully featured version of the product" - presumably my stand

    Okay - just realised that the last screen was just asking for a "title" for my question!
    I have Adobe Acrobat XI standard, but would like to trial the "compare documents" feature on Pro. I tried to download a trial version of Pro, but the installer indicated that "it detected a more fully featured version of the product" - presumably my standard version? - and stopped installation. It is not clear to me what my options are for either upgrading to Pro, or selectively purchasing the "compare documents" plugin (if possible). I am using what is presumably an Enterprise version of acrobat (i.e. my employer owns it, not me) - so not sure what limitations that results in. I think this is a simple question, but would be grateful for advice.

    - You can't install Acrobat Pro alongside Acrobat Standard. You will need to remove Standard to be able to install Pro.
    - You (probably) can't upgrade your license if it's a part of an enterprise license. The entire license will need to be upgraded.
    - It's not possible to install just the "Compare Documents" function as a sort of add-on to Acrobat Standard.

  • I have an I phone 4 that I use in Singapore, but would like to use it in the US while on holiday. How do I cahnge out the sim card, and is there anything else that I need to do.

    I have an I phone 4 that I use in Singapore, but would like to use it in the US while on holiday. How do I change out the sim card, and is there anything else that I need to do?

    Ignore the other post (the one from Brendan), he has no idea what he is talking about.
    Read your manual (you have a bookmark to it in Safari on your iPhone or put iBooks on your iPhone and download the manual from the iBookStore) and it will show you how to remove the microSIM from your iPhone. AT&T is not tourist friendly and does not offer a pay as you go plan for visitors with an iPhone. If you are going to be here for a short time you can probably buy a pay as you go microSIM and use it before AT&T notices, if it is going to be for a long time that won't work.
    There is a 3rd party company that will sell you a microSIM that will work with AT&T but they are pretty pricey.
    Finally, you can get a pay as you go microSIM from T-Mobile to work with your iPhone but you will only have EDGE data speeds.
    Message was edited by: deggie

  • I have an old iphone 3 32gb and don't use it to make calls as I have a 4s but would like to use the 3 for music any suggestions as to how I can sync two phones with I tunes

    I have an old iphone 3 32gb and don't use it to make calls as I have a 4s but would like to use the 3 for music any suggestions as to how I can sync two phones with I tunes

    Plug the device into the computer.
    Select the content desired to sync.
    Sync.

Maybe you are looking for