Skip to main content

JDBC Connectivity and More!!


JDBC  defines a universal interface for accessing various relational databases. The main function of JDBC API( java database connectivity application programming interface) is to provide methods for developer to prepare and send SQL statements to database server and fetch the results in database independent manner.
it involes the following steps-
  1. establish a DB connection.
  2. send SQL statement to the DB server.
  3. fetch the result from server and process as required.
classes used in java for DB connectivity.
  • the DriverManager class.
  • the Connection class.
  • the Statement class.
  • the ResultSet class.
Configuring MySql Connector for java connectivity.
to connect your java program to MySql server- the first requirement is  to install MySql Connector/J driver.
the driver may downloaded from http://dev.mysql.com/downloads.
goto netbeans->tools->libraries and add the mysql connector/j  .jar you just downloaded using add jar folder button.
to establish a connection.
  • import the required packages for connection.(Step 1)
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.Statement;
  4. import java.sql.ResultSet;
  • or you may use just one statement in place of importing individual classes-
  1. import java.sql.*;
  • Register the jdbc driver as given below. use one of the driver given below.(Step 2)
  1. java.sql.Driver or
  2. com.mysql.jdbc.Driver
Class.forName(“java.sql.Driver”);
or
Class.forName(“com.mysql.jdbc.Driver”);
  • open a connection(3rd step)
String uid=”root”;
String pwd=”toor”;
String db_url=”jdbc:mysql://localhost:3305/stu”
Connection con=DriverManager.getConnection(db_url,uid,pw);
Statement stmt=con.createStatement();
  • write sql query and execute.(4th step)
String query=”select name, clas, section from student”;
ResultSet rs=stmt.executeQuery(query);
  • Retrieve data from result set (5th step)
String nm=rs.getString(“name”);
String clas=rs.getString(“clas”);
String sec=rs.getString(“section”);
now process as many recordsusing a loop.
and finally cleanup and release the connection
rs.close();
stmt.close()
con.close();

Comments

Popular posts from this blog

  AI in the Indian Classroom: A Catalyst for Transformative Education The burgeoning field of Artificial Intelligence holds immense promise for revolutionizing school education in India, offering innovative solutions to long-standing challenges of scale, diversity, and equitable access. Beyond the hype, AI's exciting use cases are poised to reshape the traditional classroom into a dynamic, personalized, and efficient learning environment, tailored to the unique needs of a diverse student population. One of the most impactful applications of AI is in personalized and adaptive learning. In a country with varying learning speeds and socio-economic backgrounds, AI-powered platforms can transcend the limitations of a one-size-fits-all approach. By analyzing a student's performance data, AI can identify specific learning gaps, strengths, and preferred learning styles. For instance, an AI tutor can create a customized curriculum, offering remedial modules to a student struggling with ...

KVS PGT COMP SC PAPERS

KVS PGT CS PAPER 2014- CLICK HERE TO DOWNLOAD THIS IS TAKEN FROM SOME WHERE ON INTERNET AND ANS MARKED IN THIS PAPER ARE NOT CORRECT. THERE ARE MANY ANSWERS WHICK ARE MARKED WRONG. THE IDEA TO POST IT HERE IS TO GIVE A GLIMPSE OF PAPER TYPE TO PROSPECTIVE CANDIDATES.