I am using NetBeans

i have a 'Search by employee id' button .

I want when user click this button a dialogue box appear which take employee_id as input from user .
then that input be used for fethcing data from the database ( desired table in DB has primary key emp_id ).

How can i do this ?
I just want to use the Input (as String) given by user in dialogue box ..

Please Help!

Thanx In Advance..

Dani AI

Generated

Brief diagnosis and what to check first

The input dialog appearing and then “nothing” happening most often means the query returned zero rows or the input value was not handled. Common causes seen in threads like 's: the dialog returns null when Cancel is pressed, the entered string has leading/trailing spaces or a different format than the DB key, the connection object is out of scope or null, or exceptions are being swallowed (showing only e.getMessage() can hide useful details). 's suggestion to use a simple input dialog is fine for one value; 's advice about a custom dialog applies if more complex validation/controls are needed.

Quick checklist to narrow the problem

  • Verify the value from the input dialog is not null and is trimmed before use.
  • Confirm the Connection con used in the event handler is the same instance successfully created earlier (not a different local variable).
  • Print or log the SQL being executed and run it directly in the DB tool to confirm results.
  • If emp_id is numeric, pass an int parameter instead of a quoted string.
  • During debugging show full stack traces (or log them) so SQLExceptions are visible.

Robust pattern (use a PreparedStatement and check result)

String empId = JOptionPane.showInputDialog(this, "Enter Employee ID:");
if (empId == null) return;               // user cancelled
empId = empId.trim();
if (empId.isEmpty()) {
  JOptionPane.showMessageDialog(this, "Employee ID required");
  return;
}

String sql = "SELECT * FROM emp WHERE emp_id = ?";
try (PreparedStatement ps = con.prepareStatement(sql)) {
  ps.setString(1, empId);                // or setInt after parsing if numeric
  try (ResultSet rs = ps.executeQuery()) {
    if (rs.next()) {
      jTextField27.setText(rs.getString("emp_id"));
      jTextField28.setText(rs.getString("first_name"));
      // fill other fields similarly
    } else {
      JOptionPane.showMessageDialog(this, "No employee found for ID: " + empId);
    }
  }
} catch (SQLException ex) {
  ex.printStackTrace();
  JOptionPane.showMessageDialog(this, "Database error: " + ex.getMessage());
}

Extra tips

Prefer if (rs.next()) for a primary-key lookup (not while). Use PreparedStatement to avoid SQL injection and mismatched quoting. For long-running DB calls move work off the EDT (SwingWorker). If a custom input UI and richer validation are required, use a modal dialog as recommended.

Recommended Answers

All 6 Replies

If the data you want is contained in varaibles in the dialog box class, you need to add methods to that class that you can call to get the data.

Can you post the code you are having problems with.

thats whatn i want -- The Code ---
I dont know what to code for getting user input through dialogue box when a button is pressed.

Can you please Help with the code.

If you only need to get a single value use a JOptionPane. If you need a more complicated then use the JDialog class.
see the tutorial for samples:
Link Anchor Text

Okay here is the code for the actionPerformed event of the get employee detail button

 private void search_empDetails_by_empId(java.awt.event.ActionEvent evt) {

      try {
     String empId = JOptionPane.showInputDialog("Enter Employee ID :");

    Statement stmt=con.createStatement();
    String sql1 = "Select * from emp where emp_id = '" + (empId) + "'";
    ResultSet rs = stmt.executeQuery(sql1);

    while (rs.next())
    {
        String empid = rs.getString("emp_id");
        String f_name = rs.getString("first_name");
        String l_name = rs.getString("last_name");
        String qual = rs.getString("qualification");
        String dept = rs.getString("department");
        String dob = rs.getString("dob");
        String do_join = rs.getString("dojoin");
        String mobile = rs.getString("mob");
        String electmail = rs.getString("email");
        String ageyr = rs.getString("age");
        String sexmf = rs.getString("sex");
        String addrs = rs.getString("address");

        String proj_handled = rs.getString("project_handled");
        String curr_proj = rs.getString("current_project");
        String exp = rs.getString("experience");
        String skill = rs.getString("skills");

        String train_done = rs.getString("training_done");
        String curr_train = rs.getString("current_training");
        String skill_rmk = rs.getString("skill_remark");

        String sal_block = rs.getString("salary_block");
        String ann_income = rs.getString("annual_income");
        String facility = rs.getString("facilities");
        String insure = rs.getString("insurance"); 
// setting data in tab Employee details
jTextField27.setText(""+ empid);
jTextField28.setText(""+ f_name);
jTextField29.setText(""+ l_name);
jTextField30.setText(""+ qual);
jTextField31.setText(""+ dept);
jTextField32.setText(""+ dob);
jTextField33.setText(""+ do_join);
jTextField34.setText(""+ mobile);
jTextField35.setText(""+ electmail);
jTextField36.setText(""+ ageyr);
jTextField37.setText(""+ sexmf);
jTextField38.setText(""+ addrs);

// Setting data in tab Skill Report
jTextField6.setText(""+ proj_handled);
jTextField7.setText(""+ curr_proj);
jTextField8.setText(""+ exp);
jTextField12.setText(""+ skill);

// Setting data in tab Training Report
jTextField16.setText(""+ train_done);
jTextField18.setText(""+ curr_train);
jTextField19.setText(""+ skill_rmk);

// Setting data in tab compensation Report
jTextField23.setText(""+ sal_block);
jTextField24.setText(""+ ann_income);
jTextField25.setText(""+ facility);
jTextField26.setText(""+ insure);

// Skill Report
jTextField1.setText(""+ empid );
jTextField2.setText(""+ f_name);
jTextField3.setText(""+ l_name);
jTextField4.setText(""+ qual);
jTextField5.setText(""+ dept);

//Training Report
jTextField9.setText(""+ empid);
jTextField11.setText(""+ f_name);
jTextField13.setText(""+ l_name);
jTextField14.setText(""+ qual);
jTextField15.setText(""+ dept);

//Compensation Report
jTextField17.setText(""+ empid);
jTextField20.setText(""+ f_name);
jTextField21.setText(""+ l_name);
jTextField22.setText(""+ dept);

   // making new employee password field noneditable and invisible
        jTextField39.setEditable(false);  // new employee password field.
        jTextField39.setVisible(false);
        jLabel43.setVisible(false);
    }
}
catch(Exception e) {
    JOptionPane.showMessageDialog(this, e.getMessage());
}

I dont know where the problem is??
what i Run the program and click 'get emp_detil ' button a input dialog appear and ask for input.
when input and given and Ok is pressed it just disappears and nothing happens..

Please Help.
Thanx in advace..

and i have already given

try{
        String un="root";
        String pass="soft";
        String url="jdbc:mysql://localhost/hrms";
        Class.forName("com.mysql.jdbc.Driver");
        con=DriverManager.getConnection(url,un,pass);



    }
    catch(Exception e){
        e.printStackTrace();
    }

    // in the constructor of the class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.