hello good people,
i am trying to make a small program that takes input from 4 text fields and adds them to the database(i am using mysql through wamp server) and some other database operations. my Exit and Clear buttons are working. I am using Swing class but have done the coding for components myself(haven't used drag and drop).
PORBLEM: The problem i am having is that when i insert the data and click on save button, the data is not inserted in the mysql database. the code seems to be right. i can't figure out the problem because it is NOT generating any exceptions.
this is what i gotta do:
Develop a GUI based address book.
A database should be used at the back end for insertion or retrieval of person’s
records.
You can make a selection of any specific database in accordance with your own
convenience.
ajor Functionalities: Save Button:
Should save the data entered by the user in the database.
Delete Button: Search Button:
The user should be able to search the records based on Name, id & Email or any
combination of these parameters. >> Button:
Forward navigation << Button:
Backward navigation Update Button:
Enables the user to make changes within any existing record. Clear Button:
Enables the user to clear/reset the fields of the address book, in case the user wants to
enter a new record.
Exit Button:
Enables the user to close the program
Hints:
Make effective use of Scrollable & updatable ResultSet.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package projectactivity1;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
/**
*
* @author Sana
*/
class test extends JFrame implements ActionListener{
static Connection link;
static Statement s;
static ResultSet results;
JTextField t1, t2, t3, t4;
test(){
//code for frame
String msg = "";
setSize(470,400);
setBackground(Color.LIGHT_GRAY);
setVisible( true );
setTitle("My Address Book");
System.out.println(getSize());
setLayout(null);
//code for first row(name)
JLabel name=new JLabel("Name");
add(name);
name.setBounds(50, 50, 70, 10);
JTextField t1=new JTextField("");
add(t1);
t1.setBounds(150, 50, 250, 22);
//code for 2 row(address)
JLabel address=new JLabel("Address");
add(address);
address.setBounds(50, 80, 70, 10);
t2=new JTextField("");
add(t2);
t2.setBounds(150, 80, 250, 25);
//code for 3 row (phone)
JLabel phone=new JLabel("Phone");
add(phone);
phone.setBounds(50, 110, 70, 10);
t3=new JTextField("");
add(t3);
t3.setBounds(150, 110, 250, 25);
//code for 4 row (email)
Label email=new Label("E_mail");
add(email);
email.setBounds(50, 140, 70, 30);
t4=new JTextField("");
add(t4);
t4.setBounds(150, 140, 250, 25);
//code for save JButton
JButton save=new JButton("Save");
add(save);
save.setBackground(Color.LIGHT_GRAY);
save.setForeground(Color.BLACK);
save.setBounds(30, 200, 80, 30);
save.addActionListener(this);
//code for del JButton
JButton del=new JButton("Delete");
add(del);
del.setBackground(Color.LIGHT_GRAY);
del.setForeground(Color.BLACK);
del.setBounds(130, 200, 80, 30);
del.addActionListener(this);
//code for clear JButton
JButton clr=new JButton("Clear");
add(clr);
clr.setBackground(Color.LIGHT_GRAY);
clr.setForeground(Color.BLACK);
clr.setBounds(230, 200, 80, 30);
clr.addActionListener(this);
//code for update JButton
JButton updat=new JButton("Update");
add(updat);
updat.setBackground(Color.LIGHT_GRAY);
updat.setForeground(Color.BLACK);
updat.setBounds(330, 200, 80, 30);
updat.addActionListener(this);
//code for seaarch JButton
JButton sr=new JButton("Search");
add(sr);
sr.setBackground(Color.LIGHT_GRAY);
sr.setForeground(Color.BLACK);
sr.setBounds(30, 250, 80, 30);
sr.addActionListener(this);
//code for forward JButton
JButton fr=new JButton(">>");
add(fr);
fr.setBackground(Color.LIGHT_GRAY);
fr.setForeground(Color.BLACK);
fr.setBounds(130, 250, 80, 30);
fr.addActionListener(this);
//code for back JButton
JButton bk=new JButton("<<");
add(bk);
bk.setBackground(Color.LIGHT_GRAY);
bk.setForeground(Color.BLACK);
bk.setBounds(230, 250, 80, 30);
bk.addActionListener(this);
//code for Exit JButton
JButton exit=new JButton("Exit");
add(exit);
exit.setBackground(Color.LIGHT_GRAY);
exit.setForeground(Color.BLACK);
exit.setBounds(330, 250, 80, 30);
exit.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
String str = ae.getActionCommand();
if(str.equals("save")) {
try {
Class.forName("com.mysql.jdbc.Driver"); //Step 1.
link = DriverManager.getConnection("jdbc:mysql://localhost:3306/address_book","root","sunny786"); //Step 2.
s = link.createStatement();
String sql_query = "INSERT INTO addbook VALUES ('"+t1.getText()+"','"+t2.getText()+"','"+t3.getText()+"','"+t4.getText()+"')";
//this.actionPerformed(save);
s.executeUpdate(sql_query);
//if (sql_query == 0)
//System.out.println("\nUnable to insert record!");
link.close();
}
catch(Exception e){
e.printStackTrace();
}
}
else if (str.equals("del")){
//code for delete here
}
else if(str.equals("clr")) {
// code for clear
}
else if(str.equals("updat")) {
// code for update
}
else if(str.equals("sr")) {
// code for search
}
else if(str.equals("bk")) {
// code for back
}
else if(str.equals("fr")) {
// code for fwd
}
else if(str.equals("exit")) {
System.exit(0);
}
}
}
public class ProjectActivity1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
test t1 = new test();
}
}
Any kind of help is greatly appreciated.
Thank you! :)