Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why wont my java UI code code connect to my databased. I keep getting errors: package MedicalLoginApp; import javax.swing. * ; import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

Why wont my java UI code code connect to my databased. I keep getting errors:
package MedicalLoginApp;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.opencsv.CSVWriter;
public class MedicalAppUI extends JFrame {
private static final long serialVersionUID =1L;
private JTextField usernameField;
private JPasswordField passwordField;
public MedicalAppUI(){
JLabel usernameLabel = new JLabel("Doctor's Username:");
JLabel passwordLabel = new JLabel("Doctor's Password:");
usernameField = new JTextField();
passwordField = new JPasswordField();
JButton loginButton = new JButton("Login");
JButton exportButton = new JButton("Export to CSV");
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
add(usernameLabel);
add(usernameField);
add(passwordLabel);
add(passwordField);
add(loginButton);
add(exportButton);
loginButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
String username = usernameField.getText();
char[] password = passwordField.getPassword();
try {
if (DatabaseConnector.authenticate(username, new String(password))){
MedicalAppMain();
} else {
JOptionPane.showMessageDialog(MedicalAppUI.this, "Sorry, Invalid username or password");
}
} catch (SQLException ex){
JOptionPane.showMessageDialog(MedicalAppUI.this, "There is an error connecting to the database");
}
}
});
exportButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
exportDataToCSV();
}
});
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void exportDataToCSV(){
try {
ResultSet resultSet = DatabaseConnector.getAllDoctors();
String csvFilePath = "output.csv";
try (CSVWriter writer = new CSVWriter(new FileWriter(csvFilePath))){
writer.writeNext(new String[]{"Doctor_FirstName", "Doctor_LastName", "Department"});
while (resultSet.next()){
String[] data ={
resultSet.getString("Doctor_FirstName"),
resultSet.getString("Doctor_LastName"),
resultSet.getString("Department")
};
writer.writeNext(data);
}
JOptionPane.showMessageDialog(MedicalAppUI.this, "Data exported to CSV successfully. File: "+ csvFilePath);
}
} catch (SQLException | IOException e){
JOptionPane.showMessageDialog(MedicalAppUI.this, "Error exporting data to CSV");
}
}
private void MedicalAppMain(){
this.dispose();
}
public static void main(String[] args){
SwingUtilities.invokeLater(()-> new MedicalAppUI());
}
}
and this is mypackage MedicalLoginApp;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
public class DatabaseConnector {
private static final String URL ="jdbc:mysql://localhost:3306/Medical_login";
private static final String USER = "Username";
private static final String PASSWORD = "Password";
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USER, PASSWORD);
}
public static boolean authenticate(String Username, String Password) throws SQLException {
String sql = "SELECT * FROM Users WHERE username =? AND password =?";
try (Connection connection = getConnection();
PreparedStatement statement = connection.prepareStatement(sql)){
statement.setString(1, Username);
statement.setString(2, Password);
try (ResultSet resultSet = statement.executeQuery()){
return resultSet.next();
}
}
}
public static void insertDoctorsData(List data) throws SQLException {
String sql = "INSERT INTO Doctors (Doctor_FirstName, Doctor_LastName, Department) VALUES (?,?,?)";
try (Connection connection = getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(sql)){ database connector code:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions