Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io;
- import dk.au.hum.imv.persistence.db.JDBCConnectionFactory;
- import static dk.au.hum.imv.persistence.db.JDBCConnectionFactory.*;
- import domain.*;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import domain.Post;
- import java.sql.*;
- import java.sql.Connection;
- public class PostMapper {
- private static final String URL = "jdbc:mysql://mysql.student.hum.au.dk/ukko"; //going to the server
- private static final String USERNAME = "ukko";
- private static final String PASSWORD = "FQW9AA8N";
- private static final String dbDriver = "com.mysql.jdbc.Driver";
- private static final String TABLE_NAME = "car";
- private Connection myConnection; //put Connection into the variable myConnection
- private PreparedStatement selectAllCar;
- public PostMapper() { //constructor
- JDBCConnectionFactory.initManualConnectionHandling(URL, USERNAME, PASSWORD, dbDriver);
- String brands = "'Lada','Sherp', 'AvtoRos', 'Marussia Motors', 'GAZ', 'Sofia', 'Bulgaralpine', 'UAZ', 'Liebherr'";
- try{
- myConnection = getInstance().getNewConnection();
- String sqlStatement = "SELECT * FROM " + TABLE_NAME;
- if (brands != null && !"".equals(brands)) sqlStatement += " WHERE brand IN (" + brands + ")";
- // OBERED BY year ASC DESC
- // sqlStatement += " OBERED BY year ASC";
- selectAllCar = myConnection.prepareStatement(sqlStatement); //selecting the whole table
- }
- catch (SQLException sqlException){
- sqlException.printStackTrace();
- System.exit(1);
- }
- }
- public ArrayList<Post> getAllCars() { //grab information and put in an array
- ArrayList<Post> results = new ArrayList<Post>();
- ResultSet resultSet = null;
- try {
- resultSet = selectAllCar.executeQuery();
- //results = new ArrayList<Post>(); //actually putting in the array
- while (resultSet.next()) {
- results.add(new domain.Post(
- resultSet.getString(TABLE_NAME + ".model"),
- resultSet.getString(TABLE_NAME + ".brand"),
- resultSet.getString(TABLE_NAME + ".year"),
- resultSet.getString(TABLE_NAME + ".content"),
- resultSet.getLong(TABLE_NAME + ".id")));
- }
- } catch (SQLException sqlException) {
- sqlException.printStackTrace();
- } finally {
- if (resultSet != null)
- try {
- resultSet.close();
- } catch (SQLException sqlException) {
- sqlException.printStackTrace();
- close();
- }
- }
- return results;
- }
- public void close(){
- try {
- myConnection.close();
- }
- catch(SQLException sqlException){
- sqlException.printStackTrace();
- close();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement