Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.56 KB | None | 0 0
  1. package cmpsc221.examples.JDBC;
  2.  
  3. import java.sql.*;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JOptionPane;
  7.  
  8. public class DBTest {
  9.  
  10.     /**
  11.      * @param args
  12.      */
  13.     public static void main(String[] args) {
  14.         // TODO Auto-generated method stub
  15.         try{
  16.             Class.forName("com.mysql.jdbc.Driver");
  17.             String url = "jdbc:mysql://127.0.0.1/glidegame";
  18.             Connection con = DriverManager.getConnection(url, "root", "");
  19.             Statement stmt = con.createStatement();
  20.             int r = stmt.executeUpdate(
  21.                 "INSERT INTO `glidegame`.`highscores` ("+
  22.                 "`Name` ,"+
  23.                 "`Score` ,"+
  24.                 "`Time of Score`"+
  25.                 ")"+
  26.                 "VALUES ("+
  27.                 "'UserName', '00:01:31' , TIMESTAMP("+
  28.                 "CURRENT_TIMESTAMP )"+
  29.                 ");");
  30.         }catch (Exception ex)
  31.         {
  32.             System.out.println("Exception occurred: ");
  33.             ex.printStackTrace();
  34.         }
  35.         try{
  36.             Class.forName("com.mysql.jdbc.Driver");
  37.             String url = "jdbc:mysql://127.0.0.1/glidegame";
  38.             Connection con = DriverManager.getConnection(url, "root", "");
  39.             Statement stmt = con.createStatement();
  40.             ResultSet r = stmt.executeQuery("select * from highscores");
  41.             String s = new String();
  42.             s = "Name" + "---" + "Score" + "---" + "Time of Score" + '\n';
  43.             while(r.next())
  44.             {
  45.                 s+= r.getString("Name") + " --- " + r.getString("Score") + " --- " + r.getString("Time of Score") + '\n';
  46.             }
  47.             System.out.println(s);
  48.             JOptionPane.showMessageDialog(null, s, "High Scores", JOptionPane.INFORMATION_MESSAGE);
  49.         }catch (Exception ex)
  50.         {
  51.             System.out.println("Exception occurred: ");
  52.             ex.printStackTrace();
  53.            
  54.         }
  55.        
  56.        
  57.     }
  58.  
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement