Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 11 Object Oriented Programming Using Java

Similar presentations


Presentation on theme: "Lecture 11 Object Oriented Programming Using Java"— Presentation transcript:

1 Lecture 11 Object Oriented Programming Using Java
By Rashid Ahmad Department of Computer Science University of Peshawar

2 Java Database Connectivity
Introductory concepts to databases. A shared collection of logically related data, designed to meet the Information needs of an organization. Examples of Data Storages Microsoft Access Foxpro Microsoft SQL SERVER MySQL Oracle

3 How to connect to data sources
The answer is ODBC Open Database Connectivity, or ODBC is a standard for accessing Data. It was designed to allow the programmers to use a common Set of routines to access data stored in database. Application What about Excel, Word, Outlook Express Mail How ODBC Works ODBC Access SQL Server Oracle

4 Universal Data Access What is ADO Definition for a data store
Any persisted collection of information is a data store. OLEDB (Object linking and Embedded Databases) It allows access to a much broader range of data stores. Application What is ADO OLE-DB ODBC ODBC Data Access SQL Server Oracle MySQL Excel

5 Connecting Java to Microsoft Access
First Step is to create a DSN (Data Source Name) A data source name (DSN) is a data structure that contains the Information about a specific database that an Open Database Connectivity ( ODBC) driver needs in order to connect to it. Kinds of a DSNs User DSNs System DSNs File DSNs How to Create a DSN

6 Steps to connect to a database
Loading the database driver Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connecting to the DSN connection=DriverManager.getConnection("jdbc:odbc:sampledb"); Building a statement statement = connection.createStatement(); Executing a query boolean foundresults = statement.execute(v_Query); resultset = statement.getResultSet(); What is a ResultSet and Command Object

7 A simple program on connecting an Access Database
package jdbc; import java.sql.*; import java.util.*; public class Main { private Connection connection; private Statement statement; private ResultSet resultset; public Main() { } public static void main(String[] args) { Main obj = new Main(); obj.fnPickData();

8 A simple program on connecting an Access Database
public void fnPickData(){ int v_Counter = 0; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); connection= DriverManager.getConnection ("jdbc:odbc:sampledb"); String v_Query = "Select * FROM Personal"; statement = connection.createStatement(); boolean foundresults = statement.execute(v_Query); resultset = statement.getResultSet();

9 while(resultset.next()){
System.out.println("User ID: " + resultset.getObject("ID").toString()); System.out.println("User Name: " + resultset.getObject("Name").toString()); System.out.println(); ++v_Counter; } connection.close(); if (v_Counter == 0){ System.out.println("No record found"); } } catch(Exception ex){ System.out.println(ex.getMessage()); } } }

10

11 A Full Fledge JDBC Application
package jdbc; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.sql.*; import java.awt.*; import java.awt.Event.*; import javax.swing.*;

12 public class PersonalForm extends JFrame
implements ActionListener { private Connection connection; private Statement statement; private ResultSet resultset; private JButton btninsert,btnsearch,btndelete,btnupdate; private JLabel lblname,lblid; private JTextField txtname,txtid; private Container content; public String v_StudentID,v_Qry = "";

13 public PersonalForm() {
super("Database Access"); content = getContentPane(); content.setLayout(new FlowLayout()); lblid = new JLabel("Student ID"); content.add(lblid); txtid = new JTextField("Student ID Here"); txtid.setEditable(false); content.add(txtid); lblname = new JLabel("Student Name"); content.add(lblname); txtname = new JTextField("Enter User Name"); //txtname.setSize(50,10); content.add(txtname);

14 btninsert = new JButton("Insert");
content.add(btninsert); btnsearch = new JButton("Search"); content.add(btnsearch); btnupdate = new JButton("Update"); content.add(btnupdate); btndelete = new JButton("Delete"); content.add(btndelete); btninsert.addActionListener(this); btnsearch.addActionListener(this); btndelete.addActionListener(this); btnupdate.addActionListener(this); setSize(200,200); setVisible(true); }

15 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
public void fnTransactions(String v_Query,String v_Operation){ int v_Counter = 0; try{ Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); connection=DriverManager.getConnection("jdbc:odbc:sampledb"); statement = connection.createStatement(); if(v_Operation != "search") { int foundresults = statement.executeUpdate(v_Query); if (foundresults > 0) JOptionPane.showMessageDialog (null,"Your requested changes made successfully"); txtid.setText(""); txtname.setText(""); }

16 else if(v_Operation == "search")
{ statement.execute(v_Query); resultset = statement.getResultSet(); while(resultset.next()){ txtid.setText(resultset.getObject("ID").toString()); txtname.setText(resultset.getObject("Name").toString()); ++v_Counter; } if(v_Counter == 0) JOptionPane.showMessageDialog(null,"Record Not Found"); } } connection.close(); } catch(Exception ex){ JOptionPane.showMessageDialog(null,ex.getMessage()); } }

17 public void actionPerformed(ActionEvent ev){
if(ev.getSource() == btninsert){ v_Qry = "INSERT INTO Personal(Name) VALUES('" + txtname.getText() + "')"; fnTransactions(v_Qry,"insert"); } if(ev.getSource() == btnsearch){ v_StudentID = JOptionPane.showInputDialog("Enter ID to Search"); v_Qry = "SELECT * FROM Personal WHERE ID = " + v_StudentID; fnTransactions(v_Qry,"search"); } if(ev.getSource() == btnupdate){ v_StudentID = JOptionPane.showInputDialog("Enter Student ID to delete"); v_Qry = "UPDATE Personal SET Name = '" + txtname.getText() + "'" + "WHERE StudentID = '" + v_StudentID + "'"; fnTransactions(v_Qry,"update"); } if(ev.getSource() == btndelete){ v_Qry = "DELETE FROM Personal WHERE ID = " + v_StudentID; fnTransactions(v_Qry,"delete"); } } }

18 Output of the Program


Download ppt "Lecture 11 Object Oriented Programming Using Java"

Similar presentations


Ads by Google