Beruflich Dokumente
Kultur Dokumente
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;
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");
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;
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);
try
{
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);
try
ps.setInt(1,Integer.parseInt(s));
ps.executeUpdate();
JOptionPane.showMessageDialog(this,"record deleted");
catch(Exception e)
System.out.println(e);
}
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
java.awt.EventQueue.invokeLater(new Runnable() {
new Data().setVisible(true);
});
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;
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);
catch(Exception e)
System.out.println(e);
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);
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
try
rs.first();
showdata(rs);
catch(Exception e)
System.out.println(e);
try
rs.next();
if(rs.isAfterLast())
rs.first();
}
else
showdata(rs);
catch(Exception e)
System.out.println(e);
try
rs.previous();
if(rs.isBeforeFirst())
rs.last();
else
showdata(rs);
catch(Exception e)
{
System.out.println(e);
try
rs.last();
showdata(rs);
catch(Exception e)
System.out.println(e);
try
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);
java.awt.EventQueue.invokeLater(new Runnable() {
new jdb().setVisible(true);
});
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 login() {
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() {
b1ActionPerformed(evt);
});
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()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(b1)))
.addContainerGap(66, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.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");
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);
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
java.util.logging.Logger.getLogger(login.class.getName()).log(java.util.logging.Level.SEV
ERE, null, ex);
java.awt.EventQueue.invokeLater(new Runnable() {
new login().setVisible(true);
}
});
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;
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")
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();
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() {
b3ActionPerformed(evt);
});
b1.setText("Insert");
b1.addActionListener(new java.awt.event.ActionListener() {
b1ActionPerformed(evt);
});
jScrollPane1.setViewportView(l5);
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(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))
.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()
.addComponent(b1)
.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()
.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))
.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))
.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))
.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))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(b1)
.addComponent(b2)
.addComponent(b3))
.addContainerGap())
);
pack();
try
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);
try
int roll=Integer.parseInt(s);
ps.setInt(1, roll);
rs=ps.executeQuery();
if(rs.next())
img=rs.getBytes("photo");
}
ImageIcon ii=new ImageIcon(img);
l5.setIcon(ii);
catch(Exception e)
System.out.println(e);
java.awt.EventQueue.invokeLater(new Runnable() {
new StudInfo().setVisible(true);
});
Output: