Skip to content

Instantly share code, notes, and snippets.

@theArjun
Created February 13, 2019 11:18
Show Gist options
  • Select an option

  • Save theArjun/a333c4da721efd9877e25464d38cca50 to your computer and use it in GitHub Desktop.

Select an option

Save theArjun/a333c4da721efd9877e25464d38cca50 to your computer and use it in GitHub Desktop.
Implementation of SELECT Query using JDBC
package com.gces.jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Test {
public static void main(String[] args) throws NullPointerException {
String sql = "SELECT * FROM `employees`";
try
{
// Load and register driver.
Class.forName("com.mysql.cj.jdbc.Driver"); // Driver Class Name
// Establish connection between apps and database.
Connection connectionObject = DriverManager.getConnection("jdbc:mysql://localhost:3306/gces","root","");
// Creation of Statement Object
Statement statementObject = connectionObject.createStatement();
// Send and execute SQL Query
ResultSet rs = statementObject.executeQuery(sql);
// Process Result from ResultSet
while(rs.next()) {
System.out.println("ID : " + rs.getInt(1));
System.out.println("Name : " + rs.getString(2));
System.out.println("Address : " + rs.getString(3));
System.out.println("Salary : " + rs.getFloat(4));
}
// Close Connection
connectionObject.close();
}
catch(SQLException exc)
{
exc.printStackTrace();
}
catch(ClassNotFoundException exc)
{
exc.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment