Advertisement
WatermellonLOL

Untitled

Mar 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. package pw.melon.chat;
  2.  
  3. import java.awt.EventQueue;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. import javax.naming.NamingException;
  11. import javax.swing.JFrame;
  12.  
  13. public class Chat {
  14.  
  15. private JFrame frame;
  16.  
  17. public static void main(String[] args) throws NamingException, SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
  18.  
  19. Class.forName("com.mysql.jdbc.Driver").newInstance();
  20. Connection conn = DriverManager.getConnection
  21. ("jdbc:mysql://localhost:3306/test", "root", "");
  22.  
  23. Statement stmt = conn.createStatement();
  24. stmt.execute("SELECT * FROM test");
  25. ResultSet rs = stmt.executeQuery("SELECT * FROM test");
  26. System.out.println(rs);
  27. while (rs.next()) {
  28. int id = rs.getInt("id");
  29. String name = rs.getString("name");
  30. int age = rs.getInt("age");
  31. System.out.println(id + name + age);
  32. }
  33. stmt.close();
  34. conn.close();
  35.  
  36. /*System.out.println("Loading driver...");
  37.  
  38. try {
  39. Class.forName("com.mysql.jdbc.Driver");
  40. System.out.println("Driver loaded!");
  41. } catch (ClassNotFoundException e) {
  42. throw new IllegalStateException("Cannot find the driver in the classpath!", e);
  43. }
  44.  
  45. String url = "jdbc:mysql://localhost:3306/test";
  46. String username = "root";
  47. String password = "";
  48.  
  49. System.out.println("Connecting database...");
  50.  
  51. try (Connection connection = DriverManager.getConnection(url, username, password)) {
  52. System.out.println("Database connected!");
  53. } catch (SQLException e) {
  54. throw new IllegalStateException("Cannot connect the database!", e);
  55. }*/
  56.  
  57.  
  58. EventQueue.invokeLater(new Runnable() {
  59. public void run() {
  60. try {
  61. Chat window = new Chat();
  62. window.frame.setVisible(true);
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. }
  66. }
  67. });
  68. }
  69.  
  70. public Chat() {
  71. initialize();
  72. }
  73.  
  74. private void initialize() {
  75. frame = new JFrame();
  76. frame.setBounds(100, 100, 450, 300);
  77. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  78. }
  79.  
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement