Java JDBC

 



Blue : Interface
Orange : Class


Required downloads before start to work with java JDBC.
  1. Java any latest version (set classpath)
  2. any Database here using MySql
  3. respective JDBC driver for Database (.jar file)
  4. Code-Editor (Eclipse)


create path for drive 
separate different class paths  with semi colon ;



set path for JDBC driver
to check weather driver path set or not.



 > Steps to Connect with Database


Statement : normal query( SELECT,INSERT,UPDATE,DELETE)
PreparedStatement : Dynamic query
CallableStatement : execute function or method in database

column name as parameter



> Implementation by creating table (Normal Statement query) :



> Implementation by Inserting dynamic values (Prepared Statement query) :
 
here with help of PreparedStatement interface we will call method prepareStatement().
then executeupdate() without any parameter

Similarly we can insert data by taking from user dynamically at run time.


> Inserting Images(less size i.e less then 65kb) in database:

image here is of type blob : Binary Large Object


here we have taken image as FileInputStream Object as we know it takes input in form of bytes.
and here we have used image name since both program and image were in same folder if not give full path.

here setBinaryStream() parameters are :
  • 1 for firstposition
  • fis for value to insert at first position
  • fis.available() here is used for bytes available in stream

> for storing images of large size we will have to change type to longblob:


we can also create our connection in new class so whenever we need to connect to our database we can directly call method from that class



so here we have called connection method from above to create connection.
Here swing component of java which provide graphical user interface so here we by JFileChooser which is from Swing we can select file from system in GUI form.

jfc.showOpenDialog(null): here null is for centering the GUI window.
Similarly JOptionPane is GUI for showing success msg to user.


---------------------------------------------------------------------------------------------------------------------
Update statement 
















Comments

Popular posts from this blog

Durgesh - Exam Portal Project using Spring Boot and Angular