SlideShare a Scribd company logo
1 of 9
CS3135/CS2135
Object Oriented Programming (Java)
BSCS-3 / MCS-3
Lecture # 17
DB Connectivity
Part1
• You need MySQL database before proceeding.
• After installation, run the PHPMyAdmin from XAMPP Control Panel.
• Type: http://localhost/phpmyadmin in URL of browser.
• Now you have to create database in XAMPP Server. Create it.
• Now it’s time to add JDBC library. Visit following link.
• https://dev.mysql.com/downloads/connector/j/ To download official JDBC driver for MySQL. Unzip it. It is MySQL
JDBC Driver.
• Now configure the above file. For this purpose, put this file into your desired folder, and set ClassPath.
• Suppose we put it in a folder named MySQLConnector in c drive. The path is:
• C:MySQLConnector
• Accordingly, set CLASSPATH variable to C:MySQLConnectormysql-connector-java-5.1.8-bin.jar
• Your jdk path and driver versions of both jdk and driver may change.
• Through CommandPrompt you can compile and run your file:
• javac filename.java
• java –classpath .; C:MySQLConnectormysql-connector-java-5.1.8-bin.jar filename
Database Connectivity – Prerequisites
Instructor: Tanzila Kehkashan
2
JDBC Programming
• Java Database Connectivity (JDBC) is a Java-based data access
technology that defines how a client may access a database. It provides
methods for querying and updating data in a database. The JDBC
classes are contained in the Java package java.sql and javax.sql.
• JDBC (Java Database Connectivity) is the specification of a Java API
that allows Java programs to access DBMSs.
• The JDBC API consists of a set of interfaces and classes written in the
Java programming language.
• Using these standard interfaces and classes, programmers can write
applications that connect to databases, send queries written in
structured query language (SQL), and process the results.
JDBC Programming
Steps
1) Create a
Connection to the
database
1) Create a Statement
2) Query the database
1) Close the result set
2) Close the statement
3) Close the connection
Connect
Query
Process Results
Close
Register 1) Register the
driver
1) Get a ResultSet
2) Assign results to
Java variables
Instructor: Tanzila Kehkashan
3
• A JDBC driver is needed to connect to a database.
• Registering a driver requires the class name of the driver.
• Example) JDBC-ODBC: sun.jdbc.odbc.JdbcOdbcDriver
• Oracle driver: oracle.jdbc.driver.OracleDriver
• MySQL: com.mysql.jdbc.Driver
Loading the driver class
• java.lang.Class (no need to import)
• Class.forName(String);
• It is possible to load several drivers.
• The class DriverManager manages the loaded driver(s).
Example
Execptions by java.lang.ClassNotFoundException
• com.mysql.jdbc.Driver
• This exception comes if your driver is not cofigured. (classpath is wrongly set)
Database Connectivity – STEP 1: Register
Class.forName("com.mysql.jdbc.Driver");
Instructor: Tanzila Kehkashan
4
JDBC URL for a database
• Identifies the database to be connected
• Consists of three-parts:
• <protocol>:<subprotocol>:<subname>
• jdbc:<DBMS>://<HOSTNAME>:<PORT_NUMBER>/YOUR_DATABASE_NAME
java.sql.DriverManager
• You automatically get both JDBC packages java.sql and javax.sql, when you install JDK.
• The DriverManager allows you to connect to a database using the specified JDBC driver, database location,
database name, username and password.
• It returns a Connection object which can then be used to communicate with the database.
• Connection getConnection(String) //url
• Connection getConnection(String, String, String) //url, username, password
• Protocol: JDBC is the
only protocol in JDBC
• Protocol: JDBC is the
only protocol in JDBC
• Subname: indicates the location and name
of the database to be accessed. Syntax is
driver specific
• Subname: indicates the location and name of the database to be
accessed. Syntax is driver specific. DSN name should be used.
• Sub-protocol: identifies
a database driver
Database Connectivity - STEP 2: Connect
Instructor: Tanzila Kehkashan
5
Database Connectivity - STEP 2: Connect
Example
Exceptions
• getConnection method can throw the following exception:
• com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure
• Or
• SQLException: Connection refused or SQLException: Connection timed out
• All the possible causes are:
• IP address or hostname in JDBC URL is wrong.
• Hostname in JDBC URL is not recognized by local DNS server.
• Port number is missing or wrong in JDBC URL.
• DB server is down.
• DB server doesn't accept TCP/IP connections.
• Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
String strDbURL="jdbc:mysql://localhost:3306/testdb";
Connection con=DriverManager.getConnection(strDbURL,"root","");
Instructor: Tanzila Kehkashan
6
java.sql.Connection
• Statement createStatement()
• Statement createStatement(int resultSetType, int resultSetConcurrency)
Example
java.sql.Statement
• ResultSet executeQuery(String) //SQL queries of DML that returns a resultset
• int executeUpdate(String) //SQL DDL queries that returns no resultset (for example; INSERT, UPDATE, DELETE)
SQL Queries
• The WHERE clause is used to extract only those records that fulfill a specified criterion.
• SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
Example
Database Connectivity - STEP 3: Query
Statement stmt = con.createStatement(); //or
//stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
ResultSet rs=stmt.executeQuery("select * from stubio");
Instructor: Tanzila Kehkashan
7
Instructor: Tanzila Kehkashan
8
Instructor: Tanzila Kehkashan
9

More Related Content

Similar to OOP Lecture 17-DB Connectivity-Part1.pptx

Similar to OOP Lecture 17-DB Connectivity-Part1.pptx (20)

Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Jdbc connectivity
Jdbc connectivityJdbc connectivity
Jdbc connectivity
 
PROGRAMMING IN JAVA- unit 5-part II
PROGRAMMING IN JAVA- unit 5-part IIPROGRAMMING IN JAVA- unit 5-part II
PROGRAMMING IN JAVA- unit 5-part II
 
chapter 5 java.pptx
chapter 5  java.pptxchapter 5  java.pptx
chapter 5 java.pptx
 
PROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part IPROGRAMMING IN JAVA -unit 5 -part I
PROGRAMMING IN JAVA -unit 5 -part I
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc introduction
Jdbc introductionJdbc introduction
Jdbc introduction
 
JDBC
JDBCJDBC
JDBC
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Jdbc
JdbcJdbc
Jdbc
 
java database connectivity for java programming
java database connectivity for java programmingjava database connectivity for java programming
java database connectivity for java programming
 
jdbc_unit2.ppt
jdbc_unit2.pptjdbc_unit2.ppt
jdbc_unit2.ppt
 
22jdbc
22jdbc22jdbc
22jdbc
 
Unit 5.pdf
Unit 5.pdfUnit 5.pdf
Unit 5.pdf
 
jdbc.ppt
jdbc.pptjdbc.ppt
jdbc.ppt
 
Unit 5-jdbc2
Unit 5-jdbc2Unit 5-jdbc2
Unit 5-jdbc2
 
Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)Java Database Connectivity (JDBC)
Java Database Connectivity (JDBC)
 
Jdbc connectivity in java
Jdbc connectivity in javaJdbc connectivity in java
Jdbc connectivity in java
 

More from Tanzila Kehkashan

OOP Lecture 25-Network Programming-Part2.pptx
OOP Lecture 25-Network Programming-Part2.pptxOOP Lecture 25-Network Programming-Part2.pptx
OOP Lecture 25-Network Programming-Part2.pptxTanzila Kehkashan
 
OOP Lecture 24-Network Programming-Part1.pptx
OOP Lecture 24-Network Programming-Part1.pptxOOP Lecture 24-Network Programming-Part1.pptx
OOP Lecture 24-Network Programming-Part1.pptxTanzila Kehkashan
 
OOP Lecture 23-JAR Files.pptx
OOP Lecture 23-JAR Files.pptxOOP Lecture 23-JAR Files.pptx
OOP Lecture 23-JAR Files.pptxTanzila Kehkashan
 
OOP Lecture 21-Graphics, Audio.pptx
OOP Lecture 21-Graphics, Audio.pptxOOP Lecture 21-Graphics, Audio.pptx
OOP Lecture 21-Graphics, Audio.pptxTanzila Kehkashan
 
OOP Lecture 20-MultiThreading.pptx
OOP Lecture 20-MultiThreading.pptxOOP Lecture 20-MultiThreading.pptx
OOP Lecture 20-MultiThreading.pptxTanzila Kehkashan
 
