JDBC(Java Database Connectivity)

JDBC :- It is a standard API provided by oracle for java applications to interact with different set of databases.

Why JDBC :-

  1. 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:-

  1. 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());
  2. 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;
  3. 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);
  4. Process the Data :- Example:-while(set.next()){int id=set.getInt("column_name");String name = set.getString("column_name);System.out.println(id,name);}
  5. Close the connection :- con.close();

要查看或添加评论,请登录

社区洞察

其他会员也浏览了