0% found this document useful (0 votes)
11 views12 pages

Guyyoooo

Uploaded by

kenabadane9299
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)
11 views12 pages

Guyyoooo

Uploaded by

kenabadane9299
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/ 12

Guyyoooo

package library;

import java.sql.Connection;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.logging.Logger;

import java.util.logging.Level;

public class Library {

public static void main(String[] args) {

Library lo=new Library();

lo.createConnection();

void createConnection(){

try{

// Load the JDBC drive

Class.forName("com.mysql.cj.jdbc.Driver");

Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/libary","root","root");

Statement stmt=conn.createStatement();

ResultSet rs=stmt.executeQuery("select * from lib");

while(rs.next()){

String name=rs.getString("id");

String age=rs.getString("name");

String n=rs.getString("book");

String a=rs.getString("department");
System.out.println(name);

System.out.println(" ");

System.out.println(age);

System.out.println(" ");

System.out.println(n);

System.out.print(" ");

System.out.println(a);

System.out.println("Database Connected Successfully.");

} catch(ClassNotFoundException ex){

Logger.getLogger(Library.class.getName()).log(Level.SEVERE, null, ex);

} catch(SQLException ex){

Logger.getLogger(Library.class.getName()).log(Level.SEVERE, null, ex);

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package library;
import java.sql.Connection;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.logging.Logger;

import java.util.logging.Level;

public class lib extends javax.swing.JFrame {

Connection conn;

/**

* Creates new form lib

*/

public lib() {

initComponents();

CreateConnection();

void CreateConnection(){

try{

Class.forName("com.mysql.cj.jdbc.Driver");

conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/libary","root","root");

catch (ClassNotFoundException|SQLException ex){

Logger.getLogger(lib.class.getName()).log(Level.SEVERE, null,ex);

}
}

/**

* 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() {

jLabel1 = new javax.swing.JLabel();

jLabel2 = new javax.swing.JLabel();

jLabel3 = new javax.swing.JLabel();

t1 = new javax.swing.JTextField();

t2 = new javax.swing.JTextField();

t3 = new javax.swing.JTextField();

jLabel4 = new javax.swing.JLabel();

t4 = new javax.swing.JTextField();

b1 = new javax.swing.JButton();

jButton2 = new javax.swing.JButton();

jScrollPane1 = new javax.swing.JScrollPane();

jTextArea1 = new javax.swing.JTextArea();

jRadioButton2 = new javax.swing.JRadioButton();

jRadioButton3 = new javax.swing.JRadioButton();

jLabel5 = new javax.swing.JLabel();


setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("ID");

jLabel2.setText("Name");

jLabel3.setText("Book");

jLabel4.setText("Department");

b1.setText("Save");

b1.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

b1ActionPerformed(evt);

});

jButton2.setText("Cancel");

jTextArea1.setColumns(20);

jTextArea1.setRows(5);

jScrollPane1.setViewportView(jTextArea1);

jRadioButton2.setText("Female");

jRadioButton3.setText("Male");

jRadioButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {

jRadioButton3ActionPerformed(evt);

});

jLabel5.setText("Gender");

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(138, 138, 138)

.addComponent(b1)

.addGap(163, 163, 163)

.addComponent(jButton2))

.addGroup(layout.createSequentialGroup()

.addGap(94, 94, 94)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 468,


javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(141, Short.MAX_VALUE))

.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()

.addGap(81, 81, 81)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jLabel2)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 60,
javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jLabel3)

.addComponent(jLabel4))

.addGap(66, 66, 66)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addComponent(t1)

.addComponent(t2)

.addComponent(t3)

.addComponent(t4, javax.swing.GroupLayout.DEFAULT_SIZE, 185, Short.MAX_VALUE))

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 54,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(48, 48, 48)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addComponent(jRadioButton2, javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jRadioButton3, javax.swing.GroupLayout.Alignment.TRAILING))

.addGap(87, 87, 87))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(34, 34, 34)

.addComponent(jRadioButton3)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)

.addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(jRadioButton2)))

.addGroup(layout.createSequentialGroup()

.addGap(54, 54, 54)

.addComponent(jLabel5)))

.addGap(36, 36, 36)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(34, 34, 34)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

.addComponent(jLabel3)

.addComponent(t3, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(28, 28, 28)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel4)

.addComponent(t4, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(61, 61, 61)

.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 152,


Short.MAX_VALUE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(b1)

.addComponent(jButton2))
.addGap(58, 58, 58))

);

pack();

}// </editor-fold>

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

// TODO add your handling code here:

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

try{

String id =t1.getText();

String name =t2.getText();

String book =t3.getText();

String department =t4.getText();

Statement stmt =conn.createStatement();

String query = "insert into lib(id,name,book,department) values


('"+id+"','"+name+"','"+book+"','"+department+"')";

stmt.execute(query);

stmt.close();

System.out.println("data enter is succesfully");

} catch (SQLException ex) {

Logger.getLogger(lib.class.getName()).log(Level.SEVERE, null, ex);


}

/**

* @param args the command line arguments

*/

public static void main(String args[]) {

/* Set the Nimbus look and feel */

//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try {

for (javax.swing.UIManager.LookAndFeelInfo info :


javax.swing.UIManager.getInstalledLookAndFeels()) {

if ("Nimbus".equals(info.getName())) {

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

} catch (ClassNotFoundException ex) {

java.util.logging.Logger.getLogger(lib.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(lib.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(lib.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(lib.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);

//</editor-fold>

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new lib().setVisible(true);

});

// Variables declaration - do not modify

private javax.swing.JButton b1;

private javax.swing.JButton jButton2;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JLabel jLabel5;

private javax.swing.JRadioButton jRadioButton2;

private javax.swing.JRadioButton jRadioButton3;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JTextArea jTextArea1;

private javax.swing.JTextField t1;

private javax.swing.JTextField t2;


private javax.swing.JTextField t3;

private javax.swing.JTextField t4;

// End of variables declaration

You might also like