Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static final String GET_USERNAME = "SELECT userName FROM USER WHERE userID = ?";
- public static DataSource getMySQLDataSource() { // Funkar denna som connect
- MysqlDataSource mysqlDS = null;
- try {
- mysqlDS = new MysqlDataSource();
- mysqlDS.setURL("jdbc:mysql://localhost:3306/mydb");
- mysqlDS.setUser("root");
- mysqlDS.setPassword("");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return mysqlDS;
- }
- private void getUserInformation(int inputUserID) {
- Connection connection = null;
- PreparedStatement prstmt = null;
- // Get the MySqlDataSource
- DataSource dataSource = getMySQLDataSource();
- try {
- // Get connection from the database
- connection = dataSource.getConnection();
- // Execute query
- prstmt = connection.prepareStatement(GET_USERNAME);
- prstmt.setInt(1, inputUserID);
- ResultSet rs = prstmt.executeQuery();
- while (rs.next()) {
- // Retrieve by column name
- int userID = rs.getInt(userID);
- // Display values
- System.out.println("userID" + userID);
- }
- rs.close();
- } catch (SQLException se) {
- se.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- // catch (SQLException e) {
- // e.printStackTrace();
- // }
- finally {
- // Finally block used to close resources
- try {
- if (prstmt != null) {
- prstmt.close();
- }
- } catch (SQLException sqlException) {
- sqlException.printStackTrace();
- }
- try {
- if (connection != null) {
- connection.close();
- }
- } catch (SQLException sqlException) {
- sqlException.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement