Advertisement
swojcik

Untitled

Apr 20th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. package dailySchool;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. import javax.swing.JOptionPane;
  10.  
  11. public class Mysql
  12. {
  13.     private final String host =  "jdbc:mysql://localhost:3306/diary";
  14.     private final String username = "root";
  15.     private final String password ="";
  16.     private Statement mysts;
  17.     private ResultSet myRs;
  18.    
  19.     public void connection()
  20.     {
  21.         try
  22.         {
  23.             Connection connect = DriverManager.getConnection(host,username,password);
  24.             mysts= connect.createStatement();
  25.            
  26.             //System.out.println("połaczono z baza danych");
  27.         }
  28.         catch(Exception error)
  29.         {
  30.             error.printStackTrace();
  31.         }
  32.     }
  33.    
  34.     public void addUser(String login, String password, int rang)
  35.     {
  36.         boolean isName = false;
  37.         try
  38.         {
  39.             myRs= mysts.executeQuery("SELECT * from users ");
  40.                
  41.             while(myRs.next())
  42.             {
  43.                 if(myRs.getString("login").equals(login)){
  44.                     isName = true;
  45.                     JOptionPane.showMessageDialog(null, "Login jest zajety");
  46.                 }
  47.             }
  48.             if(isName == false)
  49.             {
  50.                 mysts.executeUpdate("INSERT INTO users(login,password,rang,accept) VALUES ("+""+login+","+ password+","+rang+",0)");
  51.                 JOptionPane.showMessageDialog(null, "Pomyslnie zarejestrowano użytkownika");
  52.             }
  53.         } catch (SQLException e)
  54.         {
  55.             e.printStackTrace();
  56.         }
  57.     }
  58.    
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement