In order to establish a database connection between Java Swing and SQL Server, we have to create a Data Source Name from Control Panel-->Administrative Tool-->Data Source(ODBC). Here in following example, I have used a Data Source named "SQLDSNSource".
In this example, I have tried to navigate the records available in a SQL table, adding new record, updating record and deleting record.
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class DBConnectivity extends JFrame implements ActionListener
{
Container container;
JLabel lblRoll=new JLabel("Roll");
JLabel lblName=new JLabel("Students Name");
JLabel lblAddress=new JLabel("Address");
JLabel lblBatch=new JLabel("Batch");
JButton btnNext=new JButton("Next >>");
JButton btnPrior=new JButton("<< Previous");
JTextField txtRoll=new JTextField(8);
JTextField txtName=new JTextField(20);
JTextArea txtAddress=new JTextArea(3,20);
JTextField txtBatch=new JTextField(10);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JButton btnNew=new JButton("New Record");
JButton btnSave=new JButton("Save Record");
JButton btnUpdate=new JButton("Update");
JButton btnDelete=new JButton("Delete");
ResultSet rs;
Connection con;
Statement st;
DBConnectivity(String title)
{
super(title);
container=getContentPane();
container.setLayout(new GridLayout(6,2));
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc:odbc:SQLDSNSource");
st=con.createStatement(rs.TYPE_SCROLL_SENSITIVE, rs.CONCUR_UPDATABLE);
rs=st.executeQuery("select * from student");
rs.next();
txtRoll.setText(rs.getString("Roll"));
txtName.setText(rs.getString("Name"));
txtAddress.setText(rs.getString("Address"));
txtBatch.setText(rs.getString("Batch"));
}
catch(Exception e)
{
}
txtRoll.setEnabled(false);
btnSave.setEnabled(false);
container.add(lblRoll);
container.add(txtRoll);
container.add(lblName);
container.add(txtName);
container.add(lblAddress);
container.add(txtAddress);
container.add(lblBatch);
container.add(txtBatch);
p1.add(btnNew);
p1.add(btnSave);
p2.add(btnUpdate);
p2.add(btnDelete);
btnPrior.addActionListener(this);
btnNext.addActionListener(this);
btnNew.addActionListener(this);
btnSave.addActionListener(this);
btnUpdate.addActionListener(this);
btnDelete.addActionListener(this);
container.add(btnPrior);
container.add(btnNext);
container.add(p1);
container.add(p2);
pack();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
try
{
if (e.getSource()==btnNext)
{
rs.next();
}
if (e.getSource()==btnPrior)
{
rs.previous();
}
if (e.getSource()==btnNew)
{
txtRoll.setText("");
txtRoll.setEnabled(true);
txtName.setText("");
txtAddress.setText("");
txtBatch.setText("");
btnNew.setEnabled(false);
btnSave.setEnabled(true);
btnUpdate.setEnabled(false);
btnDelete.setEnabled(false);
}
if (e.getSource()==btnSave)
{
String saveCom="insert into student values('"+txtRoll.getText()+"','"+txtName.getText()+"','"+txtAddress.getText()+"','"+txtBatch.getText()+"')";
st.executeUpdate(saveCom);
JOptionPane.showMessageDialog(this, "New Record has been saved");
txtRoll.setEnabled(false);
btnNew.setEnabled(true);
btnSave.setEnabled(false);
btnUpdate.setEnabled(true);
btnDelete.setEnabled(true);
}
if (e.getSource()==btnUpdate)
{
String updateCom="update student set name='"+txtName.getText()+"',address='"+txtAddress.getText()+"',batch='"+txtBatch.getText()+"' where roll='"+txtRoll.getText()+"'";
st.executeUpdate(updateCom);
JOptionPane.showMessageDialog(this, "Record has been updated");
st.close();
}
if (e.getSource()==btnDelete)
{
String deleteCom="delete from student where roll='"+txtRoll.getText()+"'";
st.executeUpdate(deleteCom);
JOptionPane.showMessageDialog(this, "Record has been deleted");
st.close();
}
txtRoll.setText(rs.getString("Roll"));
txtName.setText(rs.getString("Name"));
txtAddress.setText(rs.getString("Address"));
txtBatch.setText(rs.getString("Batch"));
}
catch(Exception ex)
{
System.out.println(ex);
}
}
public static void main(String args[])
{
new DBConnectivity("Database Connectivity");
}
}
0 comments:
Post a Comment