JDBC :- It is a standard API provided by oracle for java applications to interact with different set of databases.
- if we write some code, then data has been lost after compilation of code, if you want to store the data, then you must use JDBC or Database.
Flow of JDBC Connection:-
Java Application <---------> JDBC API <---------> JDBC Driver <---------> Database;
Steps to connect with Database:-
- Load the JDBC Driver :- There two methods for load Driver, first method is Class.forName("driver_name"), Second method is DriverManager.registerDriver(new com.mysql.jdbc.Driver());
- Create a connection :- connection between JDBC API and JDBC Driver. Connection con = DriverManager.getConnection(url,username,password); url = "jdbc:mysql://localhost:3306/database_name", username=root;
- Create a query :- There are three method for query, first is Statement,second is PreparedStatement, third is CallableStatement; PreparedStatement is used for dynamic query. Example:- String q="select * from table1"; Statement stmt=con.createStatement(); ResultSet set=stmt.executeQuery(q);
- Process the Data :- Example:-while(set.next()){int id=set.getInt("column_name");String name = set.getString("column_name);System.out.println(id,name);}
- Close the connection :- con.close();