Guest User

Connecting Sql_Ax768_12?TxtWeb/code

a guest
Apr 27th, 2017
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. ///////TEST CLASS////////
  2. package test;
  3. import java.sql.*;
  4. import javax.swing.JOptionPane;
  5.  
  6. public class Test {
  7. Connection conn = null;
  8. public static Connection ConnectDB(){
  9.  
  10. try{
  11. Class.forName("com.mysql.jdbc.Driver");
  12. Connection conn= DriverManager.getConnection("jdbc:mysql://localhost/db?user=root");
  13. JOptionPane.showMessageDialog(null,"Connected to DB");
  14. return conn;
  15.  
  16. }
  17. catch(Exception e){
  18. JOptionPane.showMessageDialog(null,e);
  19. return null;
  20. }
  21. }
  22. }
  23. ——————————
  24. /////////LOGIN FORM//////////
  25. Connection conn = null;
  26. PreparedStatement pst = null;
  27. ResultSet rs = null;
  28.  
  29. private void b1ActionPerformed(java.awt.event.ActionEvent evt) {
  30. conn=Test.ConnectDB();
  31. String Sql = "Select * from people where Fname=? and Lname=?";
  32.  
  33. try{
  34. pst=conn.prepareStatement(Sql);
  35. pst.setString(1,t1.getText());
  36. pst.setString(2,t2.getText());
  37. rs=pst.executeQuery();
  38. if(rs.next()){
  39. JOptionPane.showMessageDialog(null,"Welcome");
  40. Welcome w = new Welcome();
  41. w.setVisible(true);
  42. }
  43.  
  44. else{
  45. JOptionPane.showMessageDialog(null,"Incorrect","Acces Denied",JOptionPane.ERROR_MESSAGE);
  46. }
  47. } catch(Exception e){
  48. JOptionPane.showMessageDialog(null,e);
  49. }
  50.  
  51. }
  52. —————————————
  53. /////////////////WELCOME FORM//////////
  54. Just a label saying you are welcome
Add Comment
Please, Sign In to add comment