2/1/19

JDBC - Java Database Connectivity Coding Reference Part - 1 | JAVA

Fisrt of all, you have to download mysql workBench, but you already have Xampp or Wamp Server that had mysql also. that sitivation you don't want to download mysql workBench. you can also run mysql of Xampp or Wamp server. If you don't have Xampp / Wamp server or mysql workBench, you have to download mysql workBench or Xampp / Wamp server.

After you installed your database application, you have to add to connect your connector file for java database connectivity -JDBC
1. Download the Connector file.
https://dev.mysql.com/downloads/connector/j/
(Please Select your platform. You can select platform indepentent option also.)

2. Add the connector in your java project
Eclicps IDE - *Select the "Add External .jar file" option and Select the file.
NetBeans IDE - *Select the "Add .jar file" option and Select the file.

Six Steps to Using JDBC
1. Load the JDBC Driver
2. Establish the Database Connection
3. Create a Statement Object
4. Execute a Query
5. Process the Results
6. Close the Connection

Resource file: Database File (For Practical) 

1. Load the JDBC Driver
Class.forName("com.mysql.jdbc.Driver");
-or-
Class.forName("com.mysql.cj.jdbc.Driver");


2. Establish the Database Connection
final String DB_URL = "jdbc:mysql://localhost/DatabaseName";
final String USER = "root";
final String Pass = "";       
Connection conn = DriverManager.getConnection(DB_URL,USER,Pass);

-or-

final String DB_URL = "jdbc:mysql://localhost:3306/DatabaseName";
final String USER = "root";
final String Pass = "";       
Connection conn = DriverManager.getConnection(DB_URL,USER,Pass);

Note: You have to mention to change your database name.


3. Create a Statement Object
Statement stmt = con.createStatement();


4. Execute a Query
ResultSet res =  stmt.executeQuery("SELECT * FROM data WHERE name LIKE 'N%'");



5. Process the Results           
while(res.next()) {
    String name=res.getString("name");
    System.out.println(name);
}



6. Close the Connection
stmt.close();
res.close();
conn.close();



Examle JavaDatabase project file: 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DatabaseCon {

public static void main(String[] args) {

DatabaseCon o = new DatabaseCon();
o.createCon();

}

void createCon() {

String url = "jdbc:mysql://localhost:3306/crud";
String username="root";
String pwd="";

try {

//Load the JDBC Driver
Class.forName("com.mysql.cj.jdbc.Driver");

//Establish the Database Connection
Connection con = DriverManager.getConnection(url,username,pwd);

//Create a Statement Object
Statement stmt = con.createStatement();

//Execute a Query
ResultSet res =  stmt.executeQuery("SELECT * FROM data WHERE name LIKE 'N%'");

//Process the Results
while(res.next()) {
String name=res.getString("name");
System.out.println(name);

}
 

 //Close the Connection
stmt.close();
res.close(); 
conn.close();

// This is just print status of database connection or not. if it's not connection that will occured exception 
System.out.println();
System.out.println("Database Connected!");

}
catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();

catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

No comments:

Post a Comment

About

Hi, I'm Najathi.
I've started entrepreneurial company, Twin Brothers.Inc.
One is self-funded & the other is venture backed. I also send a weekly. where I share relevent, curated links.

Every Week I Publish a short post on writing, publishing, or content of IT Related

Contact Form

Name

Email *

Message *