Advertisement
Guest User

Untitled

a guest
Sep 14th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1. import java.sql.*;
  2. import javax.swing.*;
  3.  
  4. public class javaconnect {
  5. public static Connection ConnecDB(){
  6. Connection conn = null;
  7. String url = "jdbc:mysql://localhost:3306/testing";
  8. String driver = "com.mysql.jdbc.Driver";
  9. String user = "root";
  10. String pass = "root";
  11. try {
  12. Class.forName(driver);
  13. conn = DriverManager.getConnection(url, user, pass);
  14. if (conn == null) {
  15. System.out.println("Connection cannot be established");
  16. }
  17. System.out.println("Connected to database");
  18. return conn;
  19. } catch (Exception e) {
  20. System.out.println(e);
  21. }
  22. return null;
  23.  
  24. }
  25.  
  26. import java.awt.*;
  27. import java.sql.*;
  28. import javax.swing.*;
  29.  
  30.  
  31. public class Login extends javax.swing.JFrame {
  32. Connection conn = null;
  33. ResultSet rs=null;
  34. PreparedStatement pst=null;
  35.  
  36. public Login() {
  37. initComponents();
  38.  
  39. }
  40.  
  41.  
  42. private void txt_usernameActionPerformed(java.awt.event.ActionEvent evt) {
  43. // TODO add your handling code here:
  44. }
  45.  
  46. private void cmd_loginActionPerformed(java.awt.event.ActionEvent evt) {
  47. // TODO add your handling code here:
  48. String sql = "select * from employee_info where username=? and password=?";
  49. try{
  50. pst=conn.prepareStatement(sql);
  51. pst.setString(1, txt_username.getText());
  52. pst.setString(2, txt_password.getText());
  53.  
  54. rs=pst.executeQuery();
  55. if (rs.next()) {
  56. JOptionPane.showMessageDialog(null, "Login Sucessfully");
  57. employee_info s = new employee_info();
  58. s.setVisible(true);
  59. }
  60.  
  61.  
  62. else{
  63. JOptionPane.showMessageDialog(null, "Incorrect Username or password");
  64.  
  65.  
  66. }
  67. }
  68.  
  69. catch(Exception e){
  70.  
  71. JOptionPane.showMessageDialog(null,e);
  72.  
  73. }
  74.  
  75. }
  76.  
  77. /**
  78. * @param args the command line arguments
  79. */
  80. public static void main(String args[]) {
  81.  
  82.  
  83. try {
  84. for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  85. if ("Nimbus".equals(info.getName())) {
  86. javax.swing.UIManager.setLookAndFeel(info.getClassName());
  87. break;
  88. }
  89. }
  90. } catch (ClassNotFoundException ex) {
  91. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  92. } catch (InstantiationException ex) {
  93. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  94. } catch (IllegalAccessException ex) {
  95. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  96. } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  97. java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  98. }
  99. //</editor-fold>
  100.  
  101. /* Create and display the form */
  102. java.awt.EventQueue.invokeLater(new Runnable() {
  103. public void run() {
  104. new Login().setVisible(true);
  105. }
  106. });
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement