SlideShare a Scribd company logo
1 of 20
MySQL with Java
• Last day we learnt how to issue MySQL
  queries from MySQL console.
• Today we will learn how to issue MySQL
  queries from a java program.
JDBC
• The interface for accessing databases from
  Java is JDBC. Via JDBC you maintain the
  databases connection, issue database queries
  and updates and receive the results.
• JDBC is an interface independently of any
  database. For each database you require the
  database specific implementation of the JDBC
  driver.
MySQL JDBC driver
• To connect to MySQL from Java you have to
  use the JDBC driver from MySQL. The MySQL
  JDBC driver is called "MySQL Connector/J".
  You should be able to find the latest MySQL
  JDBC driver on this page
  http://dev.mysql.com/downloads/connector/j
• The folder I gave you in the class contains that
  JDBC driver.
• I will show you how to add this JDBC driver
  into a java project using Netbeans.
• Create a new project.
• I have created a new project named
  MySQLConnector.
• Go to the project properties by right click on
  the project in the project window and select
  the menu item ‘Properties’.
• A window will appear :




• Click on the Libraries option
• There is a jar file in the folder I gave you.
  Select the jar file.
• Now we are ready to write a java program that
  can issue MySQL queries.
• Add the following code in your project.




• Here connect object sets up the connection with
  database.
• DriverManager.getConnection("jdbc:mysql://l
  ocalhost/test?","","");
• In this line test is the database name I created.
• In your case it can be different.
• The two empty strings are username and
  password in MySQL.
• Their default values are empty string.
• Add two new lines:




• Statement class allows us to issue mysql
  query.
• You can create a table by the following 2 lines:



• stmt.executeUpdate() can be use to issue any
  create,insert,delete or update query.



• This will insert two rows in the table.
• To select from table




• result is the variable in which we store the
  value returned by the select query.
• It is a ResultSet type of object.
• We can iterate through result by result.next()
  method.
• And we can get the value of a column in the
  current row by
  result.getString(“ColumnName”).
• In this case ColumnName = username and
  password.
• If the value of the column is integer then we
  use result.getInt(“ColumnName”);
• PreparedStatement is like Statement class
  which allows us issue MySQL queries.
• PreparedStatements can use variables and are
  more efficient.
• To use PreparedStatement you need to include
  following code




This will add a new row (“0705003”,”abcde”)
into the user_password table.
• Databases are stored in disks.
• We know that disk access time is significantly
  higher than memory access time.
• Every query needs to access the database in
  the disk.
• So it will be better if we issue many queries at
  the same time by accessing the database
  once.
• For this purpose we issue batches of query to
  the database.




• You can create a query and add it to the batch.
• Here I added 5 queries to the batch by
  addBatch() method.
• At last executeBatch() issued the 5 queries.
• Its good practice to close the Statement,
  PreparedStatement, Connection and ResultSet
  in the end of the program.
Thank You

More Related Content

What's hot

What's hot (19)

Jdbc
JdbcJdbc
Jdbc
 
Database integrate with mule
Database integrate with muleDatabase integrate with mule
Database integrate with mule
 
Stored procedure in mule
Stored procedure in muleStored procedure in mule
Stored procedure in mule
 
JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)JDBC Basics (In 20 Minutes Flat)
JDBC Basics (In 20 Minutes Flat)
 
SQLite Database Tutorial In Android
SQLite Database Tutorial In AndroidSQLite Database Tutorial In Android
SQLite Database Tutorial In Android
 
Lecture 1. java database connectivity
Lecture 1. java database connectivityLecture 1. java database connectivity
Lecture 1. java database connectivity
 
Mysql grand
Mysql grandMysql grand
Mysql grand
 
Getting started into mySQL
Getting started into mySQLGetting started into mySQL
Getting started into mySQL
 
java jdbc connection
java jdbc connectionjava jdbc connection
java jdbc connection
 
JDBC Tutorial
JDBC TutorialJDBC Tutorial
JDBC Tutorial
 
1. java database connectivity (jdbc)
1. java database connectivity (jdbc)1. java database connectivity (jdbc)
1. java database connectivity (jdbc)
 
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBCBI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
BI, Integration, and Apps on Couchbase using Simba ODBC and JDBC
 
DataBase Connectivity
DataBase ConnectivityDataBase Connectivity
DataBase Connectivity
 
Jdbc in servlets
Jdbc in servletsJdbc in servlets
Jdbc in servlets
 
Assignment#10
Assignment#10Assignment#10
Assignment#10
 
What is JDBC
What is JDBCWhat is JDBC
What is JDBC
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Jdbc ppt
Jdbc pptJdbc ppt
Jdbc ppt
 
Jdbc
JdbcJdbc
Jdbc
 

Similar to My sql with java

Similar to My sql with java (20)

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 presentation
Jdbc presentationJdbc presentation
Jdbc presentation
 
Chap3 3 12
Chap3 3 12Chap3 3 12
Chap3 3 12
 
4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt4-INTERDUCATION TO JDBC-2019.ppt
4-INTERDUCATION TO JDBC-2019.ppt
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Java database connectivity
Java database connectivityJava database connectivity
Java database connectivity
 
Chapter6 database connectivity
Chapter6 database connectivityChapter6 database connectivity
Chapter6 database connectivity
 
JDBC Connectivity Model
JDBC Connectivity ModelJDBC Connectivity Model
JDBC Connectivity Model
 
Jdbc
JdbcJdbc
Jdbc
 
Jdbc
JdbcJdbc
Jdbc
 
Grails Connecting to MySQL
Grails Connecting to MySQLGrails Connecting to MySQL
Grails Connecting to MySQL
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
Jdbc
JdbcJdbc
Jdbc
 
Access Data from XPages with the Relational Controls
Access Data from XPages with the Relational ControlsAccess Data from XPages with the Relational Controls
Access Data from XPages with the Relational Controls
 
Jsp project module
Jsp project moduleJsp project module
Jsp project module
 
MSSQL SERVER
MSSQL SERVERMSSQL SERVER
MSSQL SERVER
 
22jdbc
22jdbc22jdbc
22jdbc
 
DjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling DisqusDjangoCon 2010 Scaling Disqus
DjangoCon 2010 Scaling Disqus
 
Php connectivitywithmysql
Php connectivitywithmysqlPhp connectivitywithmysql
Php connectivitywithmysql
 
Java- JDBC- Mazenet Solution
Java- JDBC- Mazenet SolutionJava- JDBC- Mazenet Solution
Java- JDBC- Mazenet Solution
 

Recently uploaded

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Recently uploaded (20)

Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

My sql with java

  • 2. • Last day we learnt how to issue MySQL queries from MySQL console. • Today we will learn how to issue MySQL queries from a java program.
  • 3. JDBC • The interface for accessing databases from Java is JDBC. Via JDBC you maintain the databases connection, issue database queries and updates and receive the results. • JDBC is an interface independently of any database. For each database you require the database specific implementation of the JDBC driver.
  • 4. MySQL JDBC driver • To connect to MySQL from Java you have to use the JDBC driver from MySQL. The MySQL JDBC driver is called "MySQL Connector/J". You should be able to find the latest MySQL JDBC driver on this page http://dev.mysql.com/downloads/connector/j • The folder I gave you in the class contains that JDBC driver.
  • 5. • I will show you how to add this JDBC driver into a java project using Netbeans. • Create a new project. • I have created a new project named MySQLConnector. • Go to the project properties by right click on the project in the project window and select the menu item ‘Properties’.
  • 6. • A window will appear : • Click on the Libraries option
  • 7.
  • 8. • There is a jar file in the folder I gave you. Select the jar file. • Now we are ready to write a java program that can issue MySQL queries.
  • 9. • Add the following code in your project. • Here connect object sets up the connection with database.
  • 10. • DriverManager.getConnection("jdbc:mysql://l ocalhost/test?","",""); • In this line test is the database name I created. • In your case it can be different. • The two empty strings are username and password in MySQL. • Their default values are empty string.
  • 11. • Add two new lines: • Statement class allows us to issue mysql query.
  • 12. • You can create a table by the following 2 lines: • stmt.executeUpdate() can be use to issue any create,insert,delete or update query. • This will insert two rows in the table.
  • 13. • To select from table • result is the variable in which we store the value returned by the select query.
  • 14. • It is a ResultSet type of object. • We can iterate through result by result.next() method. • And we can get the value of a column in the current row by result.getString(“ColumnName”). • In this case ColumnName = username and password. • If the value of the column is integer then we use result.getInt(“ColumnName”);
  • 15. • PreparedStatement is like Statement class which allows us issue MySQL queries. • PreparedStatements can use variables and are more efficient.
  • 16. • To use PreparedStatement you need to include following code This will add a new row (“0705003”,”abcde”) into the user_password table.
  • 17. • Databases are stored in disks. • We know that disk access time is significantly higher than memory access time. • Every query needs to access the database in the disk. • So it will be better if we issue many queries at the same time by accessing the database once.
  • 18. • For this purpose we issue batches of query to the database. • You can create a query and add it to the batch. • Here I added 5 queries to the batch by addBatch() method. • At last executeBatch() issued the 5 queries.
  • 19. • Its good practice to close the Statement, PreparedStatement, Connection and ResultSet in the end of the program.