
hai, i need to generate ID i.e when i select addemploye option and submit, it should generate a emp ID which is 1 greater than the previous highest ID value stored in database. eg: if the higest value of empID stored in database is 4, the next time after i select add employ option it should generate empID as 5 and i should continue with inserting other values like ename,ecity etc........Plz help

Here is a code that generates id into database. In the given code, we have fetched the last id from the database table and by adding 1, we have generated the next id and inserted into database table.
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class ComboBox{
public static void main(String[] args) throws Exception{
JFrame f=new JFrame();
f.setLayout(null);
JLabel lab=new JLabel("Select Name:");
final String st[] = {"Add Employee","Edit Employee","Delete Employee","View Employee"};
final JComboBox combo=new JComboBox(st);
JButton b=new JButton("Submit");
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent actionEvent) {
Object obj = combo.getSelectedItem();
String sel=obj.toString();
System.out.println(sel);
if(sel.equals(st[0])){
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root" );
Statement st=conn.createStatement();
Statement stmt=conn.createStatement();
ResultSet rs=st.executeQuery("select * from data1");
int id=0;
while(rs.next()){
id=rs.getInt("id");
}
System.out.println(id);
int ide=id+1;
stmt.executeUpdate("insert into data1(id) values("+ide+")");
System.out.println("Inserted");
}
catch(Exception e){
System.out.println(e);
}
}
}
});
lab.setBounds(20,20,100,20);
combo.setBounds(120,20,80,20);
b.setBounds(220,20,80,20);
f.add(lab);
f.add(combo);
f.add(b);
f.setVisible(true);
f.setSize(300,120);
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.