My sql with java

762 views

Published on

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
762
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
51
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

My sql with java

  1. 1. MySQL with Java
  2. 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. 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. 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. 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. 6. • A window will appear :• Click on the Libraries option
  7. 7. • 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.
  8. 8. • Add the following code in your project.• Here connect object sets up the connection with database.
  9. 9. • 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.
  10. 10. • Add two new lines:• Statement class allows us to issue mysql query.
  11. 11. • 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.
  12. 12. • To select from table• result is the variable in which we store the value returned by the select query.
  13. 13. • 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”);
  14. 14. • PreparedStatement is like Statement class which allows us issue MySQL queries.• PreparedStatements can use variables and are more efficient.
  15. 15. • To use PreparedStatement you need to include following codeThis will add a new row (“0705003”,”abcde”)into the user_password table.
  16. 16. • 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.
  17. 17. • 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.
  18. 18. • Its good practice to close the Statement, PreparedStatement, Connection and ResultSet in the end of the program.
  19. 19. Thank You

×