I need to make a program that asks data from a mysql database displays it. (in the full program it also needs to be able to create, alter and delete rows but I'm already stuck with displaying). The way I went at this is by making a connection with jdbc. Then execute a query and put that in a resultset. Then using a if statement to get the first row of information with resultset.next and a bunch of parsestring and settext methods.
The next step is a next and previous button to scroll through the resultset and display the information.
My plan was to put a rs.next() in the actionlistener and redo all the setTexts on the textfields with the new strings but I've tried to a 100 different ways it feels like but I keep getting errors one way or the other.
Someone also suggested I should try and set the actionlistener to add 1 to a variable. And then putting the rest of the code in a class where I can call it with a method but I don't know how to make a method like that.
Any help whatsoever would be amazing. I've been coding for only a few weeks now and I'm feeling like I am in way over my head. So if there are any other things I'm doing wrong please tell me. Cheers
The code
package duo.opdracht.info.pagina;
import javax.swing.*;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
public class DUOOpdrachtInfoPagina extends JFrame {
public static void main(String[] args) {
JFrame frame = new DUOOpdrachtInfoPagina();
frame.setSize(1000, 750);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Optredens");
frame.setContentPane(new Paneel());
frame.setVisible(true);
}
}
class Paneel extends JPanel {
private JButton volgendeKnop, vorigeKnop;
private JTextField bandArtiestVak, starttijdVak, eindtijdVak, podiumVak;
private JLabel bandArtiestLabel, starttijdLabel, eindtijdLabel, podiumLabel;
public Paneel() {
setLayout(null);
volgendeKnop = new JButton("Volgende");
volgendeKnop.addActionListener(new ButtonHandler());
vorigeKnop = new JButton("Vorige");
bandArtiestVak = new JTextField(20);
starttijdVak = new JTextField(20);
eindtijdVak = new JTextField(20);
podiumVak = new JTextField(20);
bandArtiestLabel = new JLabel("Artiest / Band: ");
starttijdLabel = new JLabel("Starttijd: ");
eindtijdLabel = new JLabel("Eindtijd: ");
podiumLabel = new JLabel("Podium: ");
volgendeKnop.setBounds(850, 600, 100, 25);
vorigeKnop.setBounds(50, 600, 100, 25);
bandArtiestVak.setBounds(200, 100, 300, 25);
starttijdVak.setBounds(200, 130, 300, 25);
eindtijdVak.setBounds(200, 160, 300, 25);
podiumVak.setBounds(200, 190, 300, 25);
bandArtiestLabel.setBounds(100, 100, 300, 25);
starttijdLabel.setBounds(100, 130, 300, 25);
eindtijdLabel.setBounds(100, 160, 300, 25);
podiumLabel.setBounds(100, 190, 300, 25);
bandArtiestVak.setEditable(false);
starttijdVak.setEditable(false);
eindtijdVak.setEditable(false);
podiumVak.setEditable(false);
add(volgendeKnop);
add(vorigeKnop);
add(bandArtiestVak);
add(starttijdVak);
add(eindtijdVak);
add(podiumVak);
add(bandArtiestLabel);
add(starttijdLabel);
add(eindtijdLabel);
add(podiumLabel);
// create our mysql database connection
try {
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/DUO_1", "duo1", "duo1");
Statement stmt = null;
String query
= "SELECT BandArtiestNaam, Starttijd, Eindtijd, Podiumnaam FROM optreden";
stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs = stmt.executeQuery(query);
if (rs.next()) {
String naam = rs.getString(1);
String stijd = rs.getString(2);
String etijd = rs.getString(3);
String pnaam = rs.getString(4);
bandArtiestVak.setText(naam);
starttijdVak.setText(stijd);
eindtijdVak.setText(etijd);
podiumVak.setText(pnaam);
}
} catch (SQLException e) {
System.out.println(e);
}
}
class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) {
}
}
}
Break your problem down into different concerns, you shouldn't have a class which does absolutely everything in your application. You will find, as you are right now, that when you have a problem, it is very hard to track down, much harder than you could have ever imagined. Lets rather start by breaking this problem down into manageable chunks, that's really all programming is.
This is a design pattern called MVC - (model view controller) different variations exist for different applications desktop/web ect. This will allow you to separate each concern into the relevant component, and test to see if each one is working, and where the problem is. You will be able to test, for instance the Dao, without needing to create a view and add listeners. Sounds like you are learning so I strongly suggest you learn this design pattern. You will find that you are unable to make a substantial application with your current design, as problems becoming ever increasingly harder to track down, and new functionality harder to add.
A few more things, you should really just convert the whole result set into something more meaningful, it's already loaded. Using rs.next every time you want a new row will mean you cannot close the result set, and you'll probably have all these resource warnings. Otherwise check out hibernate. I very much liked this site when I was starting to learn programming in Java, I suggest you check it out https://www.caveofprogramming.com/java-design-patterns/mvc-example.html