Answered step by step
Verified Expert Solution
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.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:
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
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started