Advertisement
Guest User

Untitled

a guest
Feb 12th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. /*
  2. Author:
  3. Program Title:
  4. Description:
  5. Date:
  6. Version number:
  7. */
  8.  
  9. import java.awt.*;
  10. import javax.swing.*;
  11. import java.awt.event.*;
  12. import java.sql.Connection;
  13. import java.sql.DriverManager;
  14. import java.sql.PreparedStatement;
  15. import java.sql.ResultSet;
  16.  
  17. public class Student extends JFrame
  18. {
  19. //Component's name
  20. private JLabel lblName,lblAge,lblGrade;
  21. private JTextField txtName, txtAge,txtGrade;
  22. private JButton btnAdd;
  23. String c;
  24. String d;
  25. String e;
  26.  
  27. public void Student()
  28. {
  29.  
  30. Connection conn = null;
  31. String url = "jdbc:mysql://localhost:3306/";
  32. String dbName = "Information";
  33. String driver = "com.mysql.jdbc.Driver";
  34. String userName = "root";
  35. String password = "abcd";
  36.  
  37. Container container = getContentPane();
  38. container.setLayout(new FlowLayout());
  39.  
  40. lblName=new JLabel("Name: ");
  41. container.add(lblName);
  42.  
  43. txtName=new JTextField(30);
  44. container.add(txtName);
  45.  
  46. lblAge=new JLabel("Age: ");
  47. container.add(lblAge);
  48.  
  49. txtAge=new JTextField(2);
  50. container.add(txtAge);
  51.  
  52. lblGrade=new JLabel("Grade: ");
  53. container.add(lblGrade);
  54.  
  55. txtGrade=new JTextField(1);
  56. container.add(txtGrade);
  57.  
  58. btnAdd=new JButton("Add");
  59. container.add(btnAdd);
  60.  
  61. btnAdd.addActionListener(new ActionListener()
  62. {
  63. public void actionPerformed(ActionEvent ev)
  64. {
  65. c=txtName.getText();
  66. d=txtAge.getText();
  67. e=txtGrade.getText();
  68. try
  69. {
  70. Class.forName(driver).newInstance();
  71. conn = DriverManager.getConnection(url + dbName, userName, password);
  72.  
  73. PreparedStatement statement = conn.prepareStatement("INSERT INTO Information ('StudentName', 'StudentAge','Grade') VALUES ('"+c+"', '"+d+"', '"+e+"'')");
  74. statement.executeQuery();
  75.  
  76. }
  77. catch (Exception ex)
  78. {
  79. ex.printStackTrace();
  80. }
  81. }
  82.  
  83. });
  84.  
  85. setSize(300,300);
  86. setVisible(true);
  87.  
  88. }
  89. public static void main(String[]args)
  90. {
  91. Student application = new Student();
  92. application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  93. }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement