The problem of my code is that it can't set the data from my database onto my jtextfield. Every time I run my code, it says StackOverflowError. How could I fix this error?
Here's my code
public class DataConnect extends DatabaseGUI {
private Connection dataCon;
private Statement dataStmt;
private ResultSet dataRS;
private String name, address;
int age;
public DataConnect(){
try{
Class.forName("com.mysql.jdbc.Driver");
dataCon = DriverManager.getConnection("jdbc:mysql://localhost:3306/studentrec","root","");
dataStmt = dataCon.createStatement();
dataRS = dataStmt.executeQuery("Select * from studentrecords");
}catch(Exception exc){
System.out.println(exc.getMessage());
}
}
public void setData(){
try{
dataRS.next();
name = dataRS.getString("Name");
age = dataRS.getInt("Age");
address = dataRS.getString("Address");
DatabaseGUI dbGui = new DatabaseGUI ();
dbGui.jtfData[0].setText(name);
dbGui.jtfData[1].setText(Integer.toString(age));
dbGui.jtfData[2].setText(address);
//System.out.println(name+" "+age+" "+address);
}catch(Exception exc){
}
}
}
A possible cause of your problem is here:
public class DataConnect extends DatabaseGUI {
//...
public DataConnect(){
//...
}
public void setData(){
try{
DatabaseGUI dbGui = new DatabaseGUI (); //*****
You're creating a new DatabaseGUI object within a class that extends DatabaseGUI and risk infinite recursion. Solution: probably you don't want DataConnect to extend DatabaseGUI, or you don't want to create a new DatabaseGUI inside of DataConnect, or both. If you already have a displayed DatabaseGUI, then you don't want to create another non-displayed one, but instead will want to set the data of the components of the GUI that is already displayed, not a new one that you create on the spot.
Also, I have to wonder -- do you create a DataConnect object within your DatabaseGUI class? Your question can be improved greatly by your doing a bit of debugging to find out which code in particular causes the exception to occur.
Also this empty catch block
catch(Exception exc){
}
is code that should never happen. At least print the stacktrace.
Related
i have a question regarding a program i am working on. It`s a database manager for a MqSQL db , written in Java. So i have the following program structure.
So i have a main class that extends JFrame, whichh is the main frame of the interface, like this (removed the unecessary code not relevant to the discussion) :
public class MainInterface extends JFrame {
public MainInterface {
................
MainInterface.setLayout(new FlowLayout());
MainInterface.setVisible(true);
TopToolbar toolbar;
try {
toolbar = new TopToolbar();
MainInterface.add(toolbar);
ResultsPanel Results = new ResultsPanel();
MainInterface.add(Results);
} catch (IOException e) {
e.printStackTrace();
}
}
TopToolbar and ResultsPanel are 2 other classes that extend JPanel, the TopToolbar class having a JToolBar with buttonsadded to it (Move Forward, MoveBack, Add entry)
public class TopToolbar extends JPanel implements ActionListener {
TopToolBar()
{
//constructor in which i was adding button to the toolbar, not relevat
}
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (MoveFirst.equals(cmd)){
try {
DatabaseAccess disp = new DatabaseAccess();
disp.getResults();
int id = disp.return_id();
System.out.println(id);
} catch (//exceptions) {
e1.printStackTrace();
}
}
That is the ActionListener event for the next button, which should trigger reading the next entry in the db
DatabaseAccess is another class with initializes the DB connection , and has these 2 methods :
public void getResults() throws SQLException {
Connection con = (Connection) DriverManager.getConnection(URL, user, "")
sql = (Statement) con.createStatement();
result_set = sql.executeQuery("select * from persons");
while (result_set.next()) {
id = result_set.getInt(1);
name = result_set.getString(2);
}
}
public int return_id(){
return id;
}
The return_ID method returns (and it does work) the ID (first key in the database, will obviously add methods for the rest of the entries in the db).
Now i want to show the ID in the final JPanel, the one called ResultsSet (just 3 JLabels and 3 TextFields for the ID , Name etc., in a GridLayout).
Since the dababase class creation (and subsequently the methods forr reading the db and returning the result) is done inside the ActionPerformed method inside the TopToolBar Jpanel, i can`t access it from the MainInterface JFrame and then use something like
ResultsPanel DispResults = new ResultsPanel();
add(DispResults);
DispResults.setID(id_Value)
where setID would be a method in the ResultsPanel that uses the JTextBox setText to set the text .
Hoope i`ve managed to explain my issue as clear as i can.
I disagree with several of your choices.
You should not extend JFrame. Create a JPanel and give it to a JFrame to display.
I would dissociate the database interactions from the Swing UI. Create them as interface-based POJOs on their own, without a UI. Get them tested, written, and working perfectly.
Once the database interactions are perfect, give an instance of that object to the class that controls the application. Give it a text-only interface for driving the database actions.
Once the controller is obeying every text command perfectly, using your fully tested database component, then have it create an instance of your JPanel and give it to a JFrame. It will make the same calls to its controller owner that your text UI has already executed successfully.
Computer science is all about decomposition. You solve big problems by breaking them into smaller ones.
If you'd like to see a great example of what your app might look like, download SQL Squirrel.
So I'm having these classes
public class Init {
...
JFrame addStream = new AddStream();
addStream.setVisible(true);
addStream.setLocationRelativeTo(null);
addStream.getData(); //not working
}
public class AddStream extends javax.swing.JFrame {
private String nameData, urlData, qualityData;
/** Creates new form AddStream */
public AddStream() {
initComponents();
}
private void initComponents() {
...
}
private void addActionPerformed(java.awt.event.ActionEvent evt) {
nameData = name.getText();
urlData = url.getText();
qualityData = quality.getSelectedItem().toString();
}
public String[] getData() {
return new String[]{nameData, urlData, qualityData};
}
}
Note the classes arent complete, just snippets.
When the user clicks on the Add button(addActionPerformed) the values get saved to local variables in the AddStream class and get returned by getData().
The problem I'm having is with addStream.getData();, I get "cannot find symbol"
Is there a way to get that data from AddStream JFrame to Init class?
Your problem can be easily fixed by changing this line:
JFrame addStream = new AddStream();
To this:
AddStream addStream = new AddStream();
What's happening in your code is that you're trying to call a method on a JFrame that doesn't exist on a JFrame, it only exists in an AddStream. Even though your JFrame is-a AddStream in this case, the compiler forbids this unless you tell the compiler that it is-a AddStream. And you do that with the code I've shown you.
Another way is to cast it in your call. Imagine you were using your code from above, you could then do this on your last line:
((AddStream) addStream).getData();
In runtime when you do
JFrame addstream = new AddStream();
the object is viewed as a simple JFrame (using the JFrame part of class AddStream).
getData() is only available for AddStream type objects. You could trick the JVM into using the assigned type
if( addstream instanceof AddStream ){
(AddStream) addstream.getData();
} else {
//TODO
}
this is sometime useful when switching between different implementations of the same Interface. Note that the cast is only there to pass the compiler. The runtime checks instanceof only and go aheads if the condition evaluate to true.
This question already has answers here:
Better understaning - Class.forName("com.mysql.jdbc.Driver").newInstance ();
(4 answers)
Closed 10 years ago.
I am a java beginner and trying to insert a row in db. This is first time in java i am performing insertion operation. For around 2 Hrs i was googling and frustated and cannot solve my error. I called my friend and he gave live support for me in team viewer and added just one line of code to my program.
Class.forName("com.mysql.jdbc.Driver")
Can anyone please explain why we need to include this in my code before connection string. Is it necessary to place my code there each and every time. Please Explain me in Detail.
Here is some very simplified code that illustrates how driver initialization works. There are 3 classes, please put each one in an appropriately-named file.
import java.util.HashMap;
import java.util.Map;
public class DriverMgr {
private static final Map<String, Class<?>> DRIVER_MAP = new HashMap<String, Class<?>>();
public static void registerDriver(final String name, final Class<?> cls) {
DRIVER_MAP.put(name, cls);
}
public static Object getDriver(final String name) {
final Class<?> cls = DRIVER_MAP.get(name);
if (cls == null) {
throw new RuntimeException("Driver for " + name + " not found");
}
try {
return cls.newInstance();
} catch (Exception e) {
throw new RuntimeException("Driver instantiation failed", e);
}
}
}
public class MysqlDriver {
static {
// hello, I am a static initializer
DriverMgr.registerDriver("mysql", MysqlDriver.class);
}
#Override
public String toString() {
return "I am the mysql driver";
}
}
public class TestProg {
public static void main(final String... args) {
try {
Class.forName("MysqlDriver"); // try with or without this
} catch (Exception e) {
throw new RuntimeException("Oops, failed to initialize the driver");
}
System.out.println(DriverMgr.getDriver("mysql"));
}
}
When you call Class.forName, the driver class gets loaded and the static initializer gets executed. That in turn registers the driver class with the driver manager, so that the manager is now aware of it. Obviously, this only needs to be done once.
Class.forName("com.mysql.jdbc.Driver")
It must be required to load the driver of database which you are using.
the forNmae() method in this line load the driver of mysql database.
Yes , it's necessary to include every time .
but you can use a method for not repeating the codes .
for example
public void connectToMYSQL(){
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/yourdatabase","username",""password);
}catch(Exception ex)
{
System.out.println(ex.getMessage());
}
}
and before writing any sql statement , just call the method for example
public void insert(String sql)
{
connectToMYSQL();
//.. then do your stuffs
}
The basic idea is that this action forces the driver class to be registered in JDBC's driver manager.
The method Class.forName("fully qualified class name) is used to initialize the static fields of the class and load the JDBC driver class, MySQL driver in your case, to your application. When it is instantiated, it gets registered with the DriverManager. By the latter you create connections, using Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","login","password");, which you later use to query the database.
I am building my own GUI that will display a list of Friend's objects in list form. The first problem I ran into is that when I run the code without a constructor, everything works fine. But when I create a constructor for my GUI class, the error message displayed:
load: GUIapp.class is not public or has no public constructor.
java.lang.IllegalAccessException: Class sun.applet.AppletPanel can not access a member of class GUIapp with modifiers ""
at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:65)
at java.lang.Class.newInstance0(Class.java:349)
at java.lang.Class.newInstance(Class.java:308)
at sun.applet.AppletPanel.createApplet(AppletPanel.java:807)
at sun.applet.AppletPanel.runLoader(AppletPanel.java:714)
at sun.applet.AppletPanel.run(AppletPanel.java:368)
at java.lang.Thread.run(Thread.java:680)
My Code:
public class GUIapp extends JApplet{
/*
* Attributes
*/
//** Friends Objects**//
private FriendsGroup a;
private ArrayList<friends> friendList;
//** PANEL **//
private JPanel outerPanel;
//** Button **//
private JButton button1;
/*
* Constructor for Getting all the friends set up
*/
private GUIapp(){
a = null; //initialize variable
try {
a = new FriendsGroup("friends.txt"); //import friend list
} catch (IOException e) {
System.out.println("Fail Import.");
}
friendList = a.getFriendsGroup(); //return an arrayList of Friends Object
}
/*
* Create Stuff
*/
public void createStuff() {
outerPanel = new JPanel(); //create outer panel
button1 = new JButton("Click Me");
outerPanel.add(button1,BorderLayout.SOUTH);
}
/*
* Initialize Stuff
*
*/
public void init(){
createStuff(); //initialize create stuff
this.add (outerPanel);
}
}
In the Above Code, if you take out the constructor, it seems to work perfectly. My Question is, what is wrong with the code? Why can't I seem to create a constructor to load in data first?
My Second Question is how would I go about create a panel whereby it displays a list of friends names? Theses names are imported and stored in the arraylist of friends Object called friendList stored in the constructor.
Thanks,
when you are defining a constructor by yourself
compiler will not create the default constructor
since your defined constructor is private
you will not have a public constructor
so simply create a public constructor
public GUIapp(){
// your code
}
because you define constructor private change it to;
public GUIapp(){
a = null; //initialize variable
try {
a = new FriendsGroup("friends.txt"); //import friend list
} catch (IOException e) {
System.out.println("Fail Import.");
}
friendList = a.getFriendsGroup(); //return an arrayList of Friends Object
}
The problem is this: private GUIapp(){. That means that your constructor is available only to that class. Usually constructors are public, although there do exist exceptions, example of which could be the Singleton Pattern.
Removing the constructor works because each class, by default has a parameterless constructor. Take a look at this tutorial for more information on access modifiers.
Alternatively, you could have a method like so in your GUIapp class:
public static GUIapp getInstance() { return new GUIapp(); }
and you call that from your main class, but I think that in this case, changing your constructor from private to public should be enough.
Regarding your second question, this tutorial should be of help.
You ened to change syour constructor to public and debug into:
a.getFriendsGroup();
Its not clear what this methode does, and i assume for some reason (maby the list from the file is empt) the methode tries to access a non assigned object which causes null reference exception, try to debug into the methode to see where this happends or post the code of the methode.
just started using the Serializable-thingy, I want to save a couple of lists and some other objects, but I can't seem to get some things right.
I have some JLists with their own "DefaultListModel" that I use to sort things in the list, I call the class SortModel, when I run the program I do this:
SortModel sortModel = new SortModel();
JList jList = new JList(sortModel);
Then later when the program runs, objects are added and sorted according to the specified needs, now, when I save the lists and load them again, they're empty.
I also save a object of a class that holds the background for the program (user chooses one themself), after saving it I need to add it again to the program (the object, background is stored in it), I need to add it to the program again not only load it "as it where", plus I have some objects that I added on that object with their own listeners. After I somehow succeeded in loading it, the objects are there but I can't use them, so I figure listeners don't get saved?
* explaining edit
The class that is the program extends JFrame, nothing funny about that I think.
The "background obect" (call it map) extends JComponent, I add this to (let's call it program for now...) the program and it pops up with the image which it holds. Onto this map I then add objects that also extends JComponent (call them dots), the dots are assigned their own listeners before they're added, the listeners might not be "real" listeners, but they act the same way, they're "MouseAdapter" does that makes any difference?.
/explaining edit *
* code edit *
code for saving:
FileOutputStream fOut = new FileOutputStream("testFile.mpd");
ObjectOutputStream outSt = new ObjectOutputStream(fOut);
outSt.writeObject(savedMap);
"testFile.mpd" is what it sounds like, I'm quite sure the .mpd shouldn't matter, you can make up your own formats, right? :) (main-class is called Mappedoodle, .mpd sounds reasonable, no?)
"savedMap" is an object of said Mappedoodle and holds all lists and other information needed to be saved.
code for loading:
FileInputStream fIn = new FileInputStream("testFile.mpd");
ObjectInputStream inSt = new ObjectInputStream(fIn);
Mappedoodle openedMap = (Mappedoodle)inSt.readObject();
The information in openedMap is used (well... it should be...) to overwrite certain things in the program.
* /code edit *
Adding everything back onto this object, even adding everything back into the lists wouldn't be so hard since that's just some more lists and a few loops, but I feel like I just don't really get Serializable ^^ so, someone care to try to explain why not everything gets saved? And if it is, why I can't access it? But if I can, how? :)
I don't know what more code should be relevant, please tell me what more information you would need to help me solve this, pasting the whole program would be really messy since it's 11 classes and quite a few lines.
Thanks <3
The code you must show us must be sufficient to demonstrate your error, and I unfortunately must state that yours doesn't. For instance if I use your code in a very simple example (something I recommend that you do), you'll see that it works. My test code:
Mappedoodle.java
import java.io.Serializable;
public class Mappedoodle implements Serializable {
private static final long serialVersionUID = -1760231235147491826L;
private String text;
private int value;
public Mappedoodle(String text, int value) {
this.text = text;
this.value = value;
}
public String getText() {
return text;
}
public int getValue() {
return value;
}
#Override
public String toString() {
return text + ", " + value;
}
}
MappedoodleSerialize.java
import java.io.*;
public class MappedoodleSerialize {
public static void main(String[] args) {
Mappedoodle savedMap = new Mappedoodle("Fubar", 200);
FileOutputStream fOut;
try {
// your code below
fOut = new FileOutputStream("testFile.mpd");
ObjectOutputStream outSt = new ObjectOutputStream(fOut);
outSt.writeObject(savedMap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
MappedoodleDeSerialize.java
import java.io.*;
public class MappedoodleDeSerialize {
public static void main(String[] args) {
try {
// your code below
FileInputStream fIn = new FileInputStream("testFile.mpd");
ObjectInputStream inSt = new ObjectInputStream(fIn);
Mappedoodle openedMap = (Mappedoodle) inSt.readObject();
System.out.println(openedMap);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
}
}
}
This bit of code compiles, runs and outputs as expected. Your error must lie in code that you've not shown us.
This problem was solved 5 years ago, the solution is lost due to bad memory though.