Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате |
Форум программистов > Java: Общие вопросы > Нужна помощь в коде |
Автор: Alex1945 22.2.2017, 14:28 |
Здраствуйте!!нужна помощь в коде,Java изучаю недолго,проблема такая запрос к базе данных я сделал а получить содержимое не получается в чем может быть проблема?Подскажите пожалуйста! package utils; import com.mysql.fabric.jdbc.FabricMySQLDriver; import java.sql.*; public class DBConnection { private static final String URL = "jdbc:mysql://localhost:3306/testdb"; private static final String USER = "root"; private static final String PASSWORD = "root"; public static void main(String[] args) { try { selectRecFromTable(); } catch (SQLException e) { System.out.println(e.getMessage()); } } private static void selectRecFromTable() throws SQLException { Connection dbConnection = null; PreparedStatement statement = null; String sqlQuery = "SELECT * From employee "; try{ dbConnection = getDBConnection(); statement = dbConnection.prepareStatement(sqlQuery); ResultSet resultSet = statement.executeQuery(); while (resultSet.next()){ String employeeID = resultSet.getString(1); String employeeName = resultSet.getString(2); String employeeEmail = resultSet.getString(3); String departmentID = resultSet.getString(4); System.out.println("ID = " + employeeID + " Name = " + employeeName + " Email = " + employeeEmail + " DepartmentID = " + departmentID); resultSet.close(); } } catch(SQLException e){ e.getMessage(); } finally { if(statement != null){ statement.close(); } if(dbConnection != null){ dbConnection.close(); } } } private static Connection getDBConnection(){ Connection dbConnection = null; try { Driver driver = new FabricMySQLDriver(); DriverManager.registerDriver(driver); dbConnection = DriverManager.getConnection(URL, USER, PASSWORD); if(dbConnection != null){ System.out.println("Соединение установленно"); } else { System.out.println("Соединение не установленно"); } } catch(SQLException e){ System.out.println(e.getMessage()); } return dbConnection; } } |