OOP Lecture 19-JMenuBar.pptx
OOP Lecture 19-JMenuBar.pptxOOP Lecture 19-JMenuBar.pptx
OOP Lecture 19-JMenuBar.pptxTanzila Kehkashan
 
OOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptxOOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptxTanzila Kehkashan
 
OOP Lecture 16-Math,Timer.pptx
OOP Lecture 16-Math,Timer.pptxOOP Lecture 16-Math,Timer.pptx
OOP Lecture 16-Math,Timer.pptxTanzila Kehkashan
 
OOP Lecture 15-FileHandling,JFileChooser.pptx
OOP Lecture 15-FileHandling,JFileChooser.pptxOOP Lecture 15-FileHandling,JFileChooser.pptx
OOP Lecture 15-FileHandling,JFileChooser.pptxTanzila Kehkashan
 
OOP Lecture 14-ExceptionHandling.pptx
OOP Lecture 14-ExceptionHandling.pptxOOP Lecture 14-ExceptionHandling.pptx
OOP Lecture 14-ExceptionHandling.pptxTanzila Kehkashan
 
OOP Lecture 13-Color,Font,ImageIcon.pptx
OOP Lecture 13-Color,Font,ImageIcon.pptxOOP Lecture 13-Color,Font,ImageIcon.pptx
OOP Lecture 13-Color,Font,ImageIcon.pptxTanzila Kehkashan
 
OOP Lecture 12-EventHandling2.pptx
OOP Lecture 12-EventHandling2.pptxOOP Lecture 12-EventHandling2.pptx
OOP Lecture 12-EventHandling2.pptxTanzila Kehkashan
 
OOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxOOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxTanzila Kehkashan
 
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptxOOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptxTanzila Kehkashan
 
OOP Lecture 9-JComboBox,JList,JPanel.pptx
OOP Lecture 9-JComboBox,JList,JPanel.pptxOOP Lecture 9-JComboBox,JList,JPanel.pptx
OOP Lecture 9-JComboBox,JList,JPanel.pptxTanzila Kehkashan
 
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptx
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptxOOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptx
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptxTanzila Kehkashan
 
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptx
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptxOOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptx
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptxTanzila Kehkashan
 
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptx
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptxOOP Lecture 6-JFrame,JScrollPane,JLabel.pptx
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptxTanzila Kehkashan
 
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptx
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptxOOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptx
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptxTanzila Kehkashan
 

More from Tanzila Kehkashan (20)

OOP Lecture 25-Network Programming-Part2.pptx
OOP Lecture 25-Network Programming-Part2.pptxOOP Lecture 25-Network Programming-Part2.pptx
OOP Lecture 25-Network Programming-Part2.pptx
 
OOP Lecture 24-Network Programming-Part1.pptx
OOP Lecture 24-Network Programming-Part1.pptxOOP Lecture 24-Network Programming-Part1.pptx
OOP Lecture 24-Network Programming-Part1.pptx
 
OOP Lecture 23-JAR Files.pptx
OOP Lecture 23-JAR Files.pptxOOP Lecture 23-JAR Files.pptx
OOP Lecture 23-JAR Files.pptx
 
OOP Lecture 22-JApplet.pptx
OOP Lecture 22-JApplet.pptxOOP Lecture 22-JApplet.pptx
OOP Lecture 22-JApplet.pptx
 
OOP Lecture 21-Graphics, Audio.pptx
OOP Lecture 21-Graphics, Audio.pptxOOP Lecture 21-Graphics, Audio.pptx
OOP Lecture 21-Graphics, Audio.pptx
 
OOP Lecture 20-MultiThreading.pptx
OOP Lecture 20-MultiThreading.pptxOOP Lecture 20-MultiThreading.pptx
OOP Lecture 20-MultiThreading.pptx
 
OOP Lecture 19-JMenuBar.pptx
OOP Lecture 19-JMenuBar.pptxOOP Lecture 19-JMenuBar.pptx
OOP Lecture 19-JMenuBar.pptx
 
OOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptxOOP Lecture 18-DB Connectivity-Part2.pptx
OOP Lecture 18-DB Connectivity-Part2.pptx
 
OOP Lecture 16-Math,Timer.pptx
OOP Lecture 16-Math,Timer.pptxOOP Lecture 16-Math,Timer.pptx
OOP Lecture 16-Math,Timer.pptx
 
OOP Lecture 15-FileHandling,JFileChooser.pptx
OOP Lecture 15-FileHandling,JFileChooser.pptxOOP Lecture 15-FileHandling,JFileChooser.pptx
OOP Lecture 15-FileHandling,JFileChooser.pptx
 
OOP Lecture 14-ExceptionHandling.pptx
OOP Lecture 14-ExceptionHandling.pptxOOP Lecture 14-ExceptionHandling.pptx
OOP Lecture 14-ExceptionHandling.pptx
 
OOP Lecture 13-Color,Font,ImageIcon.pptx
OOP Lecture 13-Color,Font,ImageIcon.pptxOOP Lecture 13-Color,Font,ImageIcon.pptx
OOP Lecture 13-Color,Font,ImageIcon.pptx
 
OOP Lecture 12-EventHandling2.pptx
OOP Lecture 12-EventHandling2.pptxOOP Lecture 12-EventHandling2.pptx
OOP Lecture 12-EventHandling2.pptx
 
OOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxOOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptx
 
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptxOOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
OOP Lecture 10-JTable,JTabbedPane,LayoutManagers.pptx
 
OOP Lecture 9-JComboBox,JList,JPanel.pptx
OOP Lecture 9-JComboBox,JList,JPanel.pptxOOP Lecture 9-JComboBox,JList,JPanel.pptx
OOP Lecture 9-JComboBox,JList,JPanel.pptx
 
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptx
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptxOOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptx
OOP Lecture 8-JCheckBox,JRadioButton,JProgressBar.pptx
 
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptx
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptxOOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptx
OOP Lecture 7-JTextField,JTextArea,JPassword,JButton.pptx
 
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptx
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptxOOP Lecture 6-JFrame,JScrollPane,JLabel.pptx
OOP Lecture 6-JFrame,JScrollPane,JLabel.pptx
 
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptx
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptxOOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptx
OOP Lecture 5-Inheritance,Dialogboxes,Parsing.pptx
 

Recently uploaded

Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样umasea
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWave PLM
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfLivetecs LLC
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfFerryKemperman
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....kzayra69
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)jennyeacort
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noidabntitsolutionsrishis
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 

Recently uploaded (20)

Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
办理学位证(UQ文凭证书)昆士兰大学毕业证成绩单原版一模一样
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
What is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need ItWhat is Fashion PLM and Why Do You Need It
What is Fashion PLM and Why Do You Need It
 
How to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdfHow to Track Employee Performance A Comprehensive Guide.pdf
How to Track Employee Performance A Comprehensive Guide.pdf
 
Introduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdfIntroduction Computer Science - Software Design.pdf
Introduction Computer Science - Software Design.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva2.pdf Ejercicios de programación competitiva
2.pdf Ejercicios de programación competitiva
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....What are the key points to focus on before starting to learn ETL Development....
What are the key points to focus on before starting to learn ETL Development....
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
Call Us🔝>༒+91-9711147426⇛Call In girls karol bagh (Delhi)
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in NoidaBuds n Tech IT Solutions: Top-Notch Web Services in Noida
Buds n Tech IT Solutions: Top-Notch Web Services in Noida
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 

OOP Lecture 17-DB Connectivity-Part1.pptx

  • 1. CS3135/CS2135 Object Oriented Programming (Java) BSCS-3 / MCS-3 Lecture # 17 DB Connectivity Part1
  • 2. • You need MySQL database before proceeding. • After installation, run the PHPMyAdmin from XAMPP Control Panel. • Type: http://localhost/phpmyadmin in URL of browser. • Now you have to create database in XAMPP Server. Create it. • Now it’s time to add JDBC library. Visit following link. • https://dev.mysql.com/downloads/connector/j/ To download official JDBC driver for MySQL. Unzip it. It is MySQL JDBC Driver. • Now configure the above file. For this purpose, put this file into your desired folder, and set ClassPath. • Suppose we put it in a folder named MySQLConnector in c drive. The path is: • C:MySQLConnector • Accordingly, set CLASSPATH variable to C:MySQLConnectormysql-connector-java-5.1.8-bin.jar • Your jdk path and driver versions of both jdk and driver may change. • Through CommandPrompt you can compile and run your file: • javac filename.java • java –classpath .; C:MySQLConnectormysql-connector-java-5.1.8-bin.jar filename Database Connectivity – Prerequisites Instructor: Tanzila Kehkashan 2
  • 3. JDBC Programming • Java Database Connectivity (JDBC) is a Java-based data access technology that defines how a client may access a database. It provides methods for querying and updating data in a database. The JDBC classes are contained in the Java package java.sql and javax.sql. • JDBC (Java Database Connectivity) is the specification of a Java API that allows Java programs to access DBMSs. • The JDBC API consists of a set of interfaces and classes written in the Java programming language. • Using these standard interfaces and classes, programmers can write applications that connect to databases, send queries written in structured query language (SQL), and process the results. JDBC Programming Steps 1) Create a Connection to the database 1) Create a Statement 2) Query the database 1) Close the result set 2) Close the statement 3) Close the connection Connect Query Process Results Close Register 1) Register the driver 1) Get a ResultSet 2) Assign results to Java variables Instructor: Tanzila Kehkashan 3
  • 4. • A JDBC driver is needed to connect to a database. • Registering a driver requires the class name of the driver. • Example) JDBC-ODBC: sun.jdbc.odbc.JdbcOdbcDriver • Oracle driver: oracle.jdbc.driver.OracleDriver • MySQL: com.mysql.jdbc.Driver Loading the driver class • java.lang.Class (no need to import) • Class.forName(String); • It is possible to load several drivers. • The class DriverManager manages the loaded driver(s). Example Execptions by java.lang.ClassNotFoundException • com.mysql.jdbc.Driver • This exception comes if your driver is not cofigured. (classpath is wrongly set) Database Connectivity – STEP 1: Register Class.forName("com.mysql.jdbc.Driver"); Instructor: Tanzila Kehkashan 4
  • 5. JDBC URL for a database • Identifies the database to be connected • Consists of three-parts: • <protocol>:<subprotocol>:<subname> • jdbc:<DBMS>://<HOSTNAME>:<PORT_NUMBER>/YOUR_DATABASE_NAME java.sql.DriverManager • You automatically get both JDBC packages java.sql and javax.sql, when you install JDK. • The DriverManager allows you to connect to a database using the specified JDBC driver, database location, database name, username and password. • It returns a Connection object which can then be used to communicate with the database. • Connection getConnection(String) //url • Connection getConnection(String, String, String) //url, username, password • Protocol: JDBC is the only protocol in JDBC • Protocol: JDBC is the only protocol in JDBC • Subname: indicates the location and name of the database to be accessed. Syntax is driver specific • Subname: indicates the location and name of the database to be accessed. Syntax is driver specific. DSN name should be used. • Sub-protocol: identifies a database driver Database Connectivity - STEP 2: Connect Instructor: Tanzila Kehkashan 5
  • 6. Database Connectivity - STEP 2: Connect Example Exceptions • getConnection method can throw the following exception: • com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure • Or • SQLException: Connection refused or SQLException: Connection timed out • All the possible causes are: • IP address or hostname in JDBC URL is wrong. • Hostname in JDBC URL is not recognized by local DNS server. • Port number is missing or wrong in JDBC URL. • DB server is down. • DB server doesn't accept TCP/IP connections. • Something in between Java and DB is blocking connections, e.g. a firewall or proxy. String strDbURL="jdbc:mysql://localhost:3306/testdb"; Connection con=DriverManager.getConnection(strDbURL,"root",""); Instructor: Tanzila Kehkashan 6
  • 7. java.sql.Connection • Statement createStatement() • Statement createStatement(int resultSetType, int resultSetConcurrency) Example java.sql.Statement • ResultSet executeQuery(String) //SQL queries of DML that returns a resultset • int executeUpdate(String) //SQL DDL queries that returns no resultset (for example; INSERT, UPDATE, DELETE) SQL Queries • The WHERE clause is used to extract only those records that fulfill a specified criterion. • SELECT column_name,column_name FROM table_name WHERE column_name operator value; Example Database Connectivity - STEP 3: Query Statement stmt = con.createStatement(); //or //stmt = con.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE ); ResultSet rs=stmt.executeQuery("select * from stubio"); Instructor: Tanzila Kehkashan 7