Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pw.melon.chat;
- import java.awt.EventQueue;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import javax.naming.NamingException;
- import javax.swing.JFrame;
- public class Chat {
- private JFrame frame;
- public static void main(String[] args) throws NamingException, SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection conn = DriverManager.getConnection
- ("jdbc:mysql://localhost:3306/test", "root", "");
- Statement stmt = conn.createStatement();
- stmt.execute("SELECT * FROM test");
- ResultSet rs = stmt.executeQuery("SELECT * FROM test");
- System.out.println(rs);
- while (rs.next()) {
- int id = rs.getInt("id");
- String name = rs.getString("name");
- int age = rs.getInt("age");
- System.out.println(id + name + age);
- }
- stmt.close();
- conn.close();
- /*System.out.println("Loading driver...");
- try {
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Driver loaded!");
- } catch (ClassNotFoundException e) {
- throw new IllegalStateException("Cannot find the driver in the classpath!", e);
- }
- String url = "jdbc:mysql://localhost:3306/test";
- String username = "root";
- String password = "";
- System.out.println("Connecting database...");
- try (Connection connection = DriverManager.getConnection(url, username, password)) {
- System.out.println("Database connected!");
- } catch (SQLException e) {
- throw new IllegalStateException("Cannot connect the database!", e);
- }*/
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- Chat window = new Chat();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- public Chat() {
- initialize();
- }
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 450, 300);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement