Advertisement
Guest User

Untitled

a guest
Jan 9th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1.     Statement statement;
  2.  
  3.     public DatabaseConnection(){
  4.         try{
  5.             String driver = "com.mysql.jdbc.Driver";
  6.             String url = "jdbc:mysql://localhost:3306/mygame";
  7.             String username = "root";
  8.             String password = "root";
  9.             //Class.forName(driver);
  10.  
  11.             Connection con = DriverManager.getConnection(url,username,password);
  12.             statement = con.createStatement();
  13.             System.out.println("Connected to database!");
  14.         } catch(Exception e){
  15.             System.out.println(e);
  16.             System.out.println("Connection fail");
  17.         }
  18.     }
  19.  
  20.     public void  getAllGames() {
  21.         try {
  22.             ResultSet rs = statement.executeQuery("select * from game ORDER by game_name");
  23.  
  24.             while(rs.next()){
  25.                 System.out.println(rs.getString(2));
  26.  
  27.             }
  28.         }catch (SQLException ex){
  29.             System.out.println("executing fail");
  30.         }
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement