saving and retrieving a text file in java?
- by user3319432
import java.sql. ;
import java.awt.;
import javax.swing.;
import java.awt.event.;
public class saving extends JFrame implements ActionListener{
JTextField edpno=new JTextField(10);
JLabel l0= new JLabel ("EDP Number: ");
JComboBox fname = new JComboBox();
JLabel l1= new JLabel("First Name: ");
JTextField lname= new JTextField(20);
JLabel l2= new JLabel("Last Name: ");
// JTextField contno= new JTextField(20);
// JLabel l3= new JLabel("Contact Number: ");
JComboBox contno = new JComboBox();
JLabel l3 = new JLabel ("Contact Number: ");
JButton bOK = new JButton("Save");
JButton bRetrieve = new JButton("Retrieve");
private ImageIcon icon;
JPanel C=new JPanel(){
protected void paintComponent(Graphics g){
g.drawImage(icon.getImage(),0,0,null);
super.paintComponent(g);
}
};
public Search Record (){
icon=new ImageIcon("images/canres.png");
C.setOpaque(false);
C.setLayout(new GridLayout(5,2,4,4));
setTitle("Search Record");
C.add (l0);
C.add (edpno);
edpno.addActionListener(this);
C.add (l1);
C.add (fname);
fname.setForeground(Color.BLUE);
fname.setFont(new Font(" ", Font.BOLD,15));
C.add (l2);
C.add (lname);
C.add (l3);
C.add (contno);
contno.setForeground(Color.BLUE);
contno.setFont(new Font(" ", Font.BOLD,15));
C.add(bOK);
bOK.addActionListener(this);
C.add (bRetrieve);
bRetrieve.addActionListener(this);
bOK.setBackground(Color.white);
bRetrieve.setBackground(Color.white);
}
public void saverecord(){
try{
//Connect to the Database
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String path ="jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=Database/roomassign.mdb";
String DBPassword ="";
String DBUserName ="";
Connection con = DriverManager.getConnection(path,"","");
Statement s = con.createStatement();
s.executeQuery("select firstname, Lastname, contact number from name WHERE edpno ='"+edpno.getText()+"'");
ResultSet rs = s.getResultSet();
ResultSetMetaData md = rs.getMetaData();
while(rs.next())
{
fname.setSelectedItem(rs.getString(1));
lname.setText(rs.getString(2));
contno.setSelectedItem(rs.getString(3));
// crs.setSelectedItem(rs.getString(4));
}
s.close();
con.close();
}
catch(Exception Q)
{
JOptionPane.showMessageDialog(this,Q);
}
}
public void SaveRecord(){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String path = "jdbc:odbc:;DRIVER=Microsoft Access Driver (*.mdb);DBQ=Database/roomassign.mdb";
String DBPassword ="";
String DBUsername ="";
Connection con = DriverManager.getConnection(path,"","");
Statement s = con.createStatement();
String sql = "UPDATE rooms SET Firstname='"+fname.getSelectedItem()+"',Lastname='"+lname.getText()+"',Contactnumber='"+contno.getSelectedItem()+"' WHERE '"+edpno.getText()+"'=edpno";
s.executeUpdate(sql);
JOptionPane.showMessageDialog(this,"New room Record has been successfully saved");
dispose();
s.close();
con.close();
}
catch(Exception Mismatch){
JOptionPane.showMessageDialog(this,Mismatch);
}
}
public void actionPerformed (ActionEvent ako){
if (ako.getSource() == bRetrieve){
dispose();
}
else if (ako.getSource() == bOK){
SaveRecord();
}
}
public static void main (String [] awtsave){
new Search();
}
}