How to create jdbc connection in java ?

Jdbc steps to connect to database are explained in this tutorial.

Steps for java database connectivity

In the previous tutorial named as types of jdbc drivers in java  we learned about different jdbc drivers. Now in this tutorial we will learn how to create jdbc conection in java or jdbc connection with mysql or jdbc connection in java with oracle.

There are 6 steps used  to connect a java application with the database through JDBC. 

These steps are as follows:
  •       Create an ODBC Data Source Name.
  •       Register the Driver class or load the driver 
  •       Create  a connection
  •        Create statement and execute query.
  •        Loop the result set until the data is available in table
  •         Free resources or Close connection
All these steps are explained here with their code .

jdbc steps


Step 0 – Create an ODBC DSN (Data Source Name)

Step 1 – Load the driver

Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); //for JDBC-ODBC Bridge

Parameter passed in forName() method to connect with different database are as follows:

To connect with MySql - Class.forName ("org.gjt.mm.mysql.Driver");

Jdbc connection in java with Oracle - Class.forName ("oracle.jdbc.driver.OracleDriver");

Jdbc connection with SqlServer Class.forName ("com.microsoft.jdbc.sqlserver.SQLServerDriver");

Step 2 – Create a connection

Connection conn = DriverManager.getConnection(“jdbcURL”,”username”,”password”);

here jdbcURL syntax is written as jdbc:odbc:dsnName

Where dsnName is the data source name that you have created earlier.

Note :- If you are not going through odbc then you have to provide the host string.

example -

For MySql - jdbc:mysql://127.0.0.1:3306/student


For Oracle - jdbc:oracle:thin:@127.0.0.1:1521:student

For SQL Server –

jdbc:microsoft:sqlserver://127.0.0.1:1433;SelectMethod=Cursor;DatabaseName=student

Step 3 – Create a statement for executing your database transactions.

Three types of statement can be used  Statement, PreparedStatement and CallableStatement. Here we are explaining syntax for each type.


types of statements in jdbc
Statement

Statement st = con.createStatement();
ResultSet rs = st.executeQuery(“select * from emp”);
int i = st.executeUpdate(“insert into college(name) values(‘ITS’)”);

PreparedStatement

PreparedStatement pst = con.preapareStatement(“select * from emp where name = ? and dept = ?”);
pst.setString(1,”sumit”);
pst.setString(2,”it”);
ResultSet rs = st.executeQuery();

CallableStatement

CallableStatement cstmt = con.prepareCall ("{call getDailyTotal (?, ?)}");
cstmt.setString (1, "Mon");
cstmt.registerOutParameter (2, java.sql.Types.INTEGER);
cstmt.executeUpdate();
System.out.println ("Total is " + cstmt.getInt (2));

Step 4 – Loop the ResultSet for your results

while(rs.next()){
System.out.println(“Emp Name is : ”+rs.getString(“name”));
}

Step 5 – Free the resources

rs.close();
st.close();
con.close();

Difference between PreparedStatement and Statement

This is important to note that PreparedStatement is better as compare to Statement .Some differences between  PreparedStatement and Statement are explained here 

 In case of  PreparedStatement query remains in the parsed form.Generally SQL statements can belong to any one of the two categories: static or dynamic.

With a static query the string does not change you create it once then call it over and over again.

In contrast  with a dynamic query you create a new SQL command by concatenating strings and variables after that instantiating a new Statement object and executing that command. 

Either way each time we execute a new command the database must compile the SQL code. This compilation can include various activities such as costly syntax checking, name validation, and pseudo code generation.

From the database performance point of view we should use PreparedStatement. A PreparedStatement is provide the Java encapsulation of a parameterized query in which the SQL statement compiles a single time but it can execute many times. 

If we want to change the query conditions then we employ placeholders (?) within the statement to indicate bind variables. After such bind variables (parameters to the query) are set the query executes.

COMMENTS

BLOGGER
Name

addressing modes types,1,advance-java,2,aktu entrance exam,1,aktu exam schedule,1,ASP,1,bare machine,1,base register and limit register,1,C Programming,12,C Plus Plus,1,C Programming,5,C Programming Questions,1,C programming study material for gate exam,7,Cache Memory,1,CBNST Program,1,Childcare,1,CJ,1,Cloud Computing,1,CN,3,Computer Architecture,2,Computer architecture based questions for gate exam,11,Computer Network,8,Computer Network Study Material,2,Computer network study material for gate,1,Computer Networks,8,Computer networks GATE Questions,2,Computer Science Study Material for Gate,14,computer science study material for gate exam,28,contiguous memory allocation,2,Core Java,3,cyber crime report,1,Cyber crime status,1,cybercrime and security,1,cybercrime examples,1,Data Structure,2,Data Structure Questions,1,Data Transmission Architecture,1,Data Transmission in wsn,1,DBMS,5,dbms question paper,1,DE,1,Digital Electronics,1,DS,4,Dynamic memory allocation in c,1,Electroencephalogram,1,file management in operating system notes,1,Gate 2017,5,Gate 2017 Admit card,1,Gate 2017 Exam Schedule,1,Gate 2017 Syllabus,1,gate cse study material,1,gate practice set,7,gate study material for computer science,12,Gate study material for computer science 2017,1,General,3,HCL Aptitude Test,1,HR Interview Questions,1,HTML,4,Important Date of Gate 2017 Exam,1,Information Security Policy,1,internal and external fragmentation,1,JDBC,2,JS,1,lagrange's interpolation formula,1,lagrange's interpolation formula examples,1,memory fragmentation,1,memory management,1,memory management questions and answer in os,1,Motivational,4,NCER,1,Numerical Techniques Lab,1,OOT,1,Operating System,10,Operating System Gate Questions,1,Operating System Objective Questions,4,Operating System Questions Bank,1,Operating system questions for gate,1,Operating System Study material,2,operating system study material for gate exam,13,Operating system tutorial,2,page swapping,1,paged memory allocation,1,paged memory allocation in operating system,1,Pointer in C,4,Process based question for gate,1,Regression testing,1,relocation in memory management,1,relocation registe,1,relocation register,1,resident monitor,1,resident monitor in operating system,1,routing table,1,Software Engineering,10,Software Engineering baes study material for gate,1,Software Quality Assurance,3,software verification methods,1,Stack,1,Structure in C,1,Study Material for gate Computer Science,7,swapping in memory management,1,swapping in operating system,1,TCS Code Vita,1,TCS Interview Questions,1,Technical Interview,1,Technical Questions from DBMS,1,Thrashing in Operating System,1,Threads concept in operating system,1,Tips to Learn Coding,1,Types of operating system,1,UML,1,Virtualization,1,What is process control block ?,1,what is software testing?,1,Wireless Sensor Network,3,worst fit algorithm for memory allocation,1,XML,2,
ltr
item
Computer Science Junction: How to create jdbc connection in java ?
How to create jdbc connection in java ?
Jdbc steps to connect to database are explained in this tutorial.
https://1.bp.blogspot.com/-nFCc7aHUlm4/XRDcnwSVAoI/AAAAAAAABV0/y_F8pa1jSswns-iT1DV1cGJcDLHGO8xvwCLcBGAs/s400/jdbcsteps.png
https://1.bp.blogspot.com/-nFCc7aHUlm4/XRDcnwSVAoI/AAAAAAAABV0/y_F8pa1jSswns-iT1DV1cGJcDLHGO8xvwCLcBGAs/s72-c/jdbcsteps.png
Computer Science Junction
https://www.computersciencejunction.in/2019/04/jdbc-steps-to-connect-to-database-in-java.html
https://www.computersciencejunction.in/
https://www.computersciencejunction.in/
https://www.computersciencejunction.in/2019/04/jdbc-steps-to-connect-to-database-in-java.html
true
425357657003182083
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share. STEP 2: Click the link you shared to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy