JDBC is a Java Database Connectivity. The JDBC Connectivity provides API classes and interfaces for connecting the front end in Java application with database connections.
Understand with Example
In this Tutorial we want to describe you a code that helps in understanding JDBC access database. The code illustrates the list of following steps-
Finally the println print the Id and name from the result set obtained from database. lastly all the result set, connection and statement is closed.
JdbcGetInt.java
import java.sql.*; public class JdbcGetInt {
public static void main(String args[]) {
Connection con = null;
Statement st = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/";
String db = "komal";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";
try {
Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
st = con.createStatement();
String sql = "select * from person";
rs = st.executeQuery(sql);
System.out.println("Id\tName");
while (rs.next()) {
System.out.print(rs.getInt(1));
System.out.println("\t"+rs.getString(2));
}
rs.close();
st.close();
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
|
Output
Id Name 4 Komal 5 Ajay 6 Santosh |
|
Recommend the tutorial |
Ask Questions? Discuss: JDBC access database View All Comments
Post your Comment