943,729 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 440
  • Java RSS
Aug 22nd, 2009
0

Registering system, Unwanted behavior

Expand Post »
Hello, I've made registering system to my little test program, It works well until you try to create two accounts with same name, It shouldn't allow it but it still does it. I have tried to solve this problem all day now by trying diffrent methods but sadly nothing helped. So if any of you geniuses could help me out.

Java Syntax (Toggle Plain Text)
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * Regform.java
  8.  *
  9.  * Created on 21.8.2009, 14:29:46
  10.  */
  11.  
  12. /**
  13.  *
  14.  * @author Samuli
  15.  */
  16.  
  17. import java.sql.*;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20.  
  21. public class Regform extends javax.swing.JFrame {
  22.  
  23. /** Creates new form Regform */
  24. public Regform() {
  25. initComponents();
  26. this.setLocation(800, 400);
  27. this.setTitle("Registeration");
  28. this.setResizable(false);
  29. }
  30.  
  31.  
  32. /** This method is called from within the constructor to
  33.   * initialize the form.
  34.   * WARNING: Do NOT modify this code. The content of this method is
  35.   * always regenerated by the Form Editor.
  36.   */
  37. @SuppressWarnings("unchecked")
  38.  
  39. public static boolean Cancreate = true;
  40.  
  41. // <editor-fold defaultstate="collapsed" desc="Generated Code">
  42. private void initComponents() {
  43.  
  44. jSeparator1 = new javax.swing.JSeparator();
  45. jLabel1 = new javax.swing.JLabel();
  46. jLabel2 = new javax.swing.JLabel();
  47. jLabel3 = new javax.swing.JLabel();
  48. jLabel4 = new javax.swing.JLabel();
  49. jLabel5 = new javax.swing.JLabel();
  50. Password = new javax.swing.JPasswordField();
  51. Name = new javax.swing.JTextField();
  52. Key = new javax.swing.JTextField();
  53. Proceed = new javax.swing.JButton();
  54. Cancel = new javax.swing.JButton();
  55. Status = new javax.swing.JLabel();
  56.  
  57. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  58.  
  59. jLabel1.setText("Registeration");
  60.  
  61. jLabel2.setText("Welcome to registeration if you don't know what to do, go away");
  62.  
  63. jLabel3.setText("Username:");
  64.  
  65. jLabel4.setText("Password:");
  66.  
  67. jLabel5.setText("Registeration Key:");
  68.  
  69. Proceed.setText("Proceed");
  70. Proceed.addActionListener(new java.awt.event.ActionListener() {
  71. public void actionPerformed(java.awt.event.ActionEvent evt) {
  72. ProceedActionPerformed(evt);
  73. }
  74. });
  75.  
  76. Cancel.setText("Cancel");
  77. Cancel.addActionListener(new java.awt.event.ActionListener() {
  78. public void actionPerformed(java.awt.event.ActionEvent evt) {
  79. CancelActionPerformed(evt);
  80. }
  81. });
  82.  
  83. Status.setText("Status text");
  84.  
  85. javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  86. getContentPane().setLayout(layout);
  87. layout.setHorizontalGroup(
  88. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  89. .addGroup(layout.createSequentialGroup()
  90. .addGap(44, 44, 44)
  91. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  92. .addGroup(layout.createSequentialGroup()
  93. .addComponent(jLabel5)
  94. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  95. .addComponent(Key, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE))
  96. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  97. .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  98. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  99. .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  100. .addComponent(jLabel3)
  101. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  102. .addComponent(Name))
  103. .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  104. .addComponent(jLabel4)
  105. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  106. .addComponent(Password, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
  107. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  108. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  109. .addComponent(Cancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  110. .addComponent(Proceed, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)))
  111. .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)))
  112. .addContainerGap(61, Short.MAX_VALUE))
  113. .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  114. .addContainerGap()
  115. .addComponent(jLabel1)
  116. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  117. .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE))
  118. .addGroup(layout.createSequentialGroup()
  119. .addGap(104, 104, 104)
  120. .addComponent(Status, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
  121. .addContainerGap())
  122. );
  123. layout.setVerticalGroup(
  124. layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  125. .addGroup(layout.createSequentialGroup()
  126. .addGap(16, 16, 16)
  127. .addComponent(Status)
  128. .addGap(2, 2, 2)
  129. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  130. .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  131. .addComponent(jLabel1))
  132. .addGap(18, 18, 18)
  133. .addComponent(jLabel2)
  134. .addGap(27, 27, 27)
  135. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  136. .addComponent(jLabel3)
  137. .addComponent(Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  138. .addComponent(Proceed))
  139. .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  140. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  141. .addComponent(jLabel4)
  142. .addComponent(Cancel)
  143. .addComponent(Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  144. .addGap(18, 18, 18)
  145. .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  146. .addComponent(jLabel5)
  147. .addComponent(Key, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  148. .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  149. );
  150.  
  151. pack();
  152. }// </editor-fold>
  153.  
  154. @SuppressWarnings("empty-statement")
  155. private void ProceedActionPerformed(java.awt.event.ActionEvent evt) {
  156.  
  157.  
  158. Connection con = null;
  159.  
  160. try {
  161. Class.forName("com.mysql.jdbc.Driver");
  162. } catch(Exception e) {
  163. return;
  164. }
  165.  
  166. try {
  167.  
  168. con = DriverManager.getConnection("jdbc:mysql://MY IP/breakpoint","root", "Secret");
  169. Statement st = con.createStatement ();
  170. Statement stm = con.createStatement();
  171. Statement st2 = con.createStatement();
  172. st.executeQuery("SELECT Users.Username,Users.Password,Avaimet.Avainrekisteriin FROM Users INNER JOIN Avaimet ");
  173. ResultSet rss = st.getResultSet();
  174.  
  175. String Accname = Name.getText();
  176. String Accpass = Password.getText();
  177. String Regkey = Key.getText();
  178. boolean Created = false;
  179.  
  180. while ( rss.next() )
  181. {
  182. String name = rss.getString("Username");
  183. String key = rss.getString("Avainrekisteriin");
  184.  
  185.  
  186. if (Accname.length() <= 3)
  187. Status.setText("Your account name has to be longer than 3 letters");
  188.  
  189. else if (Accpass.length() <= 3)
  190. Status.setText("Your account password has to be longer than 3 letters");
  191.  
  192.  
  193.  
  194. if ( Created == false && Accname.equalsIgnoreCase(name) && Regkey.equalsIgnoreCase(key) )
  195. {
  196. String Deletekey = "DELETE FROM Avaimet WHERE Avainrekisteriin = '"+key+"'";
  197. String StrSQL = "INSERT INTO Users (Username,Password,Banned,Level,Admin) " +
  198. "VALUES ('"+Accname+"','"+Accpass+"',"+0+",'3','0')";
  199. stm.executeUpdate(StrSQL);
  200. st2.executeUpdate(Deletekey);
  201. Created = true;
  202. System.out.print("ACC CREATED");
  203. Status.setText("Account created!");
  204. Form1.Status.setText("Account created!");
  205. this.dispose();
  206.  
  207. }
  208. else
  209. {
  210. Status.setText("Something failed!");
  211. }
  212.  
  213. }
  214.  
  215.  
  216. st.close();
  217. rss.close();
  218.  
  219.  
  220.  
  221.  
  222.  
  223. } catch (SQLException e) {
  224. System.out.println("Connection Failed! Check output console");
  225. e.printStackTrace();
  226. return;
  227. }
  228. }
  229.  
  230. private void CancelActionPerformed(java.awt.event.ActionEvent evt) {
  231. this.dispose();
  232. }
  233.  
  234. /**
  235.   * @param args the command line arguments
  236.   */
  237. public static void main(String args[]) {
  238. java.awt.EventQueue.invokeLater(new Runnable() {
  239. public void run() {
  240. new Regform().setVisible(true);
  241. }
  242. });
  243. }
  244.  
  245. // Variables declaration - do not modify
  246. private javax.swing.JButton Cancel;
  247. private javax.swing.JTextField Key;
  248. private javax.swing.JTextField Name;
  249. private javax.swing.JPasswordField Password;
  250. private javax.swing.JButton Proceed;
  251. private javax.swing.JLabel Status;
  252. private javax.swing.JLabel jLabel1;
  253. private javax.swing.JLabel jLabel2;
  254. private javax.swing.JLabel jLabel3;
  255. private javax.swing.JLabel jLabel4;
  256. private javax.swing.JLabel jLabel5;
  257. private javax.swing.JSeparator jSeparator1;
  258. // End of variables declaration
  259.  
  260. }

I think that maybe that INNER JOIN affects it somehow, I tried with left and right but then it gives sql error. Im hopeless.
By the way if you need to test the program in it self add me in msn samppa.l@hotmail.com.
Last edited by Silvershaft; Aug 22nd, 2009 at 4:35 pm.
Similar Threads
Reputation Points: 12
Solved Threads: 0
Junior Poster
Silvershaft is offline Offline
139 posts
since Jul 2009
Aug 28th, 2009
0

Re: Registering system, Unwanted behavior

hi, i could not test your program because it did not compile..but i think your problem could be in compairing objects rather than their values.
if ( Created == false && Accname.equalsIgnoreCase(name) && Regkey.equalsIgnoreCase(key) )

i think your problem could be in this line..here you are comparing if the two objects(in this case Accname and name) are the same object. You should try comparing the values. An '==' operator would do that.
Hope this helpss
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
zyaday is offline Offline
70 posts
since Jun 2008
Aug 28th, 2009
0

Re: Registering system, Unwanted behavior

I will try it and it didn't compile because it takes values from other file I have.
Reputation Points: 12
Solved Threads: 0
Junior Poster
Silvershaft is offline Offline
139 posts
since Jul 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Need quick help
Next Thread in Java Forum Timeline: JavaEE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC