6 JAVA MODULE5 Accessing Database
6 JAVA MODULE5 Accessing Database
MODULE-5
Accessing Databases using JDBC
connectivity
JDBC
JDBC Application
JDBC Drivers
Java JDBC
program driver
connectivity for Oracle
data processing
utilities driver
for Sybase
jdbc-odbc odbc
bridge driver
JDBC APIs
•JDBC is implemented via classes in the java.sql package
Interfaces Classes
CallableStatement DriverManager
Connection Date
DatabaseMetaData Time
Driver Types
PreparedStatement
ResultSet
ResultSetMetaData
Statement
Driver Manager
• DriverManager
– Loads, chooses drivers
• Driver
– connects to actual database
• Connection
– a series of SQL statements to and from the DB
• Statement
– a single SQL statement
• ResultSet
– the records returned from a Statement
Driver Manager
–Loads, chooses drivers
DriverManager
Driver
–connects to actual database a series of SQL statements to
and from the DB
Connection
Statement
a single SQL statement
ResultSet
the records returned from a Statement
Driver Manager
SQL data
jdbc:subprotocol:source
• Each driver has its own subprotocol
• Each subprotocol has its own syntax for the source
• EX:
jdbc:odbc:DataSource
– jdbc:oracle:thin:@localhost:1521:xe
jdbc:mysql://host[:port]/database
– jdbc:mysql://localhost:3306/stuDB
Driver Manager
• MySQL installation:
https://www.apachefriends.org/download.html
Video link to install XAMPP
https://www.youtube.com/watch?v=N6ENnaRotmo
SQL
Insert Values
CHAR String
VARCHAR String
LONGVARCHAR String
NUMERIC java.Math.BigDecimal
DECIMAL java.Math.BigDecimal
BIT boolean
TINYINT int
SMALLINT int
INTEGER int
BIGINT long
REAL float
FLOAT double
DOUBLE double
BINARY byte[]
VARBINARY byte[]
DATE java.sql.Date
TIME java.sql.Time
TIMESTAMP java.sql.Timestamp
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException
{
try { Display the records based on the user input
Connection con=null;
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee1","root","");
System.out.println("Connection Succesfull");
Statement s=con.createStatement(); // insert a record into table
catch(SQLException e)
Scanner inp=new Scanner(System.in); {
System.out.println("Enter the Name: "); System.out.println(e.getMessage());
String name=inp.next(); }
String sql="select * from empinfo where empname=?;"; }
//to scroll the cursor back and forth
PreparedStatement ps=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
ps.setString(1,name);
ResultSet rs=ps.executeQuery();
if(rs.next()==false){
System.out.println("Record not found"); }
else{
rs.previous();
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getBigDecimal(2)+" "+rs.getDate(3)+" "+rs.getInt(4)); }
}
ps.close();
con.close();
}
Example : Connecting Javafx with Database
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.*; Demo of connecting Javafx
import javafx.scene.layout.*;
import javafx.event.*; with Database
import java.sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
class Employee
{
private String name;
private String password;
public Employee(String name, String password)
{
this.name = name;
this.password = password;
}
public void insertstudent() throws SQLException,ClassNotFoundException,InstantiationException, IllegalAccessException
{
Connection con=null;//Establish a connection
Class.forName("com.mysql.jdbc.Driver").newInstance();//loads the driver
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/cse1007","root","");
String sql="insert into login values(?,?);";
PreparedStatement stmt=con.prepareStatement(sql);
stmt.setString(1,name);
stmt.setString(2,password);
stmt.execute();
}
}
bSubmit.setOnAction(new EventHandler<ActionEvent>()
class MyloginForm
{
{
@Override
Label lname; Demo of connecting Javafx public void handle(ActionEvent event)
TextField tname; with Database {
Label lpwd;
boolean flag=true;
//TextField tpwd;
if(tpwd.getText().isEmpty())
PasswordField tpwd;
{
Button bSubmit;
lerrormsg.setText("Enter your name");
Label lerrormsg;
flag=false;
public void myform(Stage primaryStage)
}
{
if(tname.getText().isEmpty())
lname=new Label("Enter your name");
{
tname=new TextField();
lerrormsg.setText("Enter your pssword");
lpwd=new Label ("Ener your password");
flag=false;
tpwd=new PasswordField();
}
lerrormsg=new Label();
Employee e=new Employee(tname.getText(),tpwd.getText());
bSubmit=new Button("Submit");
try
GridPane root=new GridPane();
{
root.add(lname,0,0);
e.insertstudent();
root.add(lpwd,0,2);
if(flag)
root.add(tname,1,0);
lerrormsg.setText("Record inserted Successfully");
root.add(tpwd,1,2);
else
root.add(bSubmit,0,3);
lerrormsg.setText("Record Not inserted Successfully");
root.add(lerrormsg,0,4);
}
Scene sc=new Scene(root,500,300);
catch (Exception ex)
primaryStage.setScene(sc);
{
primaryStage.show();
System.out.println(ex.getMessage());
}
}
});
}}
Demo of connecting Javafx with Database
}
Demo of connecting
Javafx with Database
OUTPUT
import java.sql.*;
import java.sql.Driver;
Example
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;