Skip to content

Instantly share code, notes, and snippets.

@SathinduGA
Created September 22, 2019 08:13
Show Gist options
  • Save SathinduGA/47e15803abef637df8068484c5e39a49 to your computer and use it in GitHub Desktop.
Save SathinduGA/47e15803abef637df8068484c5e39a49 to your computer and use it in GitHub Desktop.
Sample JDBC Database Connection
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testui;
//Import required packages
import java.sql.*;
/**
*
* @author sathindu
*/
public class DBOperations {
//JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/database_name";
//Database credentials
static final String USER = "root";
static final String PASS = "password";
void addEmployee(int id, String name, String email) {
Connection con = null;
PreparedStatement pst = null;
try {
//Register JDBC driver
Class.forName(JDBC_DRIVER);
//Open a connection
System.out.println("Connecting to Database...");
con = DriverManager.getConnection(DB_URL, USER, PASS);
//Creating PreparedStatement
System.out.println("Creating PreparedStatement");
String query = "INSERT INTO employee VALUES (?,?,?)";
pst = (PreparedStatement) con.prepareStatement(query);
pst.setInt(1, id);
pst.setString(2, name);
pst.setString(3, email);
//Execute the PreparedStatement
System.out.println("Start Executing PreparedStatement...");
int res = pst.executeUpdate();
System.out.println(res + " Rows Affected...");
} catch (SQLException ex) {
//Handle errors for JDBC
ex.printStackTrace();
} catch (Exception ex) {
//Handle other errors
ex.printStackTrace();
} finally {
//Finally block used to close resources
try {
if (pst != null) {
pst.close();
System.out.println("PreparedStatement Closed...");
}
} catch (SQLException se2) {
}
try {
if (con != null) {
con.close();
System.out.println("Database Connetion Closed...");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
System.out.println("Completed...");
}
void printEmployees() {
Connection con = null;
Statement sta = null;
try {
//Register JDBC driver
Class.forName(JDBC_DRIVER);
//Open a connection
System.out.println("Connecting to Database...");
con = DriverManager.getConnection(DB_URL, USER, PASS);
//Execute a query
System.out.println("Creating Statement...");
sta = con.createStatement();
String sql = "SELECT id, name, email FROM employee";
System.out.println("Executing Statement...");
ResultSet rs = sta.executeQuery(sql);
// Extract data from result set
while (rs.next()) {
//Retrieve by column name
int id = rs.getInt("id");
String name = rs.getString("name");
String email = rs.getString("email");
//Display values
System.out.print("ID: " + id);
System.out.print(", Name: " + name);
System.out.println(", Email: " + email);
}
//Cleaning up environment
rs.close();
} catch (SQLException ex) {
//Handle errors for JDBC
ex.printStackTrace();
} catch (Exception ex) {
//Handle other errors
ex.printStackTrace();
} finally {
//Finally block used to close resources
try {
if (sta != null) {
sta.close();
System.out.println("Statement Closed...");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
if (con != null) {
con.close();
System.out.println("Database Connection Closed...");
}
} catch (SQLException ex) {
ex.printStackTrace();
}
}
System.out.println("Completed...");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment