This question already has answers here:
Connect Java to a MySQL database
(14 answers)
Closed 6 years ago.
I ve been working on a phonebook program on eclipse java
but and idk how to save information from a textfield to mysql and then be able use it in search, such as writing the name and finding all the info of the specific contact
I also want to save it to my sql
thats my code:
public class Phone {
private JFrame frame;
private JTextField firstfield;
private JTextField phonefield;
private JTextField emailfield;
private JTextField lastfield;
private JPanel MainScreen;
private JPanel newcontact;
private JPanel searchscreen;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Phone window = new Phone();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Phone() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new CardLayout(0, 0));
final JPanel MainScreen = new JPanel();
frame.getContentPane().add(MainScreen, "name_6058038854379");
MainScreen.setLayout(null);
MainScreen.setVisible(true);
final JPanel newcontact = new JPanel();
newcontact.setLayout(null);
frame.getContentPane().add(newcontact, "name_791613670731");
newcontact.setVisible(false);
final JPanel searchscreen = new JPanel();
frame.getContentPane().add(searchscreen, "name_6068807854350");
searchscreen.setVisible(false);
JButton newrecord = new JButton("New Record");
newrecord.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
newcontact.setVisible(true);
MainScreen.setVisible(false);
}
});
newrecord.setBounds(63, 99, 116, 52);
MainScreen.add(newrecord);
JButton Search = new JButton("Search");
Search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
searchscreen.setVisible(true);
newcontact.setVisible(false);
}
});
Search.setBounds(243, 99, 102, 52);
MainScreen.add(Search);
JLabel myphonebook = new JLabel(" My Phonebook");
myphonebook.setBounds(162, 44, 102, 14);
MainScreen.add(myphonebook);
JLabel first = new JLabel("First Name:");
first.setBounds(118, 83, 66, 14);
newcontact.add(first);
JLabel last = new JLabel("Last Name:");
last.setBounds(118, 108, 66, 14);
newcontact.add(last);
JLabel Phone = new JLabel("Phone:");
Phone.setBounds(118, 143, 66, 14);
newcontact.add(Phone);
JLabel Emailadress = new JLabel("Email Adress:");
Emailadress.setBounds(118, 180, 66, 14);
newcontact.add(Emailadress);
firstfield = new JTextField();
firstfield.setColumns(10);
firstfield.setBounds(220, 80, 86, 20);
newcontact.add(firstfield);
lastfield = new JTextField();
lastfield.setColumns(10);
lastfield.setBounds(220, 108, 86, 20);
newcontact.add(lastfield);
phonefield = new JTextField();
phonefield.setColumns(10);
phonefield.setBounds(220, 140, 86, 20);
newcontact.add(phonefield);
emailfield = new JTextField();
emailfield.setColumns(10);
emailfield.setBounds(220, 177, 86, 20);
newcontact.add(emailfield);
JLabel lblNewContact = new JLabel("New Contact");
lblNewContact.setBounds(166, 30, 86, 14);
newcontact.add(lblNewContact);
JButton Save = new JButton("Save");
Save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String firstname=firstfield.getText();
String lastname=lastfield.getText();
String phonenumber=phonefield.getText();
String emailadress=emailfield.getText();
}
});
Save.setBounds(81, 232, 89, 23);
newcontact.add(Save);
JButton cancel = new JButton("Cancel");
cancel.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newcontact.setVisible(false);
MainScreen.setVisible(true);
}
});
cancel.setBounds(283, 232, 89, 23);
newcontact.add(cancel);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1, "name_6081212161880");
}
}
Write this into your savecommand. You should only create a database an table and change the uppercased words.
try
{
// create a mysql database connection
String myDriver = "org.gjt.mm.mysql.Driver";
String myUrl = "jdbc:mysql://LOCATION/DATABASE";
Class.forName(myDriver);
Connection conn = DriverManager.getConnection(myUrl, "root", "");
// create a sql date object so we can use it in our INSERT statement
Calendar calendar = Calendar.getInstance();
java.sql.Date startDate = new java.sql.Date(calendar.getTime().getTime());
// the mysql insert statement
String query = " insert into TABLE (first_name, last_name, phone_number, email_adress)"
+ " values (?, ?, ?, ?)";
// create the mysql insert preparedstatement
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setString (1, firstname);
preparedStmt.setString (2, lastname);
preparedStmt.setString (3, phonenumber);
preparedStmt.setString (4, emailadress);
// execute the preparedstatement
preparedStmt.execute();
conn.close();
}
catch (Exception e)
{
System.err.println("Got an exception!");
System.err.println(e.getMessage());
}
}
Related
I Need to delete selected row from Jtable which connected to Mysql database
I successfully make another class to add book and it's work fine
i make this class to delete book added in the table , but i cannot find where the problem in my code , and i don't get any error
IDE : netBeans
here is the class code :
package finallibrary;
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import net.proteanit.sql.DbUtils;
import java.sql.*;
import java.awt.event.*;
import javax.swing.table.DefaultTableModel;
public class BookDetails extends JFrame implements ActionListener {
private JPanel contentPane;
private JTable table;
private JTextField search;
private JButton b1, b2, b3;
public static void main(String[] args) {
new BookDetails().setVisible(true);
}
public void Book() {
try {
Conn con = new Conn();
String sql = "select * from book";
PreparedStatement st = con.c.prepareStatement(sql);
ResultSet rs = st.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
rs.close();
st.close();
con.c.close();
} catch (Exception e) {
}
}
public BookDetails() {
setTitle("Book List");
setBounds(350, 200, 890, 475);
contentPane = new JPanel();
contentPane.setBackground(Color.WHITE);
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(36, 133, 802, 268);
contentPane.add(scrollPane);
table = new JTable();
table.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
int row = table.getSelectedRow();
search.setText(table.getModel().getValueAt(row, 1).toString());
}
});
table.setBackground(new Color(240, 248, 255));
table.setForeground(Color.DARK_GRAY);
table.setFont(new Font("Trebuchet MS", Font.BOLD, 16));
scrollPane.setViewportView(table);
JButton b1 = new JButton("Search");
b1.addActionListener(this);
b1.setFont(new Font("Trebuchet MS", Font.BOLD, 18));
b1.setBounds(564, 89, 108, 33);
contentPane.add(b1);
JButton b2 = new JButton("Delete");
b2.addActionListener(this);
b2.setFont(new Font("Trebuchet MS", Font.BOLD, 18));
b2.setBounds(712, 89, 108, 33);
contentPane.add(b2);
JLabel l1 = new JLabel("Book Details");
l1.setForeground(SystemColor.desktop);
l1.setFont(new Font("Bookman Old Style", Font.PLAIN, 30));
l1.setBounds(302, 11, 400, 47);
contentPane.add(l1);
search = new JTextField();
search.setForeground(new Color(47, 79, 79));
search.setFont(new Font("Book Antiqua", Font.PLAIN, 17));
search.setBounds(189, 89, 357, 33);
contentPane.add(search);
search.setColumns(10);
JButton b3 = new JButton("Back");
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Home home = new Home();
home.setVisible(true);
}
});
b3.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
setVisible(false);
Home home = new Home();
home.setVisible(true);
}
});
b3.setFont(new Font("Trebuchet MS", Font.BOLD, 18));
b3.setBounds(36, 89, 103, 33);
contentPane.add(b3);
JPanel panel = new JPanel();
panel.setBorder(new TitledBorder(new LineBorder(new Color(0, 128, 128), 3, true), "Book-Details",
TitledBorder.LEADING, TitledBorder.TOP, null, new Color(0, 128, 0)));
panel.setBounds(10, 52, 849, 373);
contentPane.add(panel);
panel.setBackground(Color.WHITE);
Book();
}
public void actionPerformed(ActionEvent ae) {
try {
Conn con = new Conn();
if (ae.getSource() == b1) {
String sql = "select * from book where concat(name, book_id) like ?";
PreparedStatement st = con.c.prepareStatement(sql);
st.setString(1, "%" + search.getText() + "%");
ResultSet rs = st.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(rs));
rs.close();
st.close();
}
if (ae.getSource() == b2) {
String sql = "delete from book where name = '" + search.getText() + "'";
PreparedStatement st = con.c.prepareStatement(sql);
JDialog.setDefaultLookAndFeelDecorated(true);
int response = JOptionPane.showConfirmDialog(null, "Do you want to continue?", "Confirm",
JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
if (response == JOptionPane.NO_OPTION) {
} else if (response == JOptionPane.YES_OPTION) {
int rs = st.executeUpdate();
JOptionPane.showMessageDialog(null, "Deleted !!");
} else if (response == JOptionPane.CLOSED_OPTION) {
}
st.close();
}
con.c.close();
} catch (Exception e) {
}
}
}
In my project I've assembled a basic password book, the project is designed to send user information over to a mySQL database after the "Save Information" button has been clicked. It works fine when the String values are hardcoded, but when I try to use any kind of String variable or the toString() method I start getting errors. Does anyone know the correct way to send UN-hardcoded values to mySQL?
import javax.swing.*;
//import java.io.*;
//import java.lang.Thread.State;
import java.awt.event.*;
import java.sql.*;
public class PasswordBook implements ActionListener
{
private static JLabel websiteLabel;
private static JTextField websiteText;
private static JLabel userLabel;
private static JTextField userText;
private static JLabel passLabel;
private static JTextField passText;
private static JLabel emailLabel;
private static JTextField emailText;
private static JButton button;
private static JButton clearButton;
private static JLabel success;
//private static String websiteToString; (values I tried)
//private static String userToString;
//private static String emailToString;
//private static String passToString;
public static void main (String[]args)
{
JFrame frame = new JFrame();
JPanel panel = new JPanel();
frame.setSize(400,350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
panel.setLayout(null); // rows columns
websiteLabel = new JLabel("Website"); //1st
websiteLabel.setBounds(10, 20, 80, 25);
panel.add(websiteLabel);
websiteText = new JTextField(); //1st
websiteText.setBounds(100, 20, 165, 25);
panel.add(websiteText);
//websiteToString = websiteText.toString();
userLabel = new JLabel("Username"); //2nd
userLabel.setBounds(10, 60, 80 ,25);
panel.add(userLabel);
userText = new JTextField(20); // 2nd
userText.setBounds(100, 60, 165, 25);
panel.add(userText);
//userToString = userText.toString();
emailLabel = new JLabel("Email"); //3rd
emailLabel.setBounds(10, 100, 80 ,25);
panel.add(emailLabel);
emailText = new JTextField(); //2nd
emailText.setBounds(100, 100, 165, 25);
panel.add(emailText);
//emailToString = emailText.toString();
passLabel = new JLabel("Password"); //4th
passLabel.setBounds(10, 140, 80, 25);
panel.add(passLabel);
passText = new JTextField(); // 4th
passText.setBounds(100, 140, 165, 25);
panel.add(passText);
//passToString = passText.toString();
button = new JButton("Save Information");
button.setBounds(10, 180, 150 ,25);
button.addActionListener(new PasswordBook()); // action listener to button
panel.add(button);
clearButton = new JButton("Clear");
clearButton.setBounds(180, 180, 150, 25);
// CLEARS THE TEXT FIELDS WHEN PRESSED
clearButton.addActionListener(new ActionListener() {
#Override // Override allows function to perfrom independently of the parent class
public void actionPerformed(ActionEvent event)
{
websiteText.setText("");
userText.setText("");
emailText.setText("");
passText.setText("");
success.setText("");
}
});
panel.add(clearButton);
success = new JLabel("");
success.setBounds(10, 220, 300, 25);
panel.add(success);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent event) {
try {
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/passwordbook", "root", "password");
//Statement myStatement = myConn.createStatement();
String sq1 = "INSERT into website_and_user_info"
+ " (Website, Username, Email, Password)"
+ " values (?, ?, ?, ?)";
PreparedStatement statement = myConn.prepareStatement(sq1);
statement.setString(1, websiteText.toString()); // These values work when hardcoded
statement.setString(2, userText.toString());
statement.setString(3, emailText.toString());
statement.setString(4, passText.toString());
statement.executeUpdate();
System.out.println("insert complete");
} catch (Exception e) {
e.printStackTrace();
}
}
}
If I read it right, passText is a Textfild (advice use Hungarian notation, makes code easier to read) to get the text you need .gettext(). Like prepared preparedStatement.setString(int, passText.getText()); that should do the trick. But you need to get text before clearing the fields. You Code seems to first clear and you use .setTxt in the statement. If iam wrong I'm sry, atm.i just have my phone to browse.
I am new to java and I am working on Jtable with database.i fill up my jtable by data from database ,now I need to use paging and I don't know how to implement this in my code,i used DbUtils to fill up the jtable,every think is ok but the problem is that i don't know how to use pagination, thank you in advance for any sharing of information and help
public class FenetrePers extends JFrame {
private JTextField rechNomField;
private JTextField rechPrenField;
private JTextField rechemailField;
private JTextField rechtelephField;
private JButton btnNewButton_2;
AjoutPers ajpers;
Connection cnx =null;
PreparedStatement prepared=null;
ResultSet resultat=null;
private JScrollPane scrollPane;
private JTable table;
private JButton editButton;
private JButton deleteButton;
public FenetrePers() {
cnx=ConnectionSql.ConnexionDB();
this.setExtendedState(this.MAXIMIZED_BOTH);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
getContentPane().setLayout(null);
JLabel lblNewLabel = new JLabel(" Gestion du personnel");
lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 14));
lblNewLabel.setForeground(Color.BLACK);
lblNewLabel.setBounds(10, 23, 181, 21);
getContentPane().add(lblNewLabel);
rechNomField = new JTextField();
rechNomField.setBounds(27, 84, 175, 32);
getContentPane().add(rechNomField);
rechNomField.setColumns(10);
rechPrenField = new JTextField();
rechPrenField.setBounds(298, 84, 163, 32);
getContentPane().add(rechPrenField);
rechPrenField.setColumns(10);
rechemailField = new JTextField();
rechemailField.setBounds(587, 84, 163, 32);
getContentPane().add(rechemailField);
rechemailField.setColumns(10);
rechtelephField = new JTextField();
rechtelephField.setBounds(844, 84, 157, 32);
getContentPane().add(rechtelephField);
rechtelephField.setColumns(10);
JButton FindButton = new JButton("find");
FindButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
UpdateTableSearch();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
FindButton.setBounds(1034, 89, 66, 27);
getContentPane().add(FindButton);
JButton btnNewButton_1 = new JButton("reset");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String nomS=rechNomField.getText().toString();
String prenomS=rechPrenField.getText().toString();
String emailS=rechemailField.getText().toString();
String telephS=rechtelephField.getText().toString();
if(!nomS.equals("")|| !prenomS.equals("")|| !emailS.equals("")||!telephS.equals("")) {
rechNomField.setText("");
rechPrenField.setText("");
rechemailField.setText("");
rechtelephField.setText("");
UpdateTable();
}}
});
btnNewButton_1.setBounds(1110, 89, 66, 27);
getContentPane().add(btnNewButton_1);
btnNewButton_2 = new JButton("Ajouter personnel");
btnNewButton_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ajpers = new AjoutPers(FenetrePers.this);
UpdateTable();
}
});
btnNewButton_2.setBounds(1034, 24, 142, 32);
getContentPane().add(btnNewButton_2);
scrollPane = new JScrollPane();
scrollPane.setBounds(27, 150, 985, 308);
getContentPane().add(scrollPane);
table = new JTable();
table.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
}
});
scrollPane.setViewportView(table);
I am not able to load JFrame for multiple clients whereas it works fine for a single client. I am able to perform all the operations on a single client. The frame becomes visible but the contents are not.
Do I need to add something extra? Am I making the correct connections?I am running it on my local machine.
Can I run multiple instances on the same machine?
Server :
public class ServerThread extends Thread{
public static void main(String[] args) throws IOException {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ServerSocket serverSocket = new ServerSocket(4518);
while(true){
Socket clientSocket = serverSocket.accept();
Thread window=new Frame1();
window.start();
}
} catch (Exception e) {
e.printStackTrace();
}
Client:
public class Client extends JFrame{
public static void main(String arg[]){
String hostName="localhost";
int portNumber=4518;
try{
Socket echoSocket = new Socket(hostName, portNumber);
PrintWriter out =
new PrintWriter(echoSocket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(echoSocket.getInputStream()));
}
JFrame:
public class Frame1 extends Thread{
static JFrame frame;
private static JTextField textField;
private static JPasswordField passwordField;
static Connection conn = null;
static PreparedStatement pst = null;
static ResultSet rs = null;
public Frame1() {
System.out.println("hello");
initialize();
}
private static void initialize() {
frame = new JFrame();
frame.setVisible(true);
frame.setTitle("Course Registration- Spring 2017");
frame.getContentPane().setBackground(new Color(204, 204, 255));
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Sign in");
btnNewButton.setBackground(new Color(204, 102, 153));
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
conn = DBConnect.DBConnect();
String sql = "Select * from users where username =? and password=?";
try{
String userName=textField.getText();
String password=passwordField.getText();
pst = conn.prepareStatement(sql);
pst.setString(1, userName);
pst.setString(2, password);
rs = pst.executeQuery();
if(userName.equals("admin") && password.equals("admin")){
ServerSideViews s = new ServerSideViews();
s.setVisible(true);
s.setUserName(userName);
s.setFrameLogin(frame);
System.out.println(userName);
frame.setVisible(false);
}
else if(rs.next() && !(userName.equals("admin"))){
JOptionPane.showMessageDialog(null, "Welcome " + textField.getText() );
CourseRegistration c = new CourseRegistration();
c.setVisible(true);
c.setFrameLogin(frame);
frame.setVisible(false);
String sql1 = "Select major from users where username=?;";
System.out.println(sql1);
pst = conn.prepareStatement(sql1);
pst.setString(1, userName);
rs = pst.executeQuery();
String major="";
while(rs.next())
{
major=rs.getString("major");
}
c.setUserName(userName);
c.comboBox.setSelectedItem(major);
c.comboBox.disable();
}
else{
JOptionPane.showMessageDialog(null, "Invalid user name or password");
}
}catch(Exception e1){
e1.printStackTrace();
}
}
});
btnNewButton.setBounds(157, 182, 117, 29);
frame.getContentPane().add(btnNewButton);
JLabel lblUserName = new JLabel("User Name");
lblUserName.setBounds(76, 96, 86, 16);
frame.getContentPane().add(lblUserName);
JLabel lblPassword = new JLabel("Password");
lblPassword.setBounds(86, 129, 61, 16);
frame.getContentPane().add(lblPassword);
textField = new JTextField();
textField.setBounds(157, 91, 130, 26);
frame.getContentPane().add(textField);
textField.setColumns(10);
JButton btnNewButton_1 = new JButton("Register");
btnNewButton_1.setBackground(new Color(51, 102, 204));
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UserRegistration reg = new UserRegistration();
reg.setVisible(true);
reg.setFrameLogin(frame);
frame.setVisible(false);
}
});
btnNewButton_1.setBounds(157, 225, 117, 29);
frame.getContentPane().add(btnNewButton_1);
passwordField = new JPasswordField();
passwordField.setBounds(157, 124, 130, 26);
frame.getContentPane().add(passwordField);
JLabel lblNewLabel = new JLabel("Course Registration - Spring 2017");
lblNewLabel.setBounds(109, 24, 241, 16);
frame.getContentPane().add(lblNewLabel);
}
}
Don't start a task that blocks on the Event Dispatch Thread (EDT). The GUI won't be able to respond to events.
So don't start the ServerSocket from the SwingUtilities.invokeLater(..).
Instead you need to use a separate Thread for the ServerSocket.
Read the section from the Swing tutorial on Concurrency for more information and examples.
I am trying to create a register system for my MySQL database. I have a Java form that I created, in which the user inserts a username, password, and their email. I take that infromation and store it in a variable accordingly. The MySQL connection code works, and it does insert a new row, but the information is blank on the MySQL page. Am I doing someting wrong?
public class Main extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
private JTextField textField;
private JPasswordField passwordField;
private static Connection con;
private static PreparedStatement st;
private static int rs;
String username;
String password;
String email;
private JTextField textField_1;
String insertTableSQL = "INSERT INTO `users`" + "(username, password, email) VALUES" + "(?,?,?)";
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Main frame = new Main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Main() {
setResizable(false);
setTitle("Barrage : Login / Register");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 350, 200);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblNewLabel = new JLabel("Username: ");
lblNewLabel.setBounds(60, 25, 68, 20);
contentPane.add(lblNewLabel);
JLabel lblPassword = new JLabel("Password: ");
lblPassword.setBounds(60, 59, 68, 20);
contentPane.add(lblPassword);
textField = new JTextField();
username = textField.getText();
textField.setBounds(138, 22, 120, 26);
contentPane.add(textField);
textField.setColumns(10);
passwordField = new JPasswordField();
password = passwordField.getText();
passwordField.setBounds(138, 56, 120, 26);
contentPane.add(passwordField);
JButton btnRegister = new JButton("Register");
btnRegister.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/barrage", "root", "");
st = con.prepareStatement(insertTableSQL);
st.setString(1, username);
st.setString(2, password);
st.setString(3, email);
st.executeUpdate();
} catch(Exception e) {
e.printStackTrace();
}
}
});
btnRegister.setBounds(119, 138, 89, 23);
contentPane.add(btnRegister);
JLabel lblEmail = new JLabel("E-mail: ");
lblEmail.setBounds(60, 96, 68, 20);
contentPane.add(lblEmail);
textField_1 = new JTextField();
email = textField_1.getText();
textField_1.setBounds(138, 93, 120, 26);
contentPane.add(textField_1);
textField_1.setColumns(10);
}
}
Don't do it this way. This is a security vulnerability called a SQL Injection. Instead use a PreparedStatement with variable binding (eg: ?). Here is a tutorial.
As for why it's blank in the database (which is what I think you're saying), you're going to have to prove that the value of those variables are not blank with traditional debugging. IMO, if your tables were set up with the correct constraints (eg: not null) it wouldn't even let you insert in that invalid state in the first place.