大家好,我如何检查重复输入(添加学生)和如何防止它。它说当我创建一个表时,我插入数据库的输入都是重复的,显然不是。与此相一致,如何检测和检查插入的输入是否与以前的输入重复。这是控制台中的第一个输出这是继续
这是我的密码:
主.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
//import java.sql.ResultSet;
//import java.sql.SQLException;
//import java.sql.Statement;
公共类主{
public static void main (String args[]) throws Exception {
StudentDAO x1= new StudentDAO();
x1.getConnection();
x1.makeTable(); //create Table
x1.addstud("1","Yves Francisco", "2000000001","5","CpE","Male");
x1.addstud("2","Lance Eco", "2000000002","5","CpE","Male");
x1.addstud("3","Karlos Castillo", "2000000003","5","CpE","Male");
x1.addstud("4","Glenn Bordonada", "2000000004","5","ECE","Male");
x1.addstud("5","JM Enriquez", "2000000005","5","ECE","Male");
x1.addstud("6","John Martinez", "2000000006","2","ECE","Male");
x1.addstud("7","Timothy Tolentino", "2000000007","4","IT","Male");
x1.addstud("8","Kyle Dacaymat", "2000000008","3","CpE","Male");
x1.addstud("9","Dom Benedictos", "2000000009","1","IT","Male");
x1.addstud("10","Lance Roque", "2000000010","1","ECE","Male");
x1.addstud("11", "Vegeta", "2000000011", "1", "ME", "Male"); // added Student
//x1.addstud("12", "Vegeta", "2000000011", "1", "ME", "Male"); // pang check if entered input is a duplicate of a field of studName, studNumber, etc.
//x1.delstud(); // delete student
//x1.updstuddb();
x1.showdb();
}
}
studentdao.java文件
//import java.util.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
//import java.sql.ArrayList;
公办班学生{
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public void showdb() throws Exception
{
try {
Connection con = getConnection();
String query = "SELECT *FROM studinfo;";
PreparedStatement showstuddb= con.prepareStatement(query);
ResultSet rs = showstuddb.executeQuery();
System.out.println("Showing Database.....................");
System.out.println("============================================================@ THE CONTENT OF THE DATABASE @============================================================");
while (rs.next())
{
String idko = rs.getObject(1).toString();
String ngalan = rs.getObject(2).toString();
String numko=rs.getObject(3).toString();
String baitang=rs.getObject(4).toString();
String kurso=rs.getObject(5).toString();
String kasarian=rs.getObject(6).toString();
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);
}
showstuddb.close();
System.out.println("Nothing follows.....................");
con.close();
}
catch (Exception e)
{
System.out.println("Error on showing contents of database!!!" + e.getMessage());
}
}
public void updstuddb () throws Exception
{
try {
Connection con = getConnection();
String query = "UPDATE studinfo SET studName=?, studNum=?, studYrLvl=?, studKors=?, studGender=? WHERE studid=?";
PreparedStatement studup = con.prepareStatement(query);
studup.setString(1, "Kakarot"); //This will be the replacement
studup.setString(2, "2000000020");
studup.setString(3, "2");
studup.setString(4, "IT");
studup.setString(5, "dafq");
studup.setString(6, "1");// The unique element among the content of the database which is used to determine which is to update
studup.executeUpdate();
System.out.println("THE LIST HAS BEEN UPDATED @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
studup.close();
con.close();
}
catch (Exception e)
{
System.out.println("Error in updating the database!!!" + e.getMessage());
}
}
public void delstud()
{
try {
Connection con = getConnection();
String query = "DELETE FROM studinfo WHERE studid=?";
PreparedStatement userdel = con.prepareStatement(query);
userdel.setString(1, "12"); // To determine what to delete in the Database
//userdel.setString(1, "2000000002");
//userdel.setString(1, "2000000003");
//userdel.setString(1, "2000000004");
//userdel.setString(1, "2000000005");
//userdel.setString(1, "2000000006");
//userdel.setString(1, "2000000007");
//userdel.setString(1, "2000000008");
//userdel.setString(1, "2000000009");
//userdel.setString(1, "2000000010");
userdel.execute();
userdel.close();
System.out.println("Data is now deleted!!!");
con.close();
}
catch (Exception e)
{
System.out.println("Error!!!. Data is not deleted " + e.getMessage());
}
}
public void addstud(String studid, String studName, String studNum, String studYrLvl, String studKors, String studGender) throws Exception
{
//String var1 = "Yves Francisco";
//String num1 = "2000000001";
//String num2 = "5";
//String var2 = "CpE";
//String var3 = "Male";
try {
Connection con = getConnection();
PreparedStatement posted= con.prepareStatement("INSERT INTO studinfo (studid, studName, studNum, studYrLvl, studKors, studGender) VALUES (?,?,?,?,?,?)");
int y=1;
posted.setString(y++, studid);
posted.setString(y++, studName);
posted.setString(y++, studNum);
posted.setString(y++, studYrLvl);
posted.setString(y++, studKors);
posted.setString(y++, studGender);
posted.executeUpdate(); // Manipulate or Update table
posted.close();
//con.close();
}
catch (Exception e)
{
System.out.println("Error on adding columns!!!" + e.getMessage());
}
finally
{
System.out.println("Insert Successful!");
}
//FOR DUPLICATE INPUTS!!!!!!!!!!!!!!!!
try
{
Connection con = getConnection();
String query = "SELECT studName, studNum, studYrLvl, studKors, studGender FROM studinfo WHERE studName=?, studNum=?, studYrLvl=?, studKors=?, studGender=?";
PreparedStatement checkdup = con.prepareStatement(query);
ResultSet rs=checkdup.executeQuery();
while (rs.next())
{
boolean dup1=rs.getObject(2).equals(studName);
boolean dup2=rs.getObject(3).equals(studNum);
boolean dup3=rs.getObject(4).equals(studYrLvl);
boolean dup4=rs.getObject(5).equals(studKors);
boolean dup5=rs.getObject(6).equals(studGender);
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);
}
con.close();
}
catch (Exception e)
{
System.out.println("You entered a duplicate value!!. Try Again! ");
System.out.println("Take note that the entered Duplicate value is entered in the Database");
System.out.println("Remove the duplicate value using delstud() method!!!");
}
}
public void makeTable() throws Exception
{
try
{
Connection con= getConnection();
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));");
create.executeUpdate();
System.out.println("TABLE IS CREATED!!!~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
create.close();
con.close();
}
catch (Exception e)
{
System.out.println("Error on creating table!!. Table not created!"+e.getMessage());
}
finally
{
System.out.println("Table created!");
};
}
public Connection getConnection () throws Exception {
try {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/mydb";
String username= "root";
String password = "root";
Class.forName(driver);
Connection conn= DriverManager.getConnection(url, username, password);
System.out.println("You are now Connected!!");
return conn; // Return if it is successfully connected!
}
catch (Exception e)
{
System.out.println("Connection not Established!"+e.getMessage());
}
return null; // Return if unsuccessful
}
}
1条答案
按热度按时间scyqe7ek1#
问题不是表中的重复值,问题是代码中有错误。我不会像你那样做,因为如果你使用try-catch,不管怎样,如果你的代码中有问题或错误(寻找重复的代码),它会回答你有一个重复的代码。否则,即使你有成千上万的副本,如果你没有得到一个错误,你不会得到一个副本。
不管怎样,代码中的错误是没有为checkdup prepared语句赋予任何值,应该将其更改为以下内容:
编辑
有一种简单的方法可以查找重复项,您只需计算代码中有多少重复项:
在insert语句中,还需要更改一些内容。这个
studid
是一个自动递增列,因此不必将其放入insert中。应该是这样的: