0% found this document useful (0 votes)
45 views4 pages

Ques On Connectivity-1

This document contains code snippets for connecting a Java program to a MySQL database and performing basic CRUD (create, read, update, delete) operations. The snippets show: 1) Connecting to the MySQL database and initializing statements 2) Inserting new data by executing an INSERT statement 3) Deleting data by ID by executing a DELETE statement 4) Exiting the Java program

Uploaded by

AKG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views4 pages

Ques On Connectivity-1

This document contains code snippets for connecting a Java program to a MySQL database and performing basic CRUD (create, read, update, delete) operations. The snippets show: 1) Connecting to the MySQL database and initializing statements 2) Inserting new data by executing an INSERT statement 3) Deleting data by ID by executing a DELETE statement 4) Exiting the Java program

Uploaded by

AKG
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

PROGRAMS BASED ON CONNECTIVITY

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

String user,pass;

user=jTextField1.getText();

pass=new String(jPasswordField1.getPassword());

if (user.equals("maxfort") && pass.equals("rohini"))

new menu().setVisible(true);

else

JOptionPane.showMessageDialog(null," Wrong Password try again");

}
1. …….TO INSERT NEW DATA INTO MYSQL…….
private void jButton1ActionPerformed(java.awt.event.ActionEventevt) {
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/xii","root","max@123");
Statement stmt=null;
ResultSetrs=null;
stmt=con.createStatement();
jTextField1.setText("");
jTextField2.setText("");
jTextArea1.setText("");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,e.getMessage());
}
2. …….TO SAVE DATA INTO MYSQL…….
private void jButton2ActionPerformed(java.awt.event.ActionEventevt) {
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/xii","root","max@123");
Statement stmt=null;
ResultSetrs=null;
stmt=con.createStatement();
int a = Integer.parseInt(jTextField1.getText());
String b = jTextField2.getText();
String c=jTextArea1.getText();
stmt.executeUpdate("insert into customer values("+(a)+",'"+(b)+"','"+(c)+"')");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,e.getMessage());
}

3. …….TO DELETE DATA FROM MYSQL…….


private void jButton3ActionPerformed(java.awt.event.ActionEventevt) {
int x=0;
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection)
DriverManager.getConnection("jdbc:mysql://localhost:3306/xii","root","max@123");
Statement stmt=null;
ResultSetrs=null;
stmt=con.createStatement();
int a = Integer.parseInt(jTextField1.getText());
x=stmt.executeUpdate("delete from customer where cid="+(a)+"");
if(x==0)
JOptionPane.showMessageDialog(this,"not exist");
else
JOptionPane.showMessageDialog(this,"DELETED");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,e.getMessage());
}

4. …….TO EXIT FROM NETBEANS…….


System.exit(0);

You might also like