The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
October 2001

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

need further info

Posted by shashi on October 30, 2001 at 3:54 PM

Thank u Matt
But u knwo the problem is i want the user to enter data in the front end n update the DB. plz let me know how to do.
here is what i tried but could'nt do


import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import javax.swing.*;
import javax.swing.border.*;


public class customerformentrysubmit extends CJFrame implements ActionListener
{
Container c;
JPanel dataPanel, controlPanel, statusPanel;
JTextField status,CustomerNumber, CustomerName, CustomerAddress1,CustomerAddress2,CustomerCity,CustomerState,CustomerZipCode,CustomerTelephone,CustomerCreditLimit,CustomerPrimaryContact;
JLabel Cnumber,Cname,Caddress1,Caddress2, City,State,Zip,Telephone,Climit,Contact;
JButton first, prev, next, last, quit,update;

String[][] row; //gets data in the form of rows and column from JDBC
int k, i;

public customerformentrysubmit()
{

String db = "jdbc:odbc:shashi"; //this part is JDBC interface to get data
String user = ""; //from table
String pw = "";
Connection con,con1;
Statement st,st1;
ResultSet rs,rs1;

row = new String[55][20];

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(db, user, pw);
try
{
st = con.createStatement();
rs = st.executeQuery(
"SELECT * FROM tblCustomer");
k=0;
while(rs.next())
{
for(i = 0; i < 10; i++)
{
row[k][i] = rs.getString(i+1);
}
k++;
}
rs.close();
st.close();
}
catch(SQLException e)
{
System.out.println("SQLException in inner try block!");
System.out.println(e);
}
con.close();
}
catch(ClassNotFoundException e)
{
System.out.println("ClassNotFoundException!");
System.out.println(e);
}
catch(SQLException e)
{
System.out.println("SQLException in outer try block!");
System.out.println(e);
}

c = getContentPane(); //this part draws layout of window
c.setLayout(new BorderLayout(15, 15));

dataPanel = new JPanel(); //labels and text fields
dataPanel.setBorder(new TitledBorder("Data"));
dataPanel.setLayout(new GridLayout(10, 5,0 , 0));
Cnumber = new JLabel("Customernumber",10);
Cname = new JLabel("Customername",10);
Caddress1= new JLabel("Customeraddress1",10);
Caddress2= new JLabel("Customeraddress2",10);
City = new JLabel("CustomerCity",10);
State = new JLabel("CustomerState",10);
Zip = new JLabel("CustomerZipCodecode",10);
Telephone= new JLabel("CustomerTelephone",10);
Climit = new JLabel("credit limit",10);
Contact = new JLabel("primary CustomerPrimaryContact",10);


CustomerNumber = new JTextField(20);
CustomerName = new JTextField(20);
CustomerAddress1=new JTextField(20);
CustomerAddress2=new JTextField(20);
CustomerCity = new JTextField(20);
CustomerState = new JTextField(20);
CustomerZipCode=new JTextField(20);
CustomerTelephone=new JTextField(20);
CustomerCreditLimit = new JTextField(20);
CustomerPrimaryContact=new JTextField(20);


dataPanel.add(Cnumber);
dataPanel.add(CustomerNumber);
dataPanel.add(Cname);
dataPanel.add(CustomerName);
dataPanel.add(Caddress1);
dataPanel.add(CustomerAddress1);
dataPanel.add(Caddress2);
dataPanel.add(CustomerAddress2);
dataPanel.add(City);
dataPanel.add(CustomerCity);
dataPanel.add(State);
dataPanel.add(CustomerState);
dataPanel.add(Zip);
dataPanel.add(CustomerZipCode);
dataPanel.add(Telephone);
dataPanel.add(CustomerTelephone);
dataPanel.add(Climit);
dataPanel.add(CustomerCreditLimit);
dataPanel.add(Contact);
dataPanel.add(CustomerPrimaryContact);
c.add(dataPanel, BorderLayout.NORTH);

controlPanel = new JPanel(); //buttons to show data
controlPanel.setBorder(new TitledBorder("Control"));
controlPanel.setLayout(new GridLayout(1, 5));
first = new JButton("First");
first.addActionListener(this);
prev = new JButton("Prev");
prev.addActionListener(this);
next = new JButton("Next");
next.addActionListener(this);
last = new JButton("Last");
last.addActionListener(this);
quit = new JButton("Quit");
quit.addActionListener(this);
update = new JButton("Update");
update.addActionListener(this);
controlPanel.add(first);
controlPanel.add(prev);
controlPanel.add(next);
controlPanel.add(last);
controlPanel.add(quit);
controlPanel.add(update);
c.add(controlPanel, BorderLayout.CENTER);

statusPanel = new JPanel(); //status text field
statusPanel.setBorder(new TitledBorder("Status"));
status = new JTextField(20);
statusPanel.add(status);
c.add(statusPanel, BorderLayout.SOUTH);
}

int m=0 , n=0;
public void actionPerformed(ActionEvent e) //action listener interface
{
Object source = e.getSource();

if(source == first)
{
n = 0;
m = 0;
{
CustomerNumber.setText(row[n][m++]);
CustomerName.setText(row[n][m++]);
CustomerAddress1.setText(row[n][m++]);
CustomerAddress2.setText(row[n][m++]);
CustomerCity.setText(row[n][m++]);
CustomerState.setText(row[n][m++]);
CustomerZipCode.setText(row[n][m++]);
CustomerTelephone.setText(row[n][m++]);
CustomerCreditLimit.setText(row[n][m++]);
CustomerPrimaryContact.setText(row[n][m]);
//discnt.setText(row[n][m]);
}
}
else
if(source == prev)
{
m = 0;
if(n == 0)
{
status.setText("At the first row.");
}
else
{

CustomerNumber.setText(row[--n][m++]);
CustomerName.setText(row[n][m++]);
CustomerAddress1.setText(row[n][m++]);
CustomerAddress2.setText(row[n][m++]);
CustomerCity.setText(row[n][m++]);
CustomerState.setText(row[n][m++]);
CustomerZipCode.setText(row[n][m++]);
CustomerTelephone.setText(row[n][m++]);
CustomerCreditLimit.setText(row[n][m++]);
CustomerPrimaryContact.setText(row[n][m]);

}
}
else
if(source == next)
{
m = 0;
if(n == k-1)
{
status.setText("At the last row.");

CustomerNumber.setText(row[n][m++]);
CustomerName.setText(row[n][m++]);
CustomerAddress1.setText(row[n][m++]);
CustomerAddress2.setText(row[n][m++]);
CustomerCity.setText(row[n][m++]);
CustomerState.setText(row[n][m++]);
CustomerZipCode.setText(row[n][m++]);
CustomerTelephone.setText(row[n][m++]);
CustomerCreditLimit.setText(row[n][m++]);
CustomerPrimaryContact.setText(row[n][m]);

}
else
{
CustomerNumber.setText(row[++n][m++]);
CustomerName.setText(row[n][m++]);
CustomerAddress1.setText(row[n][m++]);
CustomerAddress2.setText(row[n][m++]);
CustomerCity.setText(row[n][m++]);
CustomerState.setText(row[n][m++]);
CustomerZipCode.setText(row[n][m++]);
CustomerTelephone.setText(row[n][m++]);
CustomerCreditLimit.setText(row[n][m++]);
CustomerPrimaryContact.setText(row[n][m]);
}
}
else
if(source == last)
{
m = 0;
n = k-1;
{ CustomerNumber.setText(row[n][m++]);
CustomerName.setText(row[n][m++]);
CustomerAddress1.setText(row[n][m++]);
CustomerAddress2.setText(row[n][m++]);
CustomerCity.setText(row[n][m++]);
CustomerState.setText(row[n][m++]);
CustomerZipCode.setText(row[n][m++]);
CustomerTelephone.setText(row[n][m++]);
CustomerCreditLimit.setText(row[n][m++]);
CustomerPrimaryContact.setText(row[n][m]);

}
}
else
if (source == update)
{ String c1,c2,c3,c4,c5,c6,c7,c8,c9,c10 ;
m=0;
n=k-1;
{ /* c1= CustomerNumber.getText(row[n][m++]);
c2=CustomerName.getText(row[n][m++]);
c3=CustomerAddress1.getText(row[n][m++]);
c4=CustomerAddress2.getText(row[n][m++]);
c5=CustomerCity.getText(row[n][m++]);
c6=CustomerState.getText(row[n][m++]);
c7=CustomerZipCode.getText(row[n][m++]);
c8=CustomerTelephone.getText(row[n][m++]);
c9=CustomerCreditLimit.getText(row[n][m++]);
c10=CustomerPrimaryContact.getText(row[n][m]);*/

c1=CustomerNumber.getText();
c2=CustomerName.getText();
c3=CustomerAddress1.getText();
c4=CustomerAddress2.getText();
c5=CustomerCity.getText();
c6=CustomerState.getText();
c7=CustomerZipCode.getText();
c8=CustomerTelephone.getText();
c9=CustomerCreditLimit.getText();
c10=CustomerPrimaryContact.getText();

}
st1 = con.createStatement();
String s1= "UPDATE tblCustomer SET CustomerNumber = c1, CustomerName=c2, CustomerAddress1=c3,CustomerAddress2=c4,CustomerCity=c5,CustomerState=c6,CustomerZipCode=c7,CustomerTelephone=c8,CustomerCreditLimit=c9,CustomerPrimaryContact=c10";
rs1=st1.executeUpdate(s1);

}
else
if(source == quit)
{
dispose();
System.exit(0);
}
return;
}

public static void main(String[] args) //main program inside class
{
customerformentrysubmit gui;
gui = new customerformentrysubmit();
gui.setBounds(50, 25, 50, 50);
gui.pack();
gui.setVisible(true);
return;
}

}



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us