Вы находитесь на странице: 1из 3

import java.sql.DriverManager; import com.mysql.jdbc.Connection; import com.mysql.jdbc.

Statement; Consider the following code and answer the questions that follow: String query="SELECT NAME,EMAIL FROM CONTACT" ResultSet rs=stmt.executeQuery(query); //Statement 1 if (rs.next()) //Statement 2 { String Name = rs.getString("Name"); String Email = rs.getString("Email"); jTextField2.setText(Name); jTextField3.setText(Email); } /*Netbeans Application 2 (Basic Part) - To search for a matching row and display corresponding information from the table Contact */ private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // Search the table to find a record matching // the input Mobile no. String Mobile=jTextField1.getText(); if (Mobile.isEmpty()) //Execute this part if text field is blank { jTextField2.setText(""); jTextField3.setText(""); JOptionPane.showMessageDialog (this,"Enter the Mobile No"); } // This part is executed if a Mobile No is // input in the text field else { try { Class.forName("java.sql.DriverManager"); Connection con = (Connection) DriverManager.getConnection ("jdbc:mysql://localhost:3306/cbse", "root", "abcd1234"); Statement stmt = (Statement) con.createStatement(); String query="SELECT NAME,EMAIL FROM CONTACT WHERE MOBILE='"+Mobile+"';";

ResultSet rs=stmt.executeQuery(query); if (rs.next()) { String Name = rs.getString("Name"); //Retrieve the name String Email = rs.getString("Email"); //Retrieve the email jTextField2.setText(Name); jTextField3.setText(Email); } // This part is executed if no matching record is found else JOptionPane.showMessageDialog (this,"Sorry!No such Mobile No"); } catch (Exception e) { JOptionPane.showMessageDialog(this, e.getMessage()); } } } private void jButton2ActionPerformed (java.awt.event.ActionEvent evt) { System.exit(0); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String name=jTextField1.getText(); if (name.isEmpty()) JOptionPane.showMessageDialog(this,"Name not Entered"); else {try { Class.forName("java.sql.DriverManager"); Connection con = (Connection) DriverManager.getConnection ("dbc:mysql://localhost:3306/cbse", "root", "abcd1234"); Statement stmt = (Statement) con.createStatement(); String query="SELECT Mobile,Email FROM Contact WHERE Name='"+name+"';"; ResultSet rs=stmt.executeQuery(query); int Found=0;

while(rs.next()) // Till there are records in the result set { String mobile = rs.getString("Mobile"); String email = rs.getString("Email"); jTextField2.setText(mobile); jTextField3.setText(email); JOptionPane.showMessageDialog (null,"Click OK to continue!!!"); Found++; //Increment the variable to indicate a matching //record has been found } if (Found==0) JOptionPane.showMessageDialog (this,"Sorry! No such Name in Contact List"); } catch (Exception e) { JOptionPane.showMessageDialog(this, e.getMessage()); } } }

Вам также может понравиться