Sie sind auf Seite 1von 5

package myapp;

import java.sql.*;
public class newForm extends javax.swing.JFrame {
/** Creates new form newForm */
Connection con=null;
public newForm()
{
initComponents();
try
{
//Database path
String s="jdbc:mysql://localhost:3306/test";
// Register driver
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection(s,"root","ROOT") ;
System.out.println("Database connected");
}
catch(Exception ex)
{
javax.swing.JOptionPane.showMessageDialog(null, ""+ex);
}
}
private void showdata()
{
try
{
PreparedStatement ps=con.prepareStatement("select * from t1");
ResultSet rs=ps.executeQuery();
System.out.println("-----------------------------------------------");
while(rs.next())
{
System.out.println("Roll::"+rs.getString(1) +"
Name::"+rs.getString(2));
}
System.out.println("-----------------------------------------------");
javax.swing.JOptionPane.showMessageDialog(null, "All data
displayed","Information",1);
}
catch(Exception ex)
{
javax.swing.JOptionPane.showMessageDialog(null, ""+ex);
}
}

private void insertdata()


{
try
{
PreparedStatement ps=con.prepareStatement("insert into t1
values(?,?)");
ps.setInt(1, Integer.parseInt(txtRoll.getText()));
ps.setString(2, txtName.getText());
int r=ps.executeUpdate(); //when inserted data it will return 1;
if(r>0)
{
javax.swing.JOptionPane.showMessageDialog(null, "Data
Inserted");

txtName.setText("");
txtRoll.setText("");
}
}
catch(Exception ex)
{
javax.swing.JOptionPane.showMessageDialog(null, ""+ex);
}
}
private void deletedata()
{
try
{
PreparedStatement ps=con.prepareStatement("delete from t1
where sn=?");
ps.setInt(1, Integer.parseInt(txtro.getText()));
int r=ps.executeUpdate(); //when inserted data it will return 1;
if(r>0)
{
javax.swing.JOptionPane.showMessageDialog(null, "Data
Deleted");
txtro.setText("");
}
}
catch(Exception ex)
{
javax.swing.JOptionPane.showMessageDialog(null, ""+ex.getMessage());
} }
/** 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 javax.swing.JButton();


jButton2 = new javax.swing.JButton();
txtRoll = new javax.swing.JTextField();
txtName = new javax.swing.JTextField();
txtro = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jButton3 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("Show");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("Insert");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

txtRoll.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txtRollActionPerformed(evt);
}
});
jLabel1.setText("Roll No");
jButton3.setText("Delete");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}

});

javax.swing.GroupLayout layout = new


javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(64, 64, 64)

.addComponent(jButton1))

.addGroup(layout.createSequentialGroup()

.addGap(45, 45, 45)


.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
false)
.addComponent(txtName)
.addComponent(txtRoll,
javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
.addComponent(txtro))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton3)
.addComponent(jButton2))))
.addContainerGap(145, Short.MAX_VALUE))
);
layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(49, 49, 49)
.addComponent(jButton1)
.addGap(18, 18, 18)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtRoll,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(txtName,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(39, 39, 39)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(txtro,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jButton3))
.addContainerGap(99, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
showdata();
}
private void txtRollActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
insertdata();
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
deletedata();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new newForm().setVisible(true);
}
});

}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JLabel jLabel1;
private javax.swing.JTextField txtName;
private javax.swing.JTextField txtRoll;
private javax.swing.JTextField txtro;
// End of variables declaration

import java.sql.*.abstract;
class.forName("java.sql.Driver");

Das könnte Ihnen auch gefallen