Skip to main content

inserting into ms excell through jtextfield

2 replies [Last post]
salmang
Offline
Joined: 2012-07-12
Points: 0

please help. the following code below display an interface through which an excell database can be populated with data as well as view inserted data. the view data or data selection procedure work fine, but that of data insertion failed. I will very much appreciate your help if you can make the data insertion work.
Find the code below

package excelldata;

import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.awt.*;
class ExcellData extends JFrame {
JLabel idlab,flab,slab,sal;
JTextField id,ft,st,sst;
JButton sub,ok;

public static void main(String[] args) {
ExcellData jf=new ExcellData();
}
public ExcellData(){
getContentPane().setBackground(Color.PINK);
idlab=new JLabel("ID Number");
flab=new JLabel("First Name");
slab=new JLabel("Sure Name");
sal=new JLabel("Salary");
id=new JTextField(5);
ft=new JTextField(10);
st=new JTextField(10);
sst=new JTextField(10);
sub=new JButton("Submit");
ok=new JButton("View Data");
setLayout(new FlowLayout());
setTitle("EXCELL FILE");
setSize(220,220);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel set =new JPanel();
set.add(idlab);
set.add(id);
add("West",set);
JPanel bet=new JPanel();
bet.add(flab);
bet.add(ft);
add("West",bet);
JPanel det=new JPanel();
det.add(slab);
det.add(st);
add("West",det);
JPanel met=new JPanel();
met.add(sal);
met.add(sst);
add("West",met);
JPanel net=new JPanel();
net.add(sub);
net.add(ok);
add("Center",net);
setVisible(true);
sub.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String val1=id.getText();
String val2=st.getText();
String val3=ft.getText();
String val4=sst.getText();
String url="jdbc:odbc:musa";
try{
Connection con=DriverManager.getConnection(url);
PreparedStatement pt=con.prepareStatement("INSERT INTO [Sheet1$](id,lastname,firstname,othername)values(?,?,?,?)");
pt.setString(1, val1);
pt.setString(2, val2);
pt.setString(3, val3);
pt.setString(4, val4);
pt.executeUpdate();
JOptionPane.showMessageDialog(null,"Data Inserted");
con.close();
}
catch(Exception err){
JOptionPane.showMessageDialog(null, "Error in submitting Data");
}
}

});
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
String value=id.getText();
String url="jdbc:odbc:musa";
try{
Connection con=DriverManager.getConnection(url);
PreparedStatement pt=con.prepareStatement("SELECT * FROM [Sheet1$] WHERE id=?");
pt.setString(1, value);
ResultSet rs=pt.executeQuery();
rs.next();
id.setText(rs.getString(1));
st.setText(rs.getString(2));
ft.setText(rs.getString(3));
sst.setText(rs.getString(4));
con.close();
}
catch(Exception err){
JOptionPane.showMessageDialog(null, "Record not found.\n Make sure database is running");
}
}
}
);
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jamesharte
Offline
Joined: 2012-09-28
Points: 0

What is the error you are getting and where in the code are you getting it?

mozum
Offline
Joined: 2012-05-01
Points: 0

A big thank you for always been there for me. Infact your invaluable assistance has remove so much grey areas. I was able to solve the problem but the jcombo issue is still sending. Once again thanks alot