0% found this document useful (0 votes)
48 views5 pages

Ip101 Learn All

The document describes a Java program that connects to a MySQL database and performs basic CRUD (create, read, update, delete) operations. It imports the necessary SQL packages, defines a connection to a local database, and includes methods to show, insert, and delete data from a database table. The main method initializes the GUI elements and makes the form visible.

Uploaded by

Siddhant101
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)
48 views5 pages

Ip101 Learn All

The document describes a Java program that connects to a MySQL database and performs basic CRUD (create, read, update, delete) operations. It imports the necessary SQL packages, defines a connection to a local database, and includes methods to show, insert, and delete data from a database table. The main method initializes the GUI elements and makes the form visible.

Uploaded by

Siddhant101
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

IP database

connectivity
package myapp;
import [Link].*;
public class newForm extends [Link] {
/** Creates new form newForm */
Connection con=null;
public newForm()
{
initComponents();
try
{
//Database path
String s="jdbc:mysql://localhost:3306/test";
// Register driver
[Link]("[Link]");
con=[Link](s,"root","ROOT") ;
[Link]("Database connected");
}
catch(Exception ex)
{
[Link](null, ""+ex);
}
}
private void showdata()
{
try
{
PreparedStatement ps=[Link]("select * from t1");
ResultSet rs=[Link]();
[Link]("-----------------------------------------------");
while([Link]())
{
[Link]("Roll::"+[Link](1) +"
Name::"+[Link](2));
}
[Link]("-----------------------------------------------");
[Link](null, "All data
displayed","Information",1);
}
catch(Exception ex)
{
[Link](null, ""+ex);
}
}

private void insertdata()


{
try
{
PreparedStatement ps=[Link]("insert into t1
values(?,?)");
[Link](1, [Link]([Link]()));
[Link](2, [Link]());

IP database
connectivity
int r=[Link](); //when inserted data it will return 1;
if(r>0)
{
[Link](null, "Data
Inserted");
[Link]("");
[Link]("");
}
}
catch(Exception ex)
{
[Link](null, ""+ex);
}
}
private void deletedata()
{
try
{
PreparedStatement ps=[Link]("delete from t1
where sn=?");
[Link](1, [Link]([Link]()));
int r=[Link](); //when inserted data it will return 1;
if(r>0)
{
[Link](null, "Data
Deleted");
[Link]("");
}
}
catch(Exception ex)
{
[Link](null, ""+[Link]());
} }
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jButton1 = new [Link]();


jButton2 = new [Link]();
txtRoll = new [Link]();
txtName = new [Link]();
txtro = new [Link]();
jLabel1 = new [Link]();
jButton3 = new [Link]();
setDefaultCloseOperation([Link].EXIT_ON_CLOSE);
[Link]("Show");
[Link](new [Link]() {

IP database
connectivity
public void actionPerformed([Link] evt) {
jButton1ActionPerformed(evt);
}
});
[Link]("Insert");
[Link](new [Link]() {
public void actionPerformed([Link] evt) {
jButton2ActionPerformed(evt);
}
});
[Link](new [Link]() {
public void actionPerformed([Link] evt) {
txtRollActionPerformed(evt);
}
});
[Link]("Roll No");
[Link]("Delete");
[Link](new [Link]() {
public void actionPerformed([Link] evt) {
jButton3ActionPerformed(evt);
}

});

[Link] layout = new


[Link](getContentPane());
getContentPane().setLayout(layout);
[Link](
[Link]([Link])
.addGroup([Link]()
.addGroup([Link]([Link])

.addGroup([Link]()

.addGap(64, 64, 64)

.addComponent(jButton1))

.addGroup([Link]()

.addGap(45, 45, 45)


.addComponent(jLabel1)
.addPreferredGap([Link])

IP database
connectivity
.addGroup([Link]([Link],
false)
.addComponent(txtName)
.addComponent(txtRoll,
[Link].DEFAULT_SIZE, 98, Short.MAX_VALUE)
.addComponent(txtro))
.addPreferredGap([Link])
.addGroup([Link]([Link])
.addComponent(jButton3)
.addComponent(jButton2))))
.addContainerGap(145, Short.MAX_VALUE))
);
[Link](
[Link]([Link])
.addGroup([Link]()
.addGap(49, 49, 49)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addGroup([Link]([Link])
.addComponent(txtRoll,
[Link].PREFERRED_SIZE,
[Link].DEFAULT_SIZE,
[Link].PREFERRED_SIZE)
.addComponent(jButton2))
.addPreferredGap([Link])
.addComponent(txtName,
[Link].PREFERRED_SIZE,
[Link].DEFAULT_SIZE,
[Link].PREFERRED_SIZE)
.addGap(39, 39, 39)
.addGroup([Link]([Link])
.addComponent(txtro,
[Link].PREFERRED_SIZE,
[Link].DEFAULT_SIZE,
[Link].PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jButton3))
.addContainerGap(99, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed([Link]
evt) {
// TODO add your handling code here:
showdata();
}
private void txtRollActionPerformed([Link]
evt) {
// TODO add your handling code here:

IP database
connectivity
}
private void jButton2ActionPerformed([Link]
evt) {
// TODO add your handling code here:
insertdata();
}
private void jButton3ActionPerformed([Link]
evt) {
// TODO add your handling code here:
deletedata();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
[Link](new Runnable() {
public void run() {
new newForm().setVisible(true);
}
});
}
// Variables declaration - do not modify
private [Link] jButton1;
private [Link] jButton2;
private [Link] jButton3;
private [Link] jLabel1;
private [Link] txtName;
private [Link] txtRoll;
private [Link] txtro;
// End of variables declaration

import [Link].*.abstract;
[Link]("[Link]");

You might also like