Monday, 22 July 2013

display a multiple records DB Connectivity


Data Connectivity Application 3: To Display the Details Stored in the Back-end Based on the Name Input by the User in a Form.


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
("jdbc: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())
{
String mobile = rs.getString("Mobile");
String email = rs.getString("Email");
jTextField2.setText(mobile);
jTextField3.setText(email);
JOptionPane.showMessageDialog (null,"Click OK to continue!!!");
Found++;
}
if (Found = = 0)
JOptionPane.showMessageDialog
(this,"Sorry! No such Name in Contact List");
}
catch (Exception e)
{
JOptionPane.showMessageDialog(this, e.getMessage());
}
}
}

No comments:

Post a Comment