0% found this document useful (0 votes)
21 views19 pages

Lab 4

Lab report

Uploaded by

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

Lab 4

Lab report

Uploaded by

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

Insert

package lab4;
import java.sql.*;

public class Insert {

public static void main(String[] args)throws Exception {


Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/RRDB","root","");
String sql="insert into student values
(101,'Ram','RRCampus'),(102,'Sudip','PadmodayaCampus'),(103,'Messi','Intermia
mi')";
Statement st=con.createStatement();
int row=st.executeUpdate(sql);
if(row>0)
System.out.println(row+"Row is inserted sucessfully");
else
System.out.println("Error!!");
con.close();

}
Display
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this
template
*/
package lab4;

import java.sql.*;

public class Display {

public static void main(String args[]) throws Exception {

Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/RRDB","root","");
String sql="select *from student";
Statement st=con.createStatement();
ResultSet rs=st.executeQuery(sql);
System.out.println("Roll \t Name \t College ");

while (rs.next())
{
System.out.println(rs.getInt(1)+"\t" +rs.getString(2)+"\t"+rs.getString(3));

}
con.close();

}
}
Update
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this
template
*/

package lab4;

import java.sql.*;

public class Update {


public static void main(String[]args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/RRDB","root","");

String sql="update Student set College='patan campus campus' where


Roll='101'";
Statement st=con.createStatement();
int row =st.executeUpdate(sql);
if(row>0)
{
System.out.println(row+"Updated the column successfully");

}
else
{
System.out.println("Erorr in updation");

}
con.close();
}
}
Delete
package lab4;
import java.sql.*;

public class Delete {

public static void main(String args[]) throws Exception {


Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/RRDB","root","");
String sql="delete from Student where roll=101";

Statement st=con.createStatement();
int row =st.executeUpdate(sql);
if(row>0)
{
System.out.println(row+"Deleted successfully");

}
else
{
System.out.println("Erorr in Deletion");

}
con.close();
}
}
Login form
package lab4;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class LoginForm {


JFrame f;
JLabel l1, l2;
JTextField t1;
JPasswordField t2;
JButton b1, b2;

public LoginForm() {
f = new JFrame("Login Form");
l1 = new JLabel("User ID:");
l2 = new JLabel("Password:");
t1 = new JTextField(25);
t2 = new JPasswordField(25);

b1 = new JButton("OK");
b2 = new JButton("Cancel");

f.setSize(300, 300);
f.setVisible(true);
f.setDefaultCloseOperation(3);
f.setLayout(null);

l1.setBounds(20, 30, 100, 20);


t1.setBounds(150, 30, 100, 20);
l2.setBounds(20, 70, 100, 20);
t2.setBounds(150, 70, 100, 20);
b1.setBounds(50, 120, 80, 20);
b2.setBounds(150, 120, 80, 20);

f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(b1);
f.add(b2);

b1.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyChar() == 'l') {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/account", "root", "");
String id = t1.getText();
String password = new String(t2.getPassword());

String sql = "SELECT * FROM user WHERE id = ? AND password = ?";


PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, password);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
JOptionPane.showMessageDialog(f, "Login successful!");
} else {
JOptionPane.showMessageDialog(f, "Invalid credentials.");
}
con.close();
} catch (Exception ex) {
ex.printStackTrace();
}
} else if (e.getKeyChar() == 'c') {
t1.setText("");
t2.setText("");
t1.requestFocus();
}
}
});
}

public static void main(String[] args) {


new LoginForm();
}
}
Signup fomr
package lab4;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

Public class SignupForm {


JFrame f;
JLabel l1, l2, l3, l4, l5, l6, l7;
JTextField t1, t2, t3, t6;
JRadioButton rb1, rb2;
JCheckBox cb1, cb2, cb3;
JComboBox<String> countryComboBox;
JTextArea ta1;
JButton submitButton;

public SignupForm() {
f = new JFrame("Signup Form");

l1 = new JLabel("ID:");
l2 = new JLabel("Name:");
l3 = new JLabel("Email:");
l4 = new JLabel("Gender:");
l5 = new JLabel("Hobbies:");
l6 = new JLabel("Country:");
l7 = new JLabel("Local Address:");

t1 = new JTextField(25);
t2 = new JTextField(25);
t3 = new JTextField(25);

rb1 = new JRadioButton("Male");


rb2 = new JRadioButton("Female");

ButtonGroup bg = new ButtonGroup();


bg.add(rb1);
bg.add(rb2);

cb1 = new JCheckBox("Reading");


cb2 = new JCheckBox("Traveling");
cb3 = new JCheckBox("Swimming");

String[] countries = {"USA", "Canada", "UK", "Australia", "Nepal"};


countryComboBox = new JComboBox<>(countries);

ta1 = new JTextArea();


ta1.setLineWrap(true);
ta1.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(ta1,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

submitButton = new JButton("Submit");

f.setSize(400, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(null);

l1.setBounds(20, 30, 100, 20);


t1.setBounds(150, 30, 200, 20);
l2.setBounds(20, 70, 100, 20);
t2.setBounds(150, 70, 200, 20);
l3.setBounds(20, 110, 100, 20);
t3.setBounds(150, 110, 200, 20);
l4.setBounds(20, 150, 100, 20);
rb1.setBounds(150, 150, 100, 20);
rb2.setBounds(260, 150, 100, 20);
l5.setBounds(20, 190, 100, 20);
cb1.setBounds(150, 190, 100, 20);
cb2.setBounds(260, 190, 100, 20);
cb3.setBounds(150, 220, 100, 20);
l6.setBounds(20, 260, 100, 20);
countryComboBox.setBounds(150, 260, 200, 20);
l7.setBounds(20, 300, 100, 20);
scrollPane.setBounds(150, 300, 200, 100);
submitButton.setBounds(150, 420, 100, 20);

f.add(l1);
f.add(t1);
f.add(l2);
f.add(t2);
f.add(l3);
f.add(t3);
f.add(l4);
f.add(rb1);
f.add(rb2);
f.add(l5);
f.add(cb1);
f.add(cb2);
f.add(cb3);
f.add(l6);
f.add(countryComboBox);
f.add(l7);
f.add(scrollPane);
f.add(submitButton);

f.setVisible(true);

submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/account", "root", "");

String id = t1.getText();
String name = t2.getText();
String email = t3.getText();
String gender = rb1.isSelected() ? "Male" : "Female";
StringBuilder hobbies = new StringBuilder();
if (cb1.isSelected()) hobbies.append("Reading ");
if (cb2.isSelected()) hobbies.append("Traveling ");
if (cb3.isSelected()) hobbies.append("Swimming ");
String country = countryComboBox.getSelectedItem().toString();
String localAddress = ta1.getText();

String sql = "INSERT INTO Person (id, name, email, gender, hobbies,
country, localAddress) VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, id);
pstmt.setString(2, name);
pstmt.setString(3, email);
pstmt.setString(4, gender);
pstmt.setString(5, hobbies.toString());
pstmt.setString(6, country);
pstmt.setString(7, localAddress);
pstmt.executeUpdate();

JOptionPane.showMessageDialog(f, "Data inserted successfully!");

con.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});

JButton showDetailsButton = new JButton("Show Details");


showDetailsButton.setBounds(260, 420, 120, 20);
f.add(showDetailsButton);

showDetailsButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/account", "root", "");

String inputId = JOptionPane.showInputDialog(f, "Enter ID:");


String sql = "SELECT * FROM Person WHERE id = ?";
PreparedStatement pstmt = con.prepareStatement(sql);
pstmt.setString(1, inputId);
ResultSet rs = pstmt.executeQuery();

if (rs.next()) {
String personDetails = "ID: " + rs.getString("id") + "\n" +
"Name: " + rs.getString("name") + "\n" +
"Email: " + rs.getString("email") + "\n" +
"Gender: " + rs.getString("gender") + "\n" +
"Hobbies: " + rs.getString("hobbies") + "\n" +
"Country: " + rs.getString("country") + "\n" +
"Local Address: " + rs.getString("localAddress");
JOptionPane.showMessageDialog(f, personDetails);
} else {
JOptionPane.showMessageDialog(f, "Person not found.");
}

con.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}

public static void main(String[] args) {


new SignupForm();
}
}
Scrollable updateble
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this
template
*/
package lab4;

import java.sql.*;
public class ScrollableandUpdatable {
public static void main(String[]args ) throws Exception {

Class.forName("com.mysql.jdbc.Driver");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost/RRDB","root","");
String sql="select *from Student";

PreparedStatement
ps=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONC
UR_UPDATABLE);
ResultSet rs=ps.executeQuery();
rs.absolute(3);
rs.previous();
rs.last();
System.out.println("Roll \t Name \t College");

System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getString(3));

con.close();
}
}
Rowset

package lab4;

import java.sql.*;
import javax.sql.rowset.*;

public class CacheRowSetEx {


public static void main(String[] args) throws Exception {
// Load the MySQL JDBC driver
Class.forName("com.mysql.jdbc.Driver");

// Establish the connection


Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/RRDB", "root", "");

// SQL query
String sql = "SELECT * FROM Student";

// Prepare and execute the statement


PreparedStatement ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
// Create and populate CachedRowSet
CachedRowSet rowset =
RowSetProvider.newFactory().createCachedRowSet();
rowset.populate(rs);

// Close the connection


con.close();

// Print headers
System.out.println("Roll \t Name \t College");

// Navigate and print specific rows


rowset.next();
rowset.absolute(2);
rowset.last();

// Print row data


System.out.println(rowset.getInt(1) + "\t" + rowset.getString(2) + "\t" +
rowset.getString(3));
}
}

You might also like