Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- import com.mysql.jdbc.jdbc2.optional.*;
- import java.util.*;
- import javax.naming.*;
- import javax.sql.DataSource;
- import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
- public class MainProgram {
- static MainProgram program = new MainProgram();
- // ALL USER INPUTS "KNAPPAR"
- UserHandler uh = new UserHandler();
- // SKAPA KOPPLING TILL DATABAS (connection)
- // INITIERA STATEMENTS ETC
- 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("tgcmxzqw9");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return mysqlDS;
- }
- public Connection getConnection() throws SQLException {
- Connection connection = null;
- // Get the MySqlDataSource
- System.out.println("Verifying access");
- DataSource dataSource = getMySQLDataSource();
- // Get connection from the database
- System.out.println("Connecting database...");
- connection = dataSource.getConnection();
- return connection;
- }
- private int getUserID(int inputUserID) {
- PreparedStatement prstmt = null;
- int userID = -1;
- Connection connection = null;
- try {
- connection = getConnection();
- System.out.println("Database connected!");
- // Execute query
- // egentligen ska prepareStatement innehålla "GET_USERNAME
- String sql = "SELECT userName FROM USER WHERE userID = ?";
- prstmt = connection.prepareStatement(sql);
- prstmt.setInt(1, inputUserID);
- ResultSet rs = prstmt.executeQuery();
- while (rs.next()) {
- // Retrieve by column name
- userID = rs.getInt("userID");
- // Display values
- System.out.println("userID" + userID);
- }
- rs.close();
- } catch (SQLException se) {
- se.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- finally {
- // Finally block used to close resources
- closePrstmt(prstmt);
- closeConnection(connection);
- }
- return userID;
- }
- public void closePrstmt(PreparedStatement prstmt){
- try {
- if (prstmt != null) {
- prstmt.close();
- }
- } catch (SQLException sqlException) {
- sqlException.printStackTrace();
- }
- }
- public void closeConnection(Connection connection){
- try {
- if (connection != null) {
- connection.close();
- }
- } catch (SQLException sqlException) {
- sqlException.printStackTrace();
- }
- }
- public static void main(String[] args) throws SQLException {
- // program.testAllMethods();
- program.getUserID(0);
- }
- public void testAllMethods() throws SQLException {
- uh.getUsername(0);
- uh.setUsername(0);
- uh.getParticipantCounter(0);
- uh.increaseParticipantCounter(0);
- uh.getHostCounter(0);
- uh.increaseHostCounter(0);
- uh.getGender(0);
- uh.setGender(0);
- uh.getAge(0);
- uh.setAge(0);
- uh.getName(0);
- uh.setName(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement