Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.leo.test;
- import java.io.*;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.sql.*;
- import java.util.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class test
- {
- public static void main (String[] args) throws SQLException
- {
- Scanner in = new Scanner(System.in);
- System.out.println("Please type commands");
- StringBuilder builder = new StringBuilder();
- while (in.hasNextLine())
- {
- String line = in.nextLine();
- if (line.trim().equalsIgnoreCase("EXIT"))
- {
- System.exit(0);
- }
- else if (line.endsWith(";"))
- {
- builder.append(line);
- String command = builder.toString();
- command = command.substring(0, command.length() - 1);
- query(command);
- }
- else
- {
- builder.append(line);
- }
- }
- }
- public static void query(String command)
- {
- Properties prop = new Properties();
- try (InputStream in = Files.newInputStream(Paths.get("src/com/leo/test/database.properties")))
- {
- prop.load(in);
- String driver = prop.getProperty("jdbc.driver");
- String url = prop.getProperty("jdbc.url");
- String database = prop.getProperty("jdbc.database");
- String username = prop.getProperty("jdbc.username");
- String password = prop.getProperty("jdbc.password");
- MySQLDatabase mysql = new MySQLDatabase(driver, url, username, password) ;
- mysql.setDatabase(database);
- mysql.execute(command);
- }
- catch (IOException ex) {
- Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- class MySQLDatabase
- {
- private Connection conn = null;
- public MySQLDatabase(String driver, String url, String username, String password)
- {
- try
- {
- System.setProperty("jdbc.drivers", driver);
- conn = DriverManager.getConnection(url, username, password);
- }
- catch (SQLException e)
- {
- e.printStackTrace();
- }
- }
- public void setDatabase(String database)
- {
- try
- {
- conn.setCatalog(database);
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void execute(String command)
- {
- try
- {
- Statement stmt = conn.createStatement();
- boolean hasResSet = stmt.execute(command);
- if (hasResSet)
- {
- ResultSet resSet = stmt.getResultSet();
- ResultSetMetaData metaData = resSet.getMetaData();
- int columnCount = metaData.getColumnCount();
- for (int i = 1; i <= columnCount; i++)
- {
- if (i > 1) System.out.print(", ");
- String columnLabel = metaData.getColumnLabel(i);
- System.out.print(columnLabel);
- }
- System.out.println();
- while (resSet.next())
- {
- for (int i = 1; i < columnCount; i++)
- {
- if (i > 1) System.out.print(", ");
- String value = resSet.getString(i);
- System.out.print(value);
- }
- System.out.println();
- }
- }
- else
- {
- int updateCount = stmt.getUpdateCount();
- System.out.println("Rows Affected: " + updateCount);
- }
- }
- catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public void close()
- {
- try
- {
- conn.close();
- }
- catch (SQLException ex) {
- Logger.getLogger(MySQLDatabase.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement