Skip to content

Instantly share code, notes, and snippets.

@whizyrel
Last active November 12, 2023 22:09
Show Gist options
  • Select an option

  • Save whizyrel/9cf4da2b670e2d5eaec325afa8d2ee09 to your computer and use it in GitHub Desktop.

Select an option

Save whizyrel/9cf4da2b670e2d5eaec325afa8d2ee09 to your computer and use it in GitHub Desktop.
package org.evryword.jdbcconnection;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
public class JDBCConnection {
public final static String databaseName = "peaceco";
public final static String databaseUser = "root";
public final static String databasePassword = "anythingICanImagine";
public final static String databaseHostname = "localhost";
public final static int databasePort = 3307;
public final static String connectionString = "jdbc:mysql://" + databaseHostname + ":" + databasePort + "/" + databaseName;
public static Connection databaseConnection;
public static void main(String[] args) throws SQLException {
getConnection();
createDatabase(databaseName);
createTable();
final int noOfRecords = populateTable();
System.out.println("Number of records populated => " + noOfRecords);
}
public static int populateTable() {
final int count = 10;
for (int i = 0; i < count; i++) {
final Scanner scanner = new Scanner(System.in);
String name;
String email;
int age;
String location;
String designation;
System.out.print("Input your Name: ");
name = scanner.next();
System.out.println();
System.out.print("Input your Email: ");
email = scanner.next();
System.out.println();
System.out.print("Input your Age: ");
age = scanner.nextInt();
System.out.println();
System.out.print("Input your Location: ");
location = scanner.next();
System.out.println();
System.out.print("Input your Designation: ");
designation = scanner.next();
System.out.println();
String insertEmployeeStatement = """
INSERT INTO employees (name, email, age, location, designation)
VALUES (?, ?, ?, ?, ?);
""";
try (PreparedStatement preparedStatement = databaseConnection.prepareStatement(insertEmployeeStatement)) {
preparedStatement.setString(1, name);
preparedStatement.setString(2, email);
preparedStatement.setInt(3, age);
preparedStatement.setString(4, location);
preparedStatement.setString(5, designation);
preparedStatement.executeUpdate();
} catch (SQLException se) {
throw new RuntimeException(se);
}
}
return count;
}
public static void createTable() {
if (databaseConnection == null) {
throw new RuntimeException("No database Connection available");
}
try (Statement statement = databaseConnection.createStatement()) {
final String createTableStatement = """
CREATE TABLE IF NOT EXISTS employees (
Name TEXT NOT NULL,
Email TEXT NOT NULL,
Age INTEGER NOT NULL,
Location TEXT NOT NULL,
Designation TEXT NOT NULL
)
""";
statement.executeUpdate(createTableStatement);
} catch (SQLException se) {
throw new RuntimeException(se);
}
}
public static void createDatabase(String databaseName) {
if (databaseConnection == null) {
throw new RuntimeException("No database Connection available");
}
String createDatabaseStatement = "CREATE DATABASE IF NOT EXISTS" + " " + databaseName;
try (Statement statement = databaseConnection.createStatement()) {
statement.executeUpdate(createDatabaseStatement);
} catch (SQLException se) {
throw new RuntimeException(se);
}
}
public static Connection getConnection() throws SQLException {
final Properties databaseProperties = new Properties();
databaseProperties.put("user", databaseUser);
databaseProperties.put("password", databasePassword);
Connection databaseConnection = DriverManager.getConnection(connectionString, databaseProperties);
JDBCConnection.databaseConnection = databaseConnection;
return JDBCConnection.databaseConnection;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment