JDBC Presentation

21
JDBC Database Programming Presented By: Mr. Angelo P. Del Rosario

Transcript of JDBC Presentation

JDBC Database Programming

JDBCDatabase ProgrammingPresented By: Mr. Angelo P. Del RosarioJava Database Connectivity (JDBC)JDBCis a Java database connectivity technology (Java Standard Edition platform) fromOracle Corporation. This technology is an API (Application Programming Interface)for theJava programming languagethat defines how a client may access adatabase. It provides methods for querying and updating data in a database.

Creating JDBC Application:There are following six steps involved in building a JDBC application:Import the packages .Requires that you include the packages containing the JDBC classes needed for database programming. Most often, usingimport java.sql.*will suffice.Register the JDBC driver .Requires that you initialize a driver so you can open a communications channel with the database.Open a connection .Requires using theDriverManager.getConnection()method to create a Connection object, which represents a physical connection with the database.

Creating JDBC Application: cont..Execute a query .Requires using an object of type Statement for building and submitting an SQL statement to the database.Extract data from result set .Requires that you use the appropriateResultSet.getXXX()method to retrieve the data from the result set.Clean up the environment .Requires explicitly closing all database resources versus relying on the JVM's garbage collection.

NetBeans CRUD ApplicationIntegrating CRUD Functionality.

Getting StartedBefore we start we need to install the following requirements:MySQL ServerJDK (Java Development Kit)NetBeansMySQL Connector/J Driverhttp://www.mysql.com/products/connector/

Creating the DatabaseDatabase Name: studentdbTable Name: studentField Name:

No.NameType1studnoint2studlnamevarchar(30)3studfnamevarchar(30)4studageint

Designing the User Interface

Creating a class and method for database connection

Importing the class sql classimport java.sql.*;

Declaring the VariablesConnection conn = null;

Creating a method for the connectionpublic static Connection ConnectDB(){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/dbase",userID",pword"); JOptionPane.showMessageDialog(null,"Successfully Connected To Database Server"); return conn; }catch(Exception e){ JOptionPane.showMessageDialog(null, e); return null; }

Creating a JFrame Form

JFrame Form Design

Declaring the Variables in JFrame Form Connection conn = null; ResultSet rs = null; PreparedStatement pst = null; String strSQL="";

theexecuteQuerymethod returns all the records in something called aResultSet

next moves the cursor to the next row in your table

PreparedStatementobject for sending SQL statements to the database.15Open the database connectionconn = MySQLConnection.ConnectDB();Integrating CRUD Functionality: Create strSQL ="INSERT INTO student VALUES(?,?,?,?)"; try{ pst = conn.prepareStatement(strSQL); pst.setString(1, txtsno.getText()); pst.setString(2, txtlname.getText()); pst.setString(3, txtfname.getText()); pst.setString(4, txtage.getText()); pst.execute(); JOptionPane.showMessageDialog(null,"Record Saved"); clearField(); }catch(Exception e){ JOptionPane.showMessageDialog(null,e); }

Integrating CRUD Functionality: ReadstrSQL = "SELECT * FROM student WHERE studno=?";try{ pst = conn.prepareStatement(strSQL); pst.setString(1, txtsearch.getText()); rs = pst.executeQuery(); if(rs.next()) { JOptionPane.showMessageDialog(null,"Record Found"); txtsno.setText(rs.getString("studno")); txtlname.setText(rs.getString("lastname")); txtfname.setText(rs.getString("firstname")); txtage.setText(rs.getString("age")); }else{ JOptionPane.showMessageDialog(null,"Record Not Found"); } }catch(Exception e){ JOptionPane.showMessageDialog(null,e);}

Integrating CRUD Functionality: Update strSQL = "UPDATE student SET lastname=?,firstname=?,age=? WHERE studno=?"; try{ pst = conn.prepareStatement(strSQL); pst.setString(1, txtlname.getText()); pst.setString(2, txtfname.getText()); pst.setString(3, txtage.getText()); pst.setString(4, txtsno.getText()); pst.executeUpdate(); JOptionPane.showMessageDialog(null,"Record Updated"); clearField(); }catch(Exception e){ JOptionPane.showMessageDialog(null,e); }

Integrating CRUD Functionality: Delete strSQL = "DELETE FROM student WHERE studno=?"; try{ pst = conn.prepareStatement(strSQL); pst.setString(1, txtsno.getText()); pst.execute(); JOptionPane.showMessageDialog(null, "Record Deleted"); clearField(); }catch(Exception e){ JOptionPane.showMessageDialog(null,e); }

THANK YOU..