autonumber

autonumber

sir

many many thanks for my previous answer .

i am facing a problem in my project.

i am initializing text field with 7001 and whenever i click save button it should be saved in database and incremented new value 7002 should be display. please tell me how to do this.

thanks
View Answers

March 30, 2010 at 12:39 PM

Hi Friend,

Try the following code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class Form extends JFrame {
JButton SAVE;
JPanel panel;
int i=7001;
JLabel label1,label2,label3,label4,label5;
final JTextField text1,text2,text3,text4,text5;
Form(){
label1 = new JLabel();
label1.setText("UserID:");
text1 = new JTextField(20);
text1.setText(Integer.toString(i));


label2 = new JLabel();
label2.setText("First Name:");
text2 = new JTextField(20);

label3 = new JLabel();
label3.setText("Last Name:");
text3 = new JTextField(20);

label4 = new JLabel();
label4.setText("ADDRESS:");
text4 = new JTextField(20);

label5 = new JLabel();
label5.setText("Email:");
text5 = new JTextField(20);

SAVE=new JButton("SAVE");

panel=new JPanel(new GridLayout(6,2));
panel.add(label1);
panel.add(text1);
panel.add(label2);
panel.add(text2);
panel.add(label3);
panel.add(text3);
panel.add(label4);
panel.add(text4);
panel.add(label5);
panel.add(text5);
panel.add(SAVE);
add(panel,BorderLayout.CENTER);
setTitle("FORM");

SAVE.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae){
String value1=text1.getText();
String value2=text2.getText();
String value3=text3.getText();
String value4=text4.getText();
String value5=text5.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);
Statement st = con.createStatement();
int i= st.executeUpdate("Insert into register(id,firstName,lastName,address,email) values('"+value1+"','"+value2+"','"+value3+"','"+value4+"','"+value5+"')");
JOptionPane.showMessageDialog(null,"Data is successfully inserted into the database." );
st.close();
con.close();
}
catch(Exception e){
System.out.println(e);
}
i=i+1;
text2.setText(" ");
text3.setText(" ");
text4.setText(" ");
text5.setText(" ");
text1.setText(Integer.toString(i));
}
});
}
}
class FormDemo{
public static void main(String arg[]){
try {
Form frame=new Form();
frame.setSize(300,300);
frame.setVisible(true);
}
catch(Exception e)
{}
}
}

Thanks









Related Tutorials/Questions & Answers:
autonumber - IDE Questions
ms access
ms access  how to delete the autonumber from ms access using java delete code
Advertisements
Jsp - JSP-Servlet
Jsp  Hello sir, And how to store an array of strings in access database please reply me sir Thank you sir.   Hi Friend, We have created a table named names(id(autonumber),name(text),address(text)). Try
please tell me
) id(AutoNumber) name(Text) address(Text) image(OLE OBJECT) <%@page import
SQL query - SQL
'FeesStruc' with the following fields: 1)id (AutoNumber) 2)Amount (Number) 3
java - Java Beginners
of following fields: id(AutoNumber) username(Text) password(Text) name(Text

Ads