Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- public class JDBC {
- static boolean ladujSterownik() {
- System.out.print("Sprawdzanie sterownika:");
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- return true;
- } catch (Exception e) {
- System.out.println("Blad przy ladowaniu sterownika bazy!");
- return false;
- }
- }
- public static Connection getConnection(String kindOfDatabase, String adres, int port, String userName,
- String password) {
- Connection conn = null;
- Properties connectionProps = new Properties();
- connectionProps.put("user", userName);
- connectionProps.put("password", password);
- try {
- conn = DriverManager.getConnection(kindOfDatabase + adres + ":" + port + "/", connectionProps);
- } catch (SQLException e) {
- System.out.println("Błąd połączenia z bazą danych! " + e.getMessage() + ": " + e.getErrorCode());
- System.exit(2);
- }
- System.out.println("Połączenie z bazą danych: ... OK");
- return conn;
- }
- private static Statement createStatement(Connection connection) {
- try {
- return connection.createStatement();
- } catch (SQLException e) {
- System.out.println("Błąd createStatement! " + e.getMessage() + ": " + e.getErrorCode());
- System.exit(3);
- }
- return null;
- }
- private static int executeUpdate(Statement s, String sql) {
- try {
- return s.executeUpdate(sql);
- } catch (SQLException e) {
- System.out.println("Zapytanie nie wykonane! " + e.getMessage() + ": " + e.getErrorCode());
- }
- return -1;
- }
- private static ResultSet executeQuery(Statement s, String sql) {
- try {
- return s.executeQuery(sql);
- } catch (SQLException e) {
- System.out.println("Zapytanie nie wykonane! " + e.getMessage() + ": " + e.getErrorCode());
- }
- return null;
- }
- private static void closeConnection(Connection connection, Statement s) {
- System.out.print("\nZamykanie polaczenia z bazą:");
- try {
- s.close();
- connection.close();
- } catch (SQLException e) {
- System.out.println("Bląd przy zamykaniu polączenia z bazą! " + e.getMessage() + ": " + e.getErrorCode());
- ;
- System.exit(4);
- }
- System.out.print(" zamknięcie OK");
- }
- public static void dodajAnkiete(Statement st)
- {
- String sql;
- ResultSet rs;
- if (executeUpdate(st,
- "CREATE TABLE ankieta ( id INT NOT NULL, nazwaAnkiety VARCHAR(50) NOT NULL, PRIMARY KEY (id) );") == 0)
- System.out.println("Tabela utworzona");
- else
- System.out.println("Tabela nie utworzona!");
- sql = "SELECT * FROM ANKIETA";
- rs = executeQuery(st,sql);
- try {
- if(!rs.next())
- {
- sql = "INSERT INTO ankieta VALUES(1,'Ankieta na temat komunikacji miejskiej');";
- executeUpdate(st, sql);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- if (executeUpdate(st,
- "CREATE TABLE pytanie ( id INT NOT NULL, idAnkiety INT, pytanie VARCHAR(80) NOT NULL,"
- + "odpa VARCHAR(30) NOT NULL ,odpb VARCHAR(30) NOT NULL,odpc VARCHAR(30) NOT NULL"
- + ",odpd VARCHAR(30) NOT NULL, PRIMARY KEY (id)," +
- " FOREIGN KEY (idAnkiety) REFERENCES ankieta(id) );") == 0)
- System.out.println("Tabela utworzona");
- else
- System.out.println("Tabela nie utworzona!");
- sql = "SELECT * FROM PYTANIE";
- rs = executeQuery(st,sql);
- try {
- if(!rs.next())
- {
- sql = "INSERT INTO pytanie VALUES(1,1,'Jak czesto korzystasz z komunikacji miejskiej','codziennie','dwa razy w tygodniu','raz w miesiacu','nigdy');";
- executeUpdate(st, sql);
- sql = "INSERT INTO pytanie VALUES(2,1,'Jak w skali 1-4 oceniasz komunikacje (1-zle , 4 -bardzo dobrze)','1','2','3','4');";
- executeUpdate(st, sql);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void print(Statement st)
- {
- String sql;
- ResultSet rs;
- sql = "SELECT * FROM ANKIETA";
- rs = executeQuery(st,sql);
- Ankieta a;
- Pytanie p;
- try {
- while(rs.next())
- {
- int id = rs.getInt(1);
- String nazwa = rs.getString(2);
- a = new Ankieta(id,nazwa);
- System.out.println(a);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- sql = "SELECT * FROM PYTANIE";
- rs = executeQuery(st,sql);
- try {
- while(rs.next())
- {
- int id = rs.getInt(1);
- String pytanie = rs.getString(2);
- String pytanie = rs.getString(2);
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- if (ladujSterownik()) {
- System.out.println("Polaczono z baza");
- } else {
- System.exit(1);
- }
- Connection con = getConnection("jdbc:mysql://", "localhost", 3306, "root", "");
- Statement st = createStatement(con);
- ResultSet rs;
- String sql;
- if (executeUpdate(st, "USE nowabaza1;") == 0)
- System.out.println("Baza wybrana");
- else {
- System.out.println("Baza nie istnieje! Tworzymy bazę: ");
- if (executeUpdate(st, "create Database nowabaza1;") == 1)
- System.out.println("Baza utworzona");
- else
- System.out.println("Baza nieutworzona!");
- if (executeUpdate(st, "USE nowaBaza;") == 0)
- System.out.println("Baza wybrana");
- else
- System.out.println("Baza niewybrana!");
- }
- dodajAnkiete(st);
- print(st);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement