Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package GranssnittOchForteckning;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.ResultSetMetaData;
- import java.sql.SQLException;
- import java.sql.Statement;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author augustforsman
- */
- public class dataBas {
- Connection dbConnection = null;
- Statement statement = null;
- ResultSet rs = null;
- public void connect() {
- String url = "jdbc:mysql://atlantis.informatik.umu.se/svph1517_db2_ht2016";
- String user = "svph1517_2016264";
- String password = "bavedaqad";
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- dbConnection = DriverManager.getConnection(url, user, password);
- } catch (Exception e) {
- System.err.println("MYSQL:s JDBC-drivrutiner kunde INTE hittas.");
- }
- System.out.println("Uppkoppling mot databasen öppnad.");
- }
- public void select() {
- try {
- String strSql = "SELECT * FROM ex2_skivor";
- statement = dbConnection.createStatement();
- rs = statement.executeQuery(strSql);
- ResultSetMetaData rsmd = rs.getMetaData();
- int columnsNumber = rsmd.getColumnCount();
- // Iterate through the data in the result set and display it.
- while (rs.next()) {
- //Print one row
- for (int i = 1; i <= columnsNumber; i++) {
- System.out.print(rs.getString(i) + " "); //Print one element of a row
- }
- System.out.println();//Move to the next line to print the next row.
- }
- } catch (SQLException ex) {
- System.err.println("Ett fel har uppstått: " + ex.toString());
- }
- }
- public void close() {
- try {
- if (rs != null) {
- rs.close();
- }
- if (statement != null) {
- statement.close();
- }
- if (dbConnection != null) {
- dbConnection.close();
- }
- } catch (SQLException ex) {
- System.err.println("Ett fel har uppstått: " + ex.toString());
- }
- System.out.println("Uppkoppling mot databasen stängd.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement