Click here to load reader

Java Databse Connectvity- Alex Jose

  • View
    62

  • Download
    1

Embed Size (px)

Text of Java Databse Connectvity- Alex Jose

JAVA DATABSE CONNECTVITY

JAVA DATABASE CONNECTVITYALEX JOSE KV PATTOM SHIFT-I

CONCEPT MAPPING

DATABASE CONNECTIVITY TO MYSQL

DriverManager Class : It is the class used to load JDBC Driver needed to access particular database.

Connection class : It manages the connection and communication to the database.

Statement Class: It contains the string that are submitted to the database for execution. And it will execute the SQL statement.

ResultSet Class : If the statement class contains select statement, then during the execution it will return selected rows from the database are assigned to ResultSet and it contains the methods to move through the rows and get the values.

Classes used for database connectivity.

PREREQUISITIES FOR CONNECTING TO MYSQL FROM JAVA

Step1. import packages required for Database Programming.Step2. Register the JDBC Driver.Open a connection.Execute a Query.Extract data from the ResultSet.Cleanup the environment.

Steps to Creating Database connectivity Applications.

DESIGN THE INTERFACE AS PER REQUIREMENT

1. Import the packages required for Database Programmingimport java.sql.*;

2. Register the JDBC Driver with Java Program. Class.forName(java.sql.DriverManager");

3. Open the connection.This requires using the DriverManager.getConnection() method to create a connection object, which represents the physical connection with the database. To connect to the database, we need complete URL to the database with userid and password of MySQL.

4. Execute a query.We need to create an object of type Statement or PreparedStatement for building and submitting an SQL statement to the database using the createStatement() function of Connection class.in the Statement class executeQuery() method is needed to execute select statement in SQL and executeUpdate() method is to execute DML statements.if the SQL command is Select statement we need a class called ResultSet to store and process the records.The Result set refers to a logical set of records that are fetched from the database by executing a query and made available to the application program.

next()moves the cursor forward one first()to move to the firts row.last()move the cursor to the lat lasr row.relative(int rows)moves the cursor relative to the current position.absolute(int rno)position the cursor on the rno th rows in the ResultSet. getRow()to get the current cursor position.

ResultSet cursorWhen the ResultSet is first created, the cursor is positioned before the first row.

ResultSet methods

Extract data from ResultSetResultSet class has several methods to get the values from the columns from the rows pointed by the cursor.getInt("FieldName");getString("FieldName"); getString(fieldNumber);getFloat("FieldName");getDate("FieldName");getLong("FieldName");.

DISPLAYING DATA IN TABLES

DefaultTableModel dtm=(javax.swing.table.DefaultTableModel)studenttab.getModel(); int rows=dtm.getRowCount(); if(rows>0) { for(int i=0;i