Question: 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.ioFileWriter;
import java.ioIOException;
import java.sqlResultSet;
import java.sqlSQLException;
import com.opencsv.CSVWriter;
public class MedicalAppUI extends JFrame
private static final long serialVersionUID L;
private JTextField usernameField;
private JPasswordField passwordField;
public MedicalAppUI
JLabel usernameLabel new JLabelDoctors Username:";
JLabel passwordLabel new JLabelDoctors Password:";
usernameField new JTextField;
passwordField new JPasswordField;
JButton loginButton new JButtonLogin;
JButton exportButton new JButtonExport to CSV;
setLayoutnew BoxLayoutgetContentPane BoxLayout.YAXIS;
addusernameLabel;
addusernameField;
addpasswordLabel;
addpasswordField;
addloginButton;
addexportButton;
loginButtonaddActionListenernew ActionListener
@Override
public void actionPerformedActionEvent e
String username usernameField.getText;
char password passwordField.getPassword;
try
if DatabaseConnectorauthenticateusername new Stringpassword
MedicalAppMain;
else
JOptionPane.showMessageDialogMedicalAppUIthis, "Sorry, Invalid username or password";
catch SQLException ex
JOptionPane.showMessageDialogMedicalAppUIthis, "There is an error connecting to the database";
;
exportButton.addActionListenernew ActionListener
@Override
public void actionPerformedActionEvent e
exportDataToCSV;
;
setDefaultCloseOperationJFrameEXITONCLOSE;
pack;
setLocationRelativeTonull;
setVisibletrue;
private void exportDataToCSV
try
ResultSet resultSet DatabaseConnector.getAllDoctors;
String csvFilePath "output.csv;
try CSVWriter writer new CSVWriternew FileWritercsvFilePath
writer.writeNextnew StringDoctorFirstName", "DoctorLastName", "Department";
while resultSetnext
String data
resultSet.getStringDoctorFirstName"
resultSet.getStringDoctorLastName"
resultSet.getStringDepartment
;
writer.writeNextdata;
JOptionPane.showMessageDialogMedicalAppUIthis, "Data exported to CSV successfully. File: csvFilePath;
catch SQLException IOException e
JOptionPane.showMessageDialogMedicalAppUIthis, "Error exporting data to CSV;
private void MedicalAppMain
this.dispose;
public static void mainString args
SwingUtilities.invokeLater new MedicalAppUI;
and this is mypackage MedicalLoginApp;
import java.sqlConnection;
import java.sqlDriverManager;
import java.sqlPreparedStatement;
import java.sqlResultSet;
import java.sqlSQLException;
import java.util.List;
public class DatabaseConnector
private static final String URL jdbc:mysql:localhost:Medicallogin;
private static final String USER "Username";
private static final String PASSWORD "Password";
public static Connection getConnection throws SQLException
return DriverManager.getConnectionURL USER, PASSWORD;
public static boolean authenticateString Username, String Password throws SQLException
String sql "SELECT FROM Users WHERE username AND password ;
try Connection connection getConnection;
PreparedStatement statement connection.prepareStatementsql
statement.setString Username;
statement.setString Password;
try ResultSet resultSet statement.executeQuery
return resultSet.next;
public static void insertDoctorsDataList data throws SQLException
String sql "INSERT INTO Doctors DoctorFirstName, DoctorLastName, Department VALUES ;
try Connection connection getConnection;
PreparedStatement preparedStatement connection.prepareStatementsql database connector code:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
