Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1.  
  2. import java.sql.*;
  3. import java.io.*;
  4.  
  5. import java.sql.Connection;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10.  
  11. import javax.naming.Context;
  12. import javax.naming.InitialContext;
  13. import javax.servlet.ServletException;
  14. import javax.servlet.UnavailableException;
  15. import javax.servlet.http.HttpServlet;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18. import javax.sql.DataSource;
  19. import javax.swing.*;
  20. import java.awt.*;
  21.  
  22. import java.awt.event.ActionEvent;
  23. import java.awt.event.ActionListener;
  24. import java.awt.event.ItemEvent;
  25. import java.awt.event.ItemListener;
  26. import java.awt.event.KeyEvent;
  27. import java.awt.event.KeyListener;
  28.  
  29. import javax.swing.JButton;
  30. import javax.swing.JFrame;
  31. import javax.swing.JLabel;
  32. import javax.swing.JPanel;
  33. import javax.swing.event.ChangeEvent;
  34. import javax.swing.event.ChangeListener;
  35. import javax.swing.text.Document;
  36. import javax.swing.event.DocumentEvent;
  37. import javax.swing.event.DocumentListener;
  38. import java.awt.event.KeyAdapter;
  39. import java.awt.event.KeyEvent;
  40. import java.awt.datatransfer.DataFlavor;
  41. import java.awt.datatransfer.StringSelection;
  42. import java.awt.Toolkit;
  43. import java.awt.datatransfer.Clipboard;
  44.  
  45.  
  46. public class Esimerkki extends JFrame {
  47.  
  48. private JTextField nameText = new JTextField(10);
  49. private JButton DB1 = new JButton("Print Database");
  50. private JButton DB2 = new JButton("Insert to Database");
  51.  
  52.  
  53. private JTextField emailText = new JTextField(10);
  54.  
  55. private JLabel nimi= new JLabel("Nimi");
  56. private JLabel maili = new JLabel("Email");
  57.  
  58. private JPanel panel1 = new JPanel();
  59. private JPanel panel2 = new JPanel();
  60. private JPanel panel3 = new JPanel();
  61. private JPanel panel4 = new JPanel();
  62. private JTextArea textArea = new JTextArea();
  63. public ResultSet rs= null;
  64.  
  65. protected static Connection con = null;
  66.  
  67.  
  68. public void printDB() throws IOException{
  69.  
  70.  
  71.  
  72. try {
  73. ResultSet tableRs = con.getMetaData().getTables(null,
  74. "", "HEPPI", null);
  75. if (!tableRs.next()) {
  76. String sql;
  77. sql = "CREATE TABLE HEPPI" +" ( NIMI VARCHAR(45), EMAIL VARCHAR(65) )";
  78.  
  79. Statement stmt = con.createStatement();
  80. stmt.executeUpdate(sql);
  81. stmt.close(); }
  82. } catch (Exception e) {
  83. System.out.println("Taulun lisäys epäonnistui!");
  84. }
  85.  
  86.  
  87.  
  88.  
  89. try {
  90.  
  91. Statement Stmt = con.createStatement();
  92.  
  93.  
  94. rs = Stmt.executeQuery("SELECT * FROM HEPPI");
  95. while (rs.next()) {
  96.  
  97.  
  98.  
  99. System.out.println ("hippi: " + rs.getString(1));
  100. System.out.println ("heppa: " + rs.getString(2));
  101. }
  102. } catch (SQLException e) {
  103. e.printStackTrace();
  104. }
  105.  
  106.  
  107.  
  108. }
  109.  
  110.  
  111.  
  112.  
  113. public void insertToDB() throws IOException {
  114.  
  115. try {
  116. PreparedStatement insertStmt;
  117.  
  118. insertStmt = con.prepareStatement("INSERT INTO HEPPI VALUES(?, ?)");
  119. insertStmt.setString(1, nameText.getText());
  120. insertStmt.setString(2, emailText.getText());
  121.  
  122. insertStmt.executeUpdate();
  123. insertStmt.close();
  124. System.out.println("Lisäys onnistui");
  125. } catch(Exception e) {
  126. System.out.println("Lisäys epäonnistui!");
  127. }
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. public static void main(String args[]) throws Exception {
  137. try {
  138.  
  139. String url= "jdbc:mysql://localhost:3306/javatesti";
  140. String ajuri= "com.mysql.jdbc.Driver";
  141. String tunnus = "testaaja";
  142. String salasana = "sala";
  143.  
  144.  
  145.  
  146.  
  147. Class.forName(ajuri);
  148. con = DriverManager.getConnection(url,tunnus,salasana);
  149.  
  150.  
  151.  
  152. }
  153. catch (Exception e)
  154. {
  155. System.err.println ("Cannot connect to database server");
  156. System.out.println(e);
  157. }
  158. new Esimerkki ();
  159. }
  160.  
  161.  
  162.  
  163. Esimerkki (){
  164.  
  165.  
  166. GridLayout gridLayout = new GridLayout(0,1);
  167. FlowLayout napit = new FlowLayout();
  168. napit.setAlignment(FlowLayout.LEADING);
  169. napit.setHgap(10);
  170.  
  171. Container content = getContentPane();
  172.  
  173. nameText.addActionListener(new ActionListener() {
  174. public void actionPerformed(ActionEvent evt) {
  175. String text1 = nameText.getText();
  176.  
  177. }
  178. });
  179.  
  180. emailText.addActionListener(new ActionListener() {
  181. public void actionPerformed(ActionEvent evt) {
  182. String text2 = emailText.getText();
  183.  
  184. }
  185. });
  186.  
  187.  
  188.  
  189. /* DB1.addActionListener(new ActionListener() {
  190. public void actionPerformed(ActionEvent e) {
  191. try {printDB(); }
  192. catch (Exception ee) { }
  193. }
  194. }); */
  195. DB2.addActionListener(new ActionListener() {
  196. public void actionPerformed(ActionEvent e) {
  197. try {insertToDB(); }
  198. catch (Exception ee) { }
  199. }
  200. });
  201.  
  202. DB1.addActionListener(new ActionListener() {
  203. public void actionPerformed(ActionEvent e) {
  204. try {
  205.  
  206. Statement Stmt = con.createStatement();
  207.  
  208.  
  209. rs = Stmt.executeQuery("SELECT * FROM HEPPI");
  210. while (rs.next()) {
  211.  
  212. String hep = rs.getString(1);
  213.  
  214.  
  215. }
  216. } catch (SQLException ep) {
  217. ep.printStackTrace();
  218. }
  219.  
  220.  
  221.  
  222. }
  223.  
  224. });
  225.  
  226.  
  227. textArea.setPreferredSize(new Dimension(700, 300));
  228.  
  229.  
  230. panel4.add(textArea);
  231. panel4.add(panel3);
  232. panel4.add(panel2);
  233.  
  234.  
  235.  
  236. panel1.setLayout(napit);
  237. panel1.add(DB1);
  238. panel1.add(DB2);
  239.  
  240. panel3.setLayout(gridLayout);
  241. panel3.add(nimi);
  242. panel3.add(maili);
  243.  
  244. panel2.setLayout(gridLayout);
  245. panel2.add(nameText);
  246. panel2.add(emailText);
  247.  
  248. content.add(panel4, BorderLayout.NORTH);
  249. /* content.add(panel3, BorderLayout.WEST);
  250.  
  251. content.add(panel2, BorderLayout.CENTER); */
  252. content.add(panel1, BorderLayout.SOUTH);
  253. pack();
  254. setVisible(true);
  255.  
  256.  
  257.  
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement