6 Replies - 942 Views - Last Post: 11 July 2013 - 04:28 AM Rate Topic: -----

#1 IceHot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 220
  • Joined: 28-August 12

20 Objects all having a unique Connection to the same database

Posted 09 July 2013 - 06:45 PM

I know, I know. This question spans two topics: Java and Derby databases. I am in the process of making a GUI that displays results, and I have to use 20 JPanels. Yes, 20. It will look something like this when I am done with it: Posted Image. I have two JPanels that will house two tables (each table queries the database once), and will have 18 JPanels for the different difficulties for each mode (the second mode will have only four difficulties, while the first one will have five). These JPanels (or more precisely, ChartPanels) will each perform a query on the relational database.

What I have done so far is test to see if the two JPanels I have already can have their own unique Connection to the Derby database. So far, it works. What I am concerned about is if the whole test code will fall apart if I have ALL 20 Objects have their own Connection, or if such plan is doable, but a bad practice.

Also, I am wondering if I should give each one of these Objects a general Statement or a PreparedStatement, and why would I make such choice. //I am acclimated to just using regular Statements with either no parameters or two parameters.

Is This A Good Question/Topic? 0
  • +

Replies To: 20 Objects all having a unique Connection to the same database

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: 20 Objects all having a unique Connection to the same database

Posted 09 July 2013 - 08:30 PM

You have one connection that updates 20 object. You could have 20 different, synchronized connections, but why?

Always use PreparedStatements for security.
Was This Post Helpful? 0
  • +
  • -

#3 IceHot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 220
  • Joined: 28-August 12

Re: 20 Objects all having a unique Connection to the same database

Posted 10 July 2013 - 03:05 PM

Two of the Objects (the two that I currently have), house JTables that use an AbstractTableModel. It is the AbstractTableModels that need access to the database. But, I am sure that I could simply do something like:

import javax.swing.table.AbstractTableModel;

import java.sql.*;
//some other import statements, if necessary

public class SampleTableModel extends AbstractTableModel
{
	private Connection connection = null;
	private Statement statement = null;	//this could also be a PreparedStatement 
	private ResultSet resultSet = null;	
	
	private final String[] tableNames = {"UserTableA", "UserTableB"};
	private boolean connectedToDatabase = false;	//this is for checking to see if(connectedToDatabase)
	//otherwise, we throw IllegalStateException; we shouldn't even be doing anything if connection == null
	
	public SampleTableModel(int x, Connection connection) throws SQLException
	{
		this.connection = connection;
		//as mentioned earlier, this could be set to a PreparedStatement, too, but I don't know what security
		//measures I would circumvent by just using this assignment
		statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
		//attempt query here
		resultSet = statement.executeQuery("SELECT * FROM "+tableNames[x]);
		connectedToDatabase = true;
	}
	
	//required method overrides here
}



for what I have now, and as for the 18 ChartPanels I have now, I can do something similar (give their constructor a Connection argument so that they could use the same Connection, as you have suggested). Of course, I would have to close() both the ResultSet and the Statement, right? //and leave the one Connection shared by all the Objects to be closed in the class that uses all the Objects.

Also, I am a newb at multithreaded/concurrent programming, and would like to know more about it.

This post has been edited by IceHot: 10 July 2013 - 03:07 PM

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3425
  • View blog
  • Posts: 15,490
  • Joined: 20-September 08

Re: 20 Objects all having a unique Connection to the same database

Posted 11 July 2013 - 02:22 AM

Why would you want to display so much of your data all at the same time? In addition to being confusing and chaotic to the user, it also creates an unnecessary overhead
Was This Post Helpful? 0
  • +
  • -

#5 IceHot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 220
  • Joined: 28-August 12

Re: 20 Objects all having a unique Connection to the same database

Posted 11 July 2013 - 04:17 AM

I was thinking that, since I am still running the beta test to this (I got nobody who is as dedicated to this as I am, and thus no data yet except my own :(/>/>/>/>/> ), I would add in a chart representation of the data. My original plan was to have the JFrame with just the JTables, followed by a ConfirmDialog asking the user if they want to see a chart representation of their data. If they answer "Yes", then another JFrame would pop up with all the chart data. However, I then learned from this source on Stack Overflow, this would be a very bad practice because:
  • This would appear as two icons in the Taskbar
  • JFrames are resource-heavy


So instead, I thought of making all the data in one JFrame. Hence, this idea gets its chance.
PrintStream stdout = System.out;
stdout.println("I guess it is impossible to win in this type of situation, now is it.");



But, there will only be a maximum of 12 JPanels shown at one time (including any of the ChartPanels that I will be using for the bottom JTabbedPane).

This post has been edited by IceHot: 11 July 2013 - 04:22 AM

Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3425
  • View blog
  • Posts: 15,490
  • Joined: 20-September 08

Re: 20 Objects all having a unique Connection to the same database

Posted 11 July 2013 - 04:24 AM

My answer to http://www.dreaminco...roblem-buttons/ would be appropriate for you too maybe. A standard menu approach for each view is also worth looking at
Was This Post Helpful? 1
  • +
  • -

#7 IceHot  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 220
  • Joined: 28-August 12

Re: 20 Objects all having a unique Connection to the same database

Posted 11 July 2013 - 04:28 AM

Your answer was exactly what I was planning on doing!
//bright minds think alike!


I don't quite know what you mean by "standard menu approach", however //that is if you are not talking about giving the JFrame a JMenuBar with only one JMenu containing the two GameModes as JMenuItems. I have already thought of this, but have decided against it, given that in going with the idea, I would be using such a powerful Container for only two items.

This post has been edited by IceHot: 11 July 2013 - 09:10 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1