Results 1 to 3 of 3
  1. #1
    maatata is offline Member
    Join Date
    Mar 2011
    Posts
    1
    Rep Power
    0

    Default change the database to text file

    hi...I have this code that use mysql database to save data....
    it's about editing and adding employee information....

    I want to change it to use a file like text file to save data and doesn't use database

    who can help me to change this program....


    Thank you very much....

    Java Code:
    import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.sql.*;
    import java.util.*;
    
    class EmployeeInformation {
    JFrame f;
    JPanel p1,p2,p3;
    JTabbedPane tp;
    ImageIcon btnimg1,btnimg2;
    JLabel l1, l2, l3, l4,l5,l6,l7,l8,l9,l10;
    JTextField tf1,tf2,tf3,tf4,tf5,tf6,tf7,tf8,tf9,tf10;
    JScrollPane sp1;
    JButton savebtn,resetbtn,editbtn1,editbtn2,deletebtn ;
    
    EmployeeInformation(){
    f=new JFrame("Form");
    p1=new JPanel(new GridLayout(5,2));
    p2=new JPanel(new GridLayout(5,2));
    p3=new JPanel(new GridLayout(2,2));
    tp=new JTabbedPane();
    l1=new JLabel("Employee ID:");
    l2=new JLabel("Employee Name:");
    l3=new JLabel("Employee Address:");
    l4=new JLabel("Salary:");
    l5=new JLabel("Enter Employee ID to delete record:");
    
    l7=new JLabel("Employee ID:");
    l8=new JLabel("Employee Name:");
    l9=new JLabel("Employee Address:");
    l10=new JLabel("Salary:");
    tf1=new JTextField(12);
    tf2=new JTextField(12);
    tf3=new JTextField(12);
    tf4=new JTextField(12);
    tf5=new JTextField(12);
    tf6=new JTextField(12);
    tf7=new JTextField(12);
    tf8=new JTextField(12);
    tf9=new JTextField(12);
    tf10=new JTextField(12);
    savebtn=new JButton(" Add ");
    resetbtn=new JButton(" Reset");
    editbtn1=new JButton(" Edit ");
    editbtn2=new JButton(" Save");
    deletebtn=new JButton("Delete");
    p1.add(l1);
    p1.add(tf1);
    p1.add(l2);
    p1.add(tf2);
    p1.add(l3);
    p1.add(tf3);
    p1.add(l4);
    p1.add(tf4);
    p1.add(savebtn);
    p1.add(resetbtn);
    
    p2.add(l7);
    p2.add(tf7);
    p2.add(l8);
    p2.add(tf8);
    p2.add(l9);
    p2.add(tf9);
    p2.add(l10);
    p2.add(tf10);
    p2.add(editbtn1);
    p2.add(editbtn2);
    
    p3.add(l5);
    p3.add(tf5);
    p3.add(deletebtn);
    resetbtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae){
    tf1.setText("");
    tf2.setText("");
    tf3.setText("");
    tf4.setText("");
    }
    });
    savebtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae){
    String value1=tf1.getText();
    String value2=tf2.getText();
    String value3=tf3.getText();
    String value4=tf4.getText();
    Connection con = null;
    String url = "jdbc:mysql://localhost:3306/";
    String db = "test";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String pass = "root";
    System.out.println(value1+value2+value3+value4);
    try{
    Class.forName(driver);
    con = DriverManager.getConnection(url+db, user, pass);
    PreparedStatement st=con.prepareStatement("insert into employee(emp_id,emp_name,emp_address,salary) values(?,?,?,?)");
    st.setString(1,value1);
    st.setString(2,value2);
    st.setString(3,value3);
    st.setString(4,value4);
    st.executeUpdate();
    JOptionPane.showMessageDialog(p1,"Data is successfully inserted into database.");
    con.close();
    }
    catch(Exception e){
    JOptionPane.showMessageDialog(p1,"Error in submitting data!");
    }
    }
    });
    
    deletebtn.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae){
    
    String value1=tf5.getText();
    Connection con = null;
    		 String url = "jdbc:mysql://localhost:3306/";
    		 String db = "test";
    		 String driver = "com.mysql.jdbc.Driver";
    		 String user = "root";
    		 String pass = "root";
    try{
    Class.forName(driver);
    con = DriverManager.getConnection(url+db, user, pass);
    PreparedStatement st=con.prepareStatement("DELETE FROM employee WHERE emp_id = ?");
    st.setString(1,value1);
    st.executeUpdate();
    JOptionPane.showMessageDialog(p3,"Record is deleted successfully.");
    con.close();
    }
    catch(Exception exp3)
    {
    JOptionPane.showMessageDialog(p3,"Error in deleting record.");
    }
    }
    });
    editbtn1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae){
    
    String value=tf7.getText();
    Connection con = null;
    		 String url = "jdbc:mysql://localhost:3306/";
    		 String db = "test";
    		 String driver = "com.mysql.jdbc.Driver";
    		 String user = "root";
    		 String pass = "root";
    try{
    Class.forName(driver);
    con = DriverManager.getConnection(url+db, user, pass);
    PreparedStatement st=con.prepareStatement("select * from employee where emp_id=?");
    st.setString(1,value);
    ResultSet res=st.executeQuery();
    res.next();
    tf7.setText(Integer.toString(res.getInt(1)));
    tf8.setText(res.getString(2));
    tf9.setText(res.getString(3));
    tf10.setText(Integer.toString(res.getInt(4)));
    con.close();
    }
    catch(Exception e)
    {
    JOptionPane.showMessageDialog(p2,"Can not edit data");
    }
    }
    });
    editbtn2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ae){
    Connection con = null;
    		 String url = "jdbc:mysql://localhost:3306/";
    		 String db = "test";
    		 String driver = "com.mysql.jdbc.Driver";
    		 String user = "root";
    		 String pass = "root";
    try
    {
    int x=JOptionPane.showConfirmDialog(p2,"Confirm edit? All data will be replaced");
    if(x==0){
    try{
    String value1=tf7.getText();
    String value2=tf8.getText();
    String value3=tf9.getText();
    String value4=tf10.getText();
    
    Class.forName(driver);
    con = DriverManager.getConnection(url+db, user, pass);;
    Statement st=con.createStatement();
    st.executeUpdate("update employee set emp_name='"+value2+"', emp_address='"+value3+"', salary='"+value4+"' where emp_id='"+value1+"'");
    JOptionPane.showMessageDialog(p2,"Updated successfully");
    con.close();
    }
    catch(Exception ex)
    {
    JOptionPane.showMessageDialog(p2,"Error in updating edit fields");
    }
    }
    }
    catch(Exception ex)
    {
    JOptionPane.showMessageDialog(p2,"Error");
    }
    }
    });
    }
    void dis()
    {
    f.getContentPane().add(tp);
    tp.addTab("Add Record",p1);
    tp.addTab("Edit Record",p2);
    tp.addTab("Delete Record",p3);
    
    f.setSize(350,180);
    f.setVisible(true);
    f.setResizable(true);
    }
    public static void main(String z[]){
    EmployeeInformation pro=new EmployeeInformation();
    pro.dis();
    }
    }

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    i think first you would need to separate the GUI component from the employee model bean. So create an employee data type, And design an interface for the basic operations, to manipulate this. Then have a mysql implementation implement the interface and do what it does now.
    Java Code:
    public class Employee
      String name;
      String address;
      Double salary;
      // getter and setter methods
    }
    
    public interface EmployeeManager {
      void SaveEmployee(Employee anEmployee);
      void deleteEmployee(Employee anEmployee);
    }
    Then we just have to write the implementation of this manager interface that will read/write to a text file.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    This ^^.

    Essentially that code needs to be ripped apart and split into a model, a view, and a db access layer.

    Oh, and when you do that ensure you format the code correctly...that is unreadable!

Similar Threads

  1. Replies: 3
    Last Post: 06-08-2010, 09:10 PM
  2. change syntax text color
    By dejos456 in forum New To Java
    Replies: 5
    Last Post: 12-14-2009, 12:09 PM
  3. change Text Colore
    By Mir in forum New To Java
    Replies: 39
    Last Post: 03-26-2009, 05:44 PM
  4. How to Change MySQL Database Location
    By sayan751 in forum JDBC
    Replies: 3
    Last Post: 03-19-2009, 03:42 PM
  5. How can I change text for paginate button?
    By robot in forum NetBeans
    Replies: 0
    Last Post: 11-20-2008, 07:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •