dcsimg
Question abooout java
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   nawaray_ms
Posted On:   Tuesday, October 7, 2003 02:14 AM

hi .... i have this java code that i want someone to help me with ... when i run this code it will display the fields that is stored im my table customer ..but i don't want this.. i want whenever the user enters in the customerid and customername and then clicks the button add these valuses will be inserted into the table customer.. import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test extends JFrame implements ActionListener { JTextField id; JTextField name; JButton add; JPanel p; static ResultSet res; static Connection   More>>

hi ....

i have this java code that i want someone to help me with ...

when i run this code it will display the fields that is stored im my table customer ..but i don't want this..

i want whenever the user enters in the customerid and customername and then clicks the button add these valuses will be inserted into the table customer..



import java.sql.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;


public class Test extends JFrame implements ActionListener {


JTextField id;

JTextField name;

JButton add;

JPanel p;

static ResultSet res;

static Connection conn;

static Statement stat;

static PreparedStatement prep;


public Test() {

super("Our Application");

Container c = getContentPane();

c.setLayout(new GridLayout(5,1));

id = new JTextField(20);

name = new JTextField(20);

add = new JButton("ADD");

p = new JPanel();

c.add(new JLabel("Customer ID",JLabel.CENTER));

c.add(id);

c.add(new JLabel("Customer Name",JLabel.CENTER));

c.add(name);

c.add(p);

p.add(add);

add.addActionListener(this);

pack();

setVisible(true);

addWindowListener(new WIN());


}


public static void main(String args[]) {

Test t = new Test();

try {

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

conn = DriverManager.getConnection("jdbc:odbc:cust"); // cust is the DSNName


stat = conn.createStatement();


/*result= stmt.executeUpdate(

"INSERT INTO Customers " +

"VALUES (12, 'jack')");

*/


res = stat.executeQuery("Select * from Customers"); // Customers is the table name


res.next();


}

catch(Exception e) {

System.out.println("Error" +e);

}

t.showRecord(res);

}


public void actionPerformed(ActionEvent e) {

if(e.getSource() == add) {

try {

//res.next();

prep = con.prepareStatement(

"INSERT into Customers values (?, ?)");


/*res= stat.executeUpdate(

"INSERT INTO Customers " +

"VALUES (12, 'jack')");

*/

}

catch(Exception ee) {}

showRecord(res);

}

}


public void showRecord(ResultSet res) {

try {

id.setText(res.getString(1));

name.setText(res.getString(2));

prep.setString(12, "Jim");

}

catch(Exception e) {}


}//end of the main


//Inner class WIN implemented

class WIN extends WindowAdapter {

public void windowClosing(WindowEvent w) {

JOptionPane jop = new JOptionPane();

jop.showMessageDialog(null,"Database","Thanks",JOptionPane.QUESTION_MESSAGE);


}


} //end of the class


}



and when i put this statement

res= stat.executeUpdate(

"INSERT INTO Customers " +

"VALUES (12, 'jack')");

i get this error

cannot resolve symbol

symbol : variable result

location: class Test

result= stat.executeUpdate(

^
1 error



so please can someone help me with inserting values to the table customer

   <<Less

Re: Question abooout java

Posted By:   Christopher_Koenigsberg  
Posted On:   Tuesday, October 7, 2003 06:06 AM

It helps, to declare your variable before trying to use it, e.g. "result"... perhaps that should be "res"?

About | Sitemap | Contact