Download - Java Databse Connectvity- Alex Jose
![Page 1: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/1.jpg)
JAVA DATABASE CONNECTVITY
ALEX JOSE KV PATTOM
SHIFT-I
![Page 2: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/2.jpg)
CONCEPT MAPPING
![Page 3: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/3.jpg)
DATABASE CONNECTIVITY TO MYSQL
![Page 4: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/4.jpg)
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.
![Page 5: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/5.jpg)
PREREQUISITIES FOR CONNECTING TO MYSQL FROM JAVA
![Page 6: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/6.jpg)
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.
![Page 7: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/7.jpg)
DESIGN THE INTERFACE AS PER REQUIREMENT
![Page 8: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/8.jpg)
1. Import the packages required for Database Programming
import 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.
![Page 9: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/9.jpg)
![Page 10: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/10.jpg)
![Page 11: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/11.jpg)
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.
![Page 12: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/12.jpg)
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
![Page 13: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/13.jpg)
Extract data from ResultSet ResultSet 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");.
![Page 14: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/14.jpg)
![Page 15: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/15.jpg)
DISPLAYING DATA IN TABLES
![Page 16: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/16.jpg)
DefaultTableModel dtm=(javax.swing.table.DefaultTableModel)studenttab.getModel(); int rows=dtm.getRowCount(); if(rows>0) { for(int i=0;i<rows;i++) { dtm.removeRow(0); } } try { Class.forName("java.sql.DriverManager"); Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/mydbll“,"root",""); Statement stmt=con.createStatement(); String str="select * from student"; ResultSet rs=stmt.executeQuery(str); while(rs.next()) { int sid=rs.getInt("stdid"); String sname= rs.getString("stdname");
int mark= rs.getInt(“mark"); Object obj[]={sid,sname,mark}; dtm.addRow(obj); } } catch(Exception e) { System.out.println(e); }
Example Program
![Page 17: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/17.jpg)
Executing the select statement having criteria. String str=”select * from student where stdid=”+ t1.getText; String str=”select * from student where stsname=’ ” +
tname.getText() +” ’ ”; To Execute DML commands Int sid=Integer.parseInt(t1.getText()); String sname= tname.getText(); Int mark=Integer.parseInt(tmark.getText()); String str=”insert into student values(“+ sid+” , ’ “ + sname
+” ’ , “+ mark +”)”; Stmt.executeUpdate(str); String str=”delete * from student where stdid=” +
t1.getText() +”)”; Stmt.executeUpdate(str); String str=”update student set mark =mark+5 where
stdname=’ ” + tname.getText() +” ’ )”; Stmt.executeUpdate(str);
![Page 18: Java Databse Connectvity- Alex Jose](https://reader036.vdocument.in/reader036/viewer/2022081604/5883ba371a28ab5c378b55a9/html5/thumbnails/18.jpg)
Thank you…….