Advertisement
Guest User

Untitled

a guest
Jan 12th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.30 KB | None | 0 0
  1. import java.sql.*;
  2. import java.awt.*;
  3. import javax.swing.*;
  4. import java.awt.event.*;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8.  
  9.  
  10.  
  11. public class gui implements ActionListener{
  12.  
  13. public static JButton myButton;
  14.  
  15. public static void main(String[] args) {
  16. (new gui()).go();
  17.  
  18. }
  19.  
  20. public void go() {
  21.  
  22.  
  23. JFrame myFrame = new JFrame();
  24.  
  25.  
  26.  
  27. myButton = new JButton("Hit Me");
  28.  
  29. myButton.addActionListener(this);
  30.  
  31.  
  32. myFrame.getContentPane().add(myButton);
  33.  
  34. myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  35.  
  36. myFrame.setSize(300,200);
  37. myFrame.setVisible(true);
  38. }
  39.  
  40. public void actionPerformed(ActionEvent event) {
  41.  
  42.  
  43. String url = "jdbc:mysql://localhost:3306/memes?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  44. String username = "root";
  45. String password = "7777";
  46.  
  47. try{
  48. Class.forName("com.mysql.cj.jdbc.Driver");
  49. try (Connection connection = DriverManager.getConnection(url, username, password)) {
  50. myButton.setText("Connesso al Database!");
  51. }
  52. if(TextDemo.check.equals("yes")) {
  53. JFrame frame = new JFrame("Query");
  54. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  55.  
  56. //Add contents to the window.
  57. frame.add(new TextDemo());
  58.  
  59. //Display the window.
  60. frame.pack();
  61. frame.setVisible(true);
  62. TextDemo.check="no";
  63. }
  64.  
  65. }
  66. catch (SQLException | ClassNotFoundException e) {
  67. myButton.setText("Ops :(");
  68. e.printStackTrace();}
  69.  
  70. }
  71.  
  72.  
  73.  
  74.  
  75. public static class TextDemo extends JPanel implements ActionListener {
  76. /**
  77. *
  78. */
  79. private static final long serialVersionUID = 1L;
  80. protected static JTextField textField;
  81. protected static JTextArea textArea;
  82. private final static String newline = "\n";
  83. public static String check = "yes";
  84.  
  85.  
  86.  
  87. public TextDemo() {
  88. super(new GridBagLayout());
  89.  
  90. textField = new JTextField(20);
  91. textField.addActionListener(this);
  92.  
  93. textArea = new JTextArea(10, 60);
  94. textArea.setEditable(false);
  95. JScrollPane scrollPane = new JScrollPane(textArea);
  96.  
  97. //Add Components to this panel.
  98. GridBagConstraints c = new GridBagConstraints();
  99. c.gridwidth = GridBagConstraints.REMAINDER;
  100.  
  101. c.fill = GridBagConstraints.HORIZONTAL;
  102. add(textField, c);
  103.  
  104. c.fill = GridBagConstraints.BOTH;
  105. c.weightx = 1.0;
  106. c.weighty = 1.0;
  107. add(scrollPane, c);
  108. }
  109.  
  110. public void actionPerformed(ActionEvent evt) {
  111.  
  112. if (textField.getText().equals("drop")) {
  113. gui.droptable();}
  114. else
  115. if (textField.getText().equals("show")){
  116. gui.printusers();
  117. }
  118. String text = textField.getText();
  119. textArea.append( newline);
  120. textField.selectAll();
  121. //Make sure the new text is visible, even if there
  122. //was a selection in the text area.
  123. textArea.setCaretPosition(textArea.getDocument().getLength());
  124.  
  125.  
  126.  
  127.  
  128. }}
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140. public static void droptable() {
  141.  
  142.  
  143.  
  144. // JDBC driver name and database URL
  145. final String DB_URL = "jdbc:mysql://localhost:3306/memes?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  146.  
  147. // Database credentials
  148. final String USER = "root";
  149. final String PASS = "7777";
  150.  
  151. Connection conn = null;
  152. Statement stmt = null;
  153. try{
  154. //STEP 2: Register JDBC driver
  155. Class.forName("com.mysql.cj.jdbc.Driver");
  156.  
  157. //STEP 3: Open a connection
  158. System.out.println("Connecting to a selected database...");
  159. conn = DriverManager.getConnection(DB_URL, USER, PASS);
  160. System.out.println("Connected database successfully...");
  161.  
  162. //STEP 4: Execute a query
  163. System.out.println("Deleting table in given database...");
  164. stmt = conn.createStatement();
  165.  
  166. String sql = "DROP TABLE users ";
  167.  
  168. stmt.executeUpdate(sql);
  169. System.out.println("Table deleted in given database...");
  170. }catch(SQLException se){
  171. //Handle errors for JDBC
  172. se.printStackTrace();
  173. }catch(Exception e){
  174. //Handle errors for Class.forName
  175. e.printStackTrace();
  176. }finally{
  177. //finally block used to close resources
  178. try{
  179. if(stmt!=null)
  180. conn.close();
  181. }catch(SQLException se){
  182. }// do nothing
  183. try{
  184. if(conn!=null)
  185. conn.close();
  186. }catch(SQLException se){
  187. se.printStackTrace();
  188. }//end finally try
  189. }//end try
  190. System.out.println("Goodbye!");
  191. }//end main
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. public static void printusers()
  205. {
  206. try
  207. {
  208. // create our mysql database connection
  209. String myDriver = "com.mysql.cj.jdbc.Driver";
  210. String myUrl = "jdbc:mysql://localhost:3306/memes?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
  211. Class.forName(myDriver);
  212. Connection conn = DriverManager.getConnection(myUrl, "root", "7777");
  213.  
  214. // our SQL SELECT query.
  215. // if you only need a few columns, specify them by name instead of using "*"
  216. String query = "SELECT * FROM users";
  217.  
  218. // create the java statement
  219. Statement st = conn.createStatement();
  220.  
  221. // execute the query, and get a java resultset
  222. ResultSet rs = st.executeQuery(query);
  223.  
  224. // iterate through the java resultset
  225. while (rs.next())
  226. {
  227. int id = rs.getInt("id");
  228. String firstName = rs.getString("first_name");
  229. String lastName = rs.getString("last_name");
  230. Date dateCreated = rs.getDate("date_created");
  231. boolean isAdmin = rs.getBoolean("is_admin");
  232. int numPoints = rs.getInt("num_points");
  233.  
  234. // print the results
  235. String output = id +"\t" + firstName+"\t" + lastName +"\t" + dateCreated +"\t" + isAdmin+"\t" + numPoints+"\n" ;
  236. System.out.println(output);
  237. TextDemo.textArea.append(output);
  238.  
  239.  
  240. }
  241. st.close();
  242. }
  243. catch (Exception e)
  244. {
  245. System.err.println("Got an exception! ");
  246. System.err.println(e.getMessage());
  247. }
  248. }
  249.  
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement