Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- //package jdbc;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- //TODO: insert function
- public class JDBC {
- String driver = "com.mysql.cj.jdbc.Driver";
- String url = "jdbc:mysql://hostname:3306/kuku";
- /**Loading driver*/
- static boolean loadDriver() {
- System.out.print("Checking driver...");
- try {
- Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
- return true;
- } catch (Exception e) {
- System.out.println("Error during loading a driver");
- return false;
- }
- }
- /**
- * Method for connecting to database
- *
- * @param address
- * - address of the database
- * @param dataBaseName
- * - ..as above
- * @param userName
- * - login to database
- * @param password
- * - password to database
- * @return - connects with database
- */
- private static Connection connectToDatabase(String address,
- String dataBaseName, String userName, String password) {
- System.out.print("\nConnecting with database..:");
- String database = "jdbc:mysql://" + address + "/" + dataBaseName;
- java.sql.Connection connection = null;
- try {
- connection = DriverManager.getConnection(database, userName, password);
- } catch (SQLException e) {
- System.out.println("Error during connecting to database");
- System.exit(1);
- }
- return connection;
- }
- /**
- * Displays data from SELECT query
- *
- * @param r
- * - result
- */
- private static void printDataFromQuery(ResultSet r) {
- ResultSetMetaData rsmd;
- try {
- rsmd = r.getMetaData();
- int numcols = rsmd.getColumnCount();
- // display names of columns:
- for (int i = 1; i <= numcols; i++) {
- System.out.print("\t" + rsmd.getColumnLabel(i) + "\t|");
- }
- System.out.print("\n____________________________________________________________________________\n");
- //r.next() - jump to next record
- while (r.next()) {
- for (int i = 1; i <= numcols; i++) {
- Object obj = r.getObject(i);
- if (obj != null)
- System.out.print("\t" + obj.toString() + "\t|");
- else
- System.out.print("\t");
- }
- System.out.println();
- }
- } catch (SQLException e) {
- System.out.println("Error during reading from database" + e.toString());
- System.exit(3);
- }
- }
- /**
- * Wykonanie kwerendy i przesłanie wyników do obiektu ResultSet
- *
- * @param s
- * - Statement
- * @param sql
- * - zapytanie
- * @return wynik
- */
- private static ResultSet executeQuery(Statement s, String sql) {
- try {
- return s.executeQuery(sql);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * tworzenie obiektu Statement przesyłającego zapytania do bazy connection
- *
- * @param connection
- * - połączenie z bazą
- * @return obiekt Statement przesyłający zapytania do bazy
- */
- private static Statement createStatement(Connection connection) {
- try {
- return connection.createStatement();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- ;
- return null;
- }
- /**
- * Zamykanie połączenia z bazą danych
- *
- * @param connection
- * - połączenie z bazą
- * @param s
- * - obiekt przesyłający zapytanie do bazy
- */
- private static void closeConnection(Connection connection, Statement s) {
- System.out.print("\nZamykanie polaczenia z databaseą:");
- try {
- s.close();
- connection.close();
- } catch (SQLException e) {
- System.out
- .println("Bląd przy zamykaniu polączenia " + e.toString());
- System.exit(4);
- }
- System.out.print(" zamknięcie OK");
- }
- public JDBC(){
- if (loadDriver())
- System.out.print("Driver correct");
- else
- System.exit(1);
- java.sql.Connection connection = connectToDatabase("127.0.0.1",
- "kuku", "root", "");
- if (connection != null)
- System.out.print("Connection correct\n");
- // WYKONYWANIE OPERACJI NA BAZIE DANYCH
- System.out.println("Pobieranie danych z bazy:");
- //String sql = "INSERT INTO Winnings (name, won) VALUES ('pawel', 2)";
- String sql = "Select * from Winnings";
- Statement s = createStatement(connection);
- ResultSet r = executeQuery(s, sql);
- printDataFromQuery(r);
- closeConnection(connection, s);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement