Sie sind auf Seite 1von 31

Category : B

Aim: JDBC
B1. Write a JDBC Program that create Emp table.The Emp table will have EmpNo,
EmpName, Salary and Location

Source Code:

package javaapplication23;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.Statement;

public class JavaApplication23 {

public static void main(String[] args) {

try

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

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

System.out.println("connection jhdg");

Statement st=con.createStatement();

System.out.println("statement Created");

String query="create table emp(eno int,ename varchar(20),salary int,location


varchar(20))";

st.execute(query);

System.out.println("table Created");

st.close();

con.close();

}
catch(Exception e)

System.out.println(e);

OUTPUT :
B2. Write a JDBC program that will allow user to insert and update records to Emp
table.

Source Code:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import javax.swing.JOptionPane;

public class Data extends javax.swing.JFrame {

Connection con;

PreparedStatement ps;

String query;

public Data() {

initComponents();

try

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

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

catch(Exception e)

System.out.println(e);

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

try
{

ps=con.prepareStatement("insert into emp values(?,?,?,?)");

ps.setInt(1,Integer.parseInt(t1.getText()));

ps.setString(2,t2.getText());

ps.setInt(3,Integer.parseInt(t3.getText()));

ps.setString(4,t4.getText());

ps.executeUpdate();

JOptionPane.showMessageDialog(this,"record added");

catch(Exception e)

System.out.println(e);

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

try

String s=JOptionPane.showInputDialog(this,"Enter Eno:");

ps=con.prepareStatement("Delete from emp where eno=?");

ps.setInt(1,Integer.parseInt(s));

ps.executeUpdate();

JOptionPane.showMessageDialog(this,"record deleted");

catch(Exception e)

System.out.println(e);
}

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

t1.setText("");

t2.setText("");

t3.setText("");

t4.setText("");

public static void main(String args[]) {

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

public void run() {

new Data().setVisible(true);

});

private javax.swing.JButton b1;

private javax.swing.JButton b2;

private javax.swing.JButton b3;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JTextField t1;

private javax.swing.JTextField t2;

private javax.swing.JTextField t3;


private javax.swing.JTextField t4;

OUTPUT:
B3. Write a JDBC program that will allow user to Navigate & update records to
Emp Table

Source Code:

package catb3;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import javax.swing.JOptionPane;

public class jdb extends javax.swing.JFrame {

Connection con;

PreparedStatement ps;

String query;

ResultSet rs;

Statement stmt;

public jdb() {

initComponents();

try

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

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

stmt=con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_U
PDATABLE);

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


}

catch(Exception e)

System.out.println(e);

void showdata(ResultSet rs)

try

t1.setText(""+rs.getInt(1));

t2.setText(rs.getString(2));

t3.setText(""+rs.getInt(3));

t4.setText(rs.getString(4));

catch(Exception e)

System.out.println(e);

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

t1.setText("");

t2.setText("");
t3.setText("");

t4.setText("");

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

try

rs.first();

showdata(rs);

catch(Exception e)

System.out.println(e);

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

try

rs.next();

if(rs.isAfterLast())

JOptionPane.showMessageDialog(this,"you are at last record");

rs.first();

}
else

showdata(rs);

catch(Exception e)

System.out.println(e);

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

try

rs.previous();

if(rs.isBeforeFirst())

JOptionPane.showMessageDialog(this,"you are at First record");

rs.last();

else

showdata(rs);

catch(Exception e)
{

System.out.println(e);

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

try

rs.last();

showdata(rs);

catch(Exception e)

System.out.println(e);

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

try

ps=con.prepareStatement("Update emp set ename=?, salary=?,location=? where


eno=?");

ps.setString(1,t2.getText());

ps.setInt(2,Integer.parseInt(t3.getText()));

ps.setString(3,t4.getText());
ps.setInt(4,Integer.parseInt(t1.getText()));

ps.executeUpdate();

JOptionPane.showMessageDialog(this,"record Updated");

catch(Exception e)

System.out.println(e);

public static void main(String args[]) {

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

public void run() {

new jdb().setVisible(true);

});

private javax.swing.JButton b1;

private javax.swing.JButton b2;

private javax.swing.JButton b3;

private javax.swing.JButton b4;

private javax.swing.JButton b5;

private javax.swing.JButton b6;

private javax.swing.JLabel jLabel1;

private javax.swing.JLabel jLabel2;

private javax.swing.JLabel jLabel3;

private javax.swing.JLabel jLabel4;

private javax.swing.JTextField t1;


private javax.swing.JTextField t2;

private javax.swing.JTextField t3;

private javax.swing.JTextField t4;

OUTPUT:
B4.Write a Program to accept username and password from user and verify
records from login(uname,password) table if correct display login successful
otherwise retry.

Source Code:

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import javax.swing.JOptionPane;

public class login extends javax.swing.JFrame {

public login() {

initComponents();

private void initComponents() {

l1 = new javax.swing.JLabel();

l2 = new javax.swing.JLabel();

t1 = new javax.swing.JTextField();

t2 = new javax.swing.JPasswordField();

b1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

l1.setText("User Name:-");

l2.setText("Password:-");
b1.setText("Login");

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

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

b1ActionPerformed(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(33, 33, 33)

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

.addComponent(l1, javax.swing.GroupLayout.PREFERRED_SIZE, 100,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(l2, javax.swing.GroupLayout.PREFERRED_SIZE, 100,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(101, 101, 101)

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

.addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, 100,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 100,


javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGroup(layout.createSequentialGroup()

.addGap(91, 91, 91)

.addComponent(b1)))

.addContainerGap(66, Short.MAX_VALUE))

);

layout.setVerticalGroup(

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

.addGroup(layout.createSequentialGroup()

.addGap(46, 46, 46)

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

.addComponent(l1, javax.swing.GroupLayout.PREFERRED_SIZE, 40,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, 40,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(30, 30, 30)

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

.addComponent(l2, javax.swing.GroupLayout.PREFERRED_SIZE, 40,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, 40,


javax.swing.GroupLayout.PREFERRED_SIZE))

.addGap(53, 53, 53)

.addComponent(b1)

.addContainerGap(68, Short.MAX_VALUE))

);

pack();

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

try

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

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

System.out.println("Connection Created");

PreparedStatement ps=con.prepareStatement("Select id from login where id=? and


pwd=?");

ps.setString(1,t1.getText());

ps.setString(2,t2.getText());

ResultSet rs=ps.executeQuery();

if(rs.next())

JOptionPane.showMessageDialog(this,"login Successful...");

else

JOptionPane.showMessageDialog(this,"Login Failed...");

ps.close();

con.close();

catch(Exception e)

System.out.println(e);

public static void main(String args[]) {


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(login.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);

} catch (InstantiationException ex) {

java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);

} catch (IllegalAccessException ex) {

java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);

} catch (javax.swing.UnsupportedLookAndFeelException ex) {

java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);

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

public void run() {

new login().setVisible(true);
}

});

// Variables declaration - do not modify

private javax.swing.JButton b1;

private javax.swing.JLabel l1;

private javax.swing.JLabel l2;

private javax.swing.JTextField t1;

private javax.swing.JPasswordField t2;

Output:-
B5. Write a JDBC program to store images of student in a student (rollno, name,
marks, photo) table as well as retrieve images from table.

Source Code:

import java.io.File;

import java.io.FileInputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

import javax.swing.ImageIcon;

import javax.swing.JOptionPane;

public class StudInfo extends javax.swing.JFrame {

Connection con;

PreparedStatement ps;

ResultSet rs;

Statement st;

public StudInfo() {

initComponents();

try

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

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

System.out.println("Connection Created");

catch(Exception e)
{

System.out.println(e);

@SuppressWarnings("unchecked")

// <editor-fold defaultstate="collapsed" desc="Generated Code">

private void initComponents() {

l1 = new javax.swing.JLabel();

l2 = new javax.swing.JLabel();

l3 = new javax.swing.JLabel();

l4 = new javax.swing.JLabel();

t1 = new javax.swing.JTextField();

t2 = new javax.swing.JTextField();

t3 = new javax.swing.JTextField();

t4 = new javax.swing.JTextField();

b2 = new javax.swing.JButton();

b3 = new javax.swing.JButton();

b1 = new javax.swing.JButton();

jScrollPane1 = new javax.swing.JScrollPane();

l5 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

l1.setText("Roll No:");

l2.setText("Name:");

l3.setText("Marks:");

l4.setText("Photo:");

b2.setText("Clear");

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

b2ActionPerformed(evt);

});

b3.setText("Display");

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

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

b3ActionPerformed(evt);

});

b1.setText("Insert");

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

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

b1ActionPerformed(evt);

});

jScrollPane1.setViewportView(l5);

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

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

.addComponent(l1, javax.swing.GroupLayout.DEFAULT_SIZE, 51,


Short.MAX_VALUE)

.addComponent(l2, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(l3, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

.addComponent(l4, javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))

.addGap(92, 92, 92)

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

.addComponent(t2)

.addComponent(t3)

.addComponent(t4)

.addComponent(t1, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 126,
javax.swing.GroupLayout.PREFERRED_SIZE)))

.addGroup(layout.createSequentialGroup()

.addGap(97, 97, 97)

.addComponent(b1)

.addGap(66, 66, 66)

.addComponent(b2, javax.swing.GroupLayout.PREFERRED_SIZE, 68,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addGap(61, 61, 61)

.addComponent(b3))

.addGroup(layout.createSequentialGroup()
.addGap(117, 117, 117)

.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE,
319, javax.swing.GroupLayout.PREFERRED_SIZE)))

.addContainerGap(144, Short.MAX_VALUE))

);

layout.setVerticalGroup(

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

.addGroup(layout.createSequentialGroup()

.addGap(53, 53, 53)

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

.addComponent(l1)

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

.addGap(18, 18, 18)

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

.addComponent(l2)

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

.addGap(18, 18, 18)

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

.addComponent(l3)

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

.addGap(18, 18, 18)

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

.addComponent(l4)
.addComponent(t4, 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(b1)

.addComponent(b2)

.addComponent(b3))

.addGap(27, 27, 27)

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


Short.MAX_VALUE)

.addContainerGap())

);

pack();

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

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

try

File f=new File(t4.getText());

FileInputStream fis=new FileInputStream(f);

ps=con.prepareStatement("Insert into studinfo value(?,?,?,?)");

ps.setInt(1,Integer.parseInt(t1.getText()));
ps.setString(2,t2.getText());

ps.setInt(3,Integer.parseInt(t3.getText()));

ps.setBinaryStream(4,fis,(int)f.length());

ps.executeUpdate();

JOptionPane.showMessageDialog(this,"Added");

fis.close();

ps.close();

catch(Exception e)

System.out.println(e);

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

try

String s=JOptionPane.showInputDialog(this,"Enter roll no.");

int roll=Integer.parseInt(s);

ps=con.prepareStatement("select photo from studinfo where rollno=?");

ps.setInt(1, roll);

rs=ps.executeQuery();

byte img[]=new byte[6500];

if(rs.next())

img=rs.getBytes("photo");

}
ImageIcon ii=new ImageIcon(img);

l5.setIcon(ii);

catch(Exception e)

System.out.println(e);

public static void main(String args[]) {

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

public void run() {

new StudInfo().setVisible(true);

});

private javax.swing.JButton b1;

private javax.swing.JButton b2;

private javax.swing.JButton b3;

private javax.swing.JScrollPane jScrollPane1;

private javax.swing.JLabel l1;

private javax.swing.JLabel l2;

private javax.swing.JLabel l3;

private javax.swing.JLabel l4;

private javax.swing.JLabel l5;


private javax.swing.JTextField t1;

private javax.swing.JTextField t2;

private javax.swing.JTextField t3;

private javax.swing.JTextField t4;

Output:

Das könnte Ihnen auch gefallen