Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class DBUtil {
- // Connecting to database.
- public static Connection getConnection() throws SQLException {
- String url = "jdbc:mysql://localhost:3306/database";
- String user = "root";
- String pass = "admin";
- Connection connection = DriverManager.getConnection(url, user, pass);
- return connection;
- }
- // Close database connection.
- public static void closeConnection(Connection con) {
- try {
- if(con != null) {
- con.close();
- System.out.println("Database Connection Closed!");
- }
- }catch(Exception e) {
- e.printStackTrace();
- }
- }
- // Close Prepared Statement.
- public static void closePrepStatement(PreparedStatement pStmt) {
- try {
- if(pStmt != null) {
- pStmt.close();
- System.out.println("Prepared Statemet Closed");
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- // Close Result Set.
- public static void closeResSet(ResultSet rSet) {
- try{
- if(rSet != null) {
- rSet.close();
- System.out.println("Result Set Closed");
- }
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.util.Scanner;
- // SQL variables defined
- public class Source {
- DBUtil dbConnection = new DBUtil();
- Scanner input = new Scanner(System.in);
- Connection connection = null;
- PreparedStatement pStatement = null;
- ResultSet rSet = null;
- // Constructor
- public Source() {
- getInput();
- displayData();
- }
- // Getting user input and saving to database
- private void getInput() {
- System.out.println("What is your name?");
- String fName = input.nextLine();
- System.out.println("What is your second name?");
- String sName = input.nextLine();
- System.out.println("What is your proffesion?");
- String userProff = input.nextLine();
- try {
- connection = DBUtil.getConnection();
- String sqlQuery = "INSERT INTO employees_info (firstname, secondname, proffesion) VALUES (?, ?, ?)";
- pStatement = connection.prepareStatement(sqlQuery);
- pStatement.setString(1, fName);
- pStatement.setString(2, sName);
- pStatement.setString(3, userProff);
- pStatement.executeUpdate();
- System.out.println("Record saved!");
- }catch(Exception e) {
- e.printStackTrace();
- }finally {
- DBUtil.closePrepStatement(pStatement);
- DBUtil.closeConnection(connection);
- }
- }
- // Get data from database and display
- private void displayData() {
- System.out.println("Current records from the database:");
- try {
- connection = DBUtil.getConnection();
- String sqlQuery = "SELECT * FROM employees_info";
- pStatement = connection.prepareStatement(sqlQuery);
- rSet = pStatement.executeQuery();
- while(rSet.next()) {
- System.out.println("");
- System.out.println("Next Record:");
- System.out.println(rSet.getInt("ID"));
- System.out.println(rSet.getString("firstname"));
- System.out.println(rSet.getString("secondname"));
- System.out.println(rSet.getString("proffesion"));
- }
- }catch (Exception e) {
- e.printStackTrace();
- }finally {
- DBUtil.closeResSet(rSet);
- DBUtil.closePrepStatement(pStatement);
- DBUtil.closeConnection(connection);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement