java—如何检查重复输入并防止将其添加到数据库中

zf2sa74q  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(378)

大家好,我如何检查重复输入(添加学生)和如何防止它。它说当我创建一个表时,我插入数据库的输入都是重复的,显然不是。与此相一致,如何检测和检查插入的输入是否与以前的输入重复。这是控制台中的第一个输出这是继续
这是我的密码:
主.java

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. //import java.sql.ResultSet;
  5. //import java.sql.SQLException;
  6. //import java.sql.Statement;

公共类主{

  1. public static void main (String args[]) throws Exception {
  2. StudentDAO x1= new StudentDAO();
  3. x1.getConnection();
  4. x1.makeTable(); //create Table
  5. x1.addstud("1","Yves Francisco", "2000000001","5","CpE","Male");
  6. x1.addstud("2","Lance Eco", "2000000002","5","CpE","Male");
  7. x1.addstud("3","Karlos Castillo", "2000000003","5","CpE","Male");
  8. x1.addstud("4","Glenn Bordonada", "2000000004","5","ECE","Male");
  9. x1.addstud("5","JM Enriquez", "2000000005","5","ECE","Male");
  10. x1.addstud("6","John Martinez", "2000000006","2","ECE","Male");
  11. x1.addstud("7","Timothy Tolentino", "2000000007","4","IT","Male");
  12. x1.addstud("8","Kyle Dacaymat", "2000000008","3","CpE","Male");
  13. x1.addstud("9","Dom Benedictos", "2000000009","1","IT","Male");
  14. x1.addstud("10","Lance Roque", "2000000010","1","ECE","Male");
  15. x1.addstud("11", "Vegeta", "2000000011", "1", "ME", "Male"); // added Student
  16. //x1.addstud("12", "Vegeta", "2000000011", "1", "ME", "Male"); // pang check if entered input is a duplicate of a field of studName, studNumber, etc.
  17. //x1.delstud(); // delete student
  18. //x1.updstuddb();
  19. x1.showdb();
  20. }

}
studentdao.java文件

  1. //import java.util.*;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. //import java.sql.ArrayList;

公办班学生{

  1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. }
  4. public void showdb() throws Exception
  5. {
  6. try {
  7. Connection con = getConnection();
  8. String query = "SELECT *FROM studinfo;";
  9. PreparedStatement showstuddb= con.prepareStatement(query);
  10. ResultSet rs = showstuddb.executeQuery();
  11. System.out.println("Showing Database.....................");
  12. System.out.println("============================================================@ THE CONTENT OF THE DATABASE @============================================================");
  13. while (rs.next())
  14. {
  15. String idko = rs.getObject(1).toString();
  16. String ngalan = rs.getObject(2).toString();
  17. String numko=rs.getObject(3).toString();
  18. String baitang=rs.getObject(4).toString();
  19. String kurso=rs.getObject(5).toString();
  20. String kasarian=rs.getObject(6).toString();
  21. System.out.println("My ID number is: "+ idko + " Name is: " + ngalan + " Student Number is: " + numko + " Year/Level is: "+ baitang +" Course is: " + kurso + " Sex is: " + kasarian);
  22. }
  23. showstuddb.close();
  24. System.out.println("Nothing follows.....................");
  25. con.close();
  26. }
  27. catch (Exception e)
  28. {
  29. System.out.println("Error on showing contents of database!!!" + e.getMessage());
  30. }
  31. }
  32. public void updstuddb () throws Exception
  33. {
  34. try {
  35. Connection con = getConnection();
  36. String query = "UPDATE studinfo SET studName=?, studNum=?, studYrLvl=?, studKors=?, studGender=? WHERE studid=?";
  37. PreparedStatement studup = con.prepareStatement(query);
  38. studup.setString(1, "Kakarot"); //This will be the replacement
  39. studup.setString(2, "2000000020");
  40. studup.setString(3, "2");
  41. studup.setString(4, "IT");
  42. studup.setString(5, "dafq");
  43. studup.setString(6, "1");// The unique element among the content of the database which is used to determine which is to update
  44. studup.executeUpdate();
  45. System.out.println("THE LIST HAS BEEN UPDATED @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
  46. studup.close();
  47. con.close();
  48. }
  49. catch (Exception e)
  50. {
  51. System.out.println("Error in updating the database!!!" + e.getMessage());
  52. }
  53. }
  54. public void delstud()
  55. {
  56. try {
  57. Connection con = getConnection();
  58. String query = "DELETE FROM studinfo WHERE studid=?";
  59. PreparedStatement userdel = con.prepareStatement(query);
  60. userdel.setString(1, "12"); // To determine what to delete in the Database
  61. //userdel.setString(1, "2000000002");
  62. //userdel.setString(1, "2000000003");
  63. //userdel.setString(1, "2000000004");
  64. //userdel.setString(1, "2000000005");
  65. //userdel.setString(1, "2000000006");
  66. //userdel.setString(1, "2000000007");
  67. //userdel.setString(1, "2000000008");
  68. //userdel.setString(1, "2000000009");
  69. //userdel.setString(1, "2000000010");
  70. userdel.execute();
  71. userdel.close();
  72. System.out.println("Data is now deleted!!!");
  73. con.close();
  74. }
  75. catch (Exception e)
  76. {
  77. System.out.println("Error!!!. Data is not deleted " + e.getMessage());
  78. }
  79. }
  80. public void addstud(String studid, String studName, String studNum, String studYrLvl, String studKors, String studGender) throws Exception
  81. {
  82. //String var1 = "Yves Francisco";
  83. //String num1 = "2000000001";
  84. //String num2 = "5";
  85. //String var2 = "CpE";
  86. //String var3 = "Male";
  87. try {
  88. Connection con = getConnection();
  89. PreparedStatement posted= con.prepareStatement("INSERT INTO studinfo (studid, studName, studNum, studYrLvl, studKors, studGender) VALUES (?,?,?,?,?,?)");
  90. int y=1;
  91. posted.setString(y++, studid);
  92. posted.setString(y++, studName);
  93. posted.setString(y++, studNum);
  94. posted.setString(y++, studYrLvl);
  95. posted.setString(y++, studKors);
  96. posted.setString(y++, studGender);
  97. posted.executeUpdate(); // Manipulate or Update table
  98. posted.close();
  99. //con.close();
  100. }
  101. catch (Exception e)
  102. {
  103. System.out.println("Error on adding columns!!!" + e.getMessage());
  104. }
  105. finally
  106. {
  107. System.out.println("Insert Successful!");
  108. }
  109. //FOR DUPLICATE INPUTS!!!!!!!!!!!!!!!!
  110. try
  111. {
  112. Connection con = getConnection();
  113. String query = "SELECT studName, studNum, studYrLvl, studKors, studGender FROM studinfo WHERE studName=?, studNum=?, studYrLvl=?, studKors=?, studGender=?";
  114. PreparedStatement checkdup = con.prepareStatement(query);
  115. ResultSet rs=checkdup.executeQuery();
  116. while (rs.next())
  117. {
  118. boolean dup1=rs.getObject(2).equals(studName);
  119. boolean dup2=rs.getObject(3).equals(studNum);
  120. boolean dup3=rs.getObject(4).equals(studYrLvl);
  121. boolean dup4=rs.getObject(5).equals(studKors);
  122. boolean dup5=rs.getObject(6).equals(studGender);
  123. System.out.println("The name you entered is: " + dup1 + " The student number you entered is: " + dup2 + " The Yr/Lvl you entered: " + dup3 + " The Course you entered: " + dup4 + " The Sex you entered is: " + dup5);
  124. }
  125. con.close();
  126. }
  127. catch (Exception e)
  128. {
  129. System.out.println("You entered a duplicate value!!. Try Again! ");
  130. System.out.println("Take note that the entered Duplicate value is entered in the Database");
  131. System.out.println("Remove the duplicate value using delstud() method!!!");
  132. }
  133. }
  134. public void makeTable() throws Exception
  135. {
  136. try
  137. {
  138. Connection con= getConnection();
  139. PreparedStatement create = con.prepareStatement("CREATE TABLE IF NOT EXISTS studinfo (studid INT NOT NULL AUTO_INCREMENT PRIMARY KEY, studName varchar(255), studNum varchar(30), studYrLvl varchar(2), studKors varchar(30), studGender varchar(10));");
  140. create.executeUpdate();
  141. System.out.println("TABLE IS CREATED!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  142. create.close();
  143. con.close();
  144. }
  145. catch (Exception e)
  146. {
  147. System.out.println("Error on creating table!!. Table not created!"+e.getMessage());
  148. }
  149. finally
  150. {
  151. System.out.println("Table created!");
  152. };
  153. }
  154. public Connection getConnection () throws Exception {
  155. try {
  156. String driver = "com.mysql.jdbc.Driver";
  157. String url = "jdbc:mysql://localhost:3306/mydb";
  158. String username= "root";
  159. String password = "root";
  160. Class.forName(driver);
  161. Connection conn= DriverManager.getConnection(url, username, password);
  162. System.out.println("You are now Connected!!");
  163. return conn; // Return if it is successfully connected!
  164. }
  165. catch (Exception e)
  166. {
  167. System.out.println("Connection not Established!"+e.getMessage());
  168. }
  169. return null; // Return if unsuccessful
  170. }

}

scyqe7ek

scyqe7ek1#

问题不是表中的重复值,问题是代码中有错误。我不会像你那样做,因为如果你使用try-catch,不管怎样,如果你的代码中有问题或错误(寻找重复的代码),它会回答你有一个重复的代码。否则,即使你有成千上万的副本,如果你没有得到一个错误,你不会得到一个副本。
不管怎样,代码中的错误是没有为checkdup prepared语句赋予任何值,应该将其更改为以下内容:

  1. //FOR DUPLICATE INPUTS!!!!!!!!!!!!!!!!
  2. try
  3. {
  4. Connection con = getConnection();
  5. String query = "SELECT studName, studNum, studYrLvl, studKors, studGender FROM studinfo WHERE studName=?, studNum=?, studYrLvl=?, studKors=?, studGender=?";
  6. PreparedStatement checkdup = con.prepareStatement(query);
  7. checkdup.setString(1, studName);
  8. checkdup.setString(2, studNum);
  9. checkdup.setString(3, studYrLvl);
  10. checkdup.setString(4, studKors);
  11. checkdup.setString(5, studGender);
  12. ResultSet rs=checkdup.executeQuery();
  13. while (rs.next())
  14. {
  15. boolean dup1=rs.getObject(2).equals(studName);
  16. boolean dup2=rs.getObject(3).equals(studNum);
  17. boolean dup3=rs.getObject(4).equals(studYrLvl);
  18. boolean dup4=rs.getObject(5).equals(studKors);
  19. boolean dup5=rs.getObject(6).equals(studGender);
  20. System.out.println("The name you entered is: " + dup1 + " The student number you entered is: " + dup2 + " The Yr/Lvl you entered: " + dup3 + " The Course you entered: " + dup4 + " The Sex you entered is: " + dup5);
  21. }
  22. con.close();
  23. }
  24. catch (Exception e)
  25. {
  26. System.out.println("You entered a duplicate value!!. Try Again! ");
  27. System.out.println("Take note that the entered Duplicate value is entered in the Database");
  28. System.out.println("Remove the duplicate value using delstud() method!!!");
  29. }

编辑
有一种简单的方法可以查找重复项,您只需计算代码中有多少重复项:

  1. //FOR DUPLICATE INPUTS!!!!!!!!!!!!!!!!
  2. try
  3. {
  4. Connection con = getConnection();
  5. String query = "SELECT studName, studNum, studYrLvl, studKors, studGender FROM studinfo WHERE studName=?, studNum=?, studYrLvl=?, studKors=?, studGender=?";
  6. PreparedStatement checkdup = con.prepareStatement(query);
  7. checkdup.setString(1, studName);
  8. checkdup.setString(2, studNum);
  9. checkdup.setString(3, studYrLvl);
  10. checkdup.setString(4, studKors);
  11. checkdup.setString(5, studGender);
  12. ResultSet rs=checkdup.executeQuery();
  13. int dup=0;
  14. while (rs.next())
  15. {
  16. dup+=1;
  17. }
  18. if (dup>1 ) {
  19. System.out.println("You entered a duplicate value!!. Try Again! ");
  20. System.out.println("Take note that the entered Duplicate value is entered in the Database");
  21. System.out.println("Remove the duplicate value using delstud() method!!!");
  22. }
  23. con.close();
  24. }
  25. catch (Exception e)
  26. {
  27. System.out.println("There is an error ");
  28. }

在insert语句中,还需要更改一些内容。这个 studid 是一个自动递增列,因此不必将其放入insert中。应该是这样的:

  1. Connection con = getConnection();
  2. PreparedStatement posted= con.prepareStatement("INSERT INTO studinfo (studName, studNum, studYrLvl, studKors, studGender) VALUES (?,?,?,?,?)");
  3. int y=1;
  4. posted.setString(y++, studName);
  5. posted.setString(y++, studNum);
  6. posted.setString(y++, studYrLvl);
  7. posted.setString(y++, studKors);
  8. posted.setString(y++, studGender);
  9. posted.executeUpdate(); // Manipulate or Update table
  10. posted.close();
展开查看全部

相关问题