Write a java program establish a JDBC connection

Write a java program establish a Java Database Connectivity (JDBC)

Aim:

To write a java program establish a JDBC connection, create a table student with properties name, register number, mark1,mark2, mark3. Insert the values into the table by using java.

What is JDBC explain?

Java Database Connectivity (JDBC) is an application program interface (API) specification for connecting programs written in Java to the data in popular databases. The application program interface lets you encode access request statements in Structured Query Language (SQL) that are then passed to the program that manages the database.

It returns the results through a similar interface. JDBC is very similar to the SQL Access Group’s Open Database Connectivity (ODBC) and, with a small “bridge” program, you can use the JDBC interface to access databases through the ODBC interface. For example, you could write a program designed to access many popular database products on a number of operating system platforms.

When accessing a database on a PC running Microsoft’s Windows 2000 and, for example, a Microsoft Access database, your program with JDBC statements would be able to access the Microsoft Access database.JDBC actually has two levels of interface. In addition to the main interface, there is also an API from a JDBC “manager” that in turn communicates with individual database product “drivers,” the JDBC-ODBC bridge if necessary, and a JDBC network driver when the Java program is running in a network environment (that is, accessing a remote database).

When accessing a remote database, JDBC takes advantage of the Internet’s file addressing scheme and a file name looks much like a Web page address (or Uniform Resource Locator). For example, a Java SQL statement might identify the database as:

JDBC Example Program in Java

import java.sql.*;
public class JDBCExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/STUDENTS";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
//STEP 3: Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
//STEP 4: Execute a query
System.out.println("Creating table in given database...");
stmt = conn.createStatement();
String sql = "CREATE TABLE STUDENT " +
"(RollNo INTEGER, " +
" Name VARCHAR(255), " +
" Marks1 INTEGER " +
" Marks2 INTEGER, " +
" Marks3 INTEGER, ")";
stmt.executeUpdate(sql);
System.out.println("Created table in given database...");
String sql1="INSERT INTO STUDENTS (RollNo,Name,Marks1,Marks2,Marks3) values (1,"a",21,23,34)";
stmt.executeUpdate(sql1);
System.out.println("data inserted into table");
}catch(SQLException se){
//Handle errors for JDBC
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
conn.close();
}catch(SQLException se){
}// do nothing
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
se.printStackTrace();
}//end finally try
}//end try
System.out.println("Goodbye!");
}//end main
}//end JDBCExample
Write a java program establish a JDBC connection

FAQ:

What are the 4 types of JDBC drivers?

#1: JDBC-ODBE
#2: Native API
#3: Net Protocol Java Driver
#4: Native Protocol Java Driver

What are the steps in JDBC connection?

In the following way the JDBC Connections occurs:
//STEP 1: Register JDBC driver
Class.forName(“com.mysql.jdbc.Driver”);
//STEP 2: Open a connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);

What is JDBC in Java with example?

JDBC is an application program interface (API) specification for connecting programs written in Java to the data in popular databases. The application program interface lets you encode access request statements in SQL that are then passed to the program that manages the database.
Example: You can check the above mention.

What is ODBC and JDBC?

JDBC (Java Database Connectivity) is an application program interface (API) specification for connecting programs written in Java to the data in popular databases. Open Database Connectivity (ODBC) is an SQL-based Application Programming Interface (API) created by Microsoft that is used by Windows software applications to access databases via SQL.

What is JDBC Connection class?

The Java Database Connectivity (JDBC) Connection class, java.sql.Connection, represents a database connection to a relational database. Before reading or write data from and to a database via JDBC, you just need to open a connection to the database.

Recommended Post:

Find the solution to the salesforce Question and many more

Pramod Kumar Yadav is from Janakpur Dham, Nepal. He was born on December 23, 1994, and has one elder brother and two elder sisters. He completed his education at various schools and colleges in Nepal and completed a degree in Computer Science Engineering from MITS in Andhra Pradesh, India. Pramod has worked as the owner of RC Educational Foundation Pvt Ltd, a teacher, and an Educational Consultant, and is currently working as an Engineer and Digital Marketer.



Leave a Comment