我是java程序的新手。我使用eclipse和MYSQL创建了一个带登录界面的java程序。一旦我登录重复登录成功消息2或3次。它的开口是主界面的2倍。这是我的代码。寻找解决方案。
Sub NINODifferences()
Dim ws1 As Variant, ws2 As Variant, ws3 As Variant
Dim i As Long, j As Long
ws1 = ActiveWorkbook.Sheets("SheetA").UsedRange
ws2 = ActiveWorkbook.Sheets("SheetB").UsedRange
ReDim ws3(4, 0)
For i = 1 To UBound(ws1)
For j = 1 To UBound(ws2)
If Trim(ws1(i, 1)) = Trim(ws2(j, 1)) Then
If Trim(ws1(i, 17)) <> Trim(ws2(j, 24)) Then
ReDim Preserve ws3(4, count)
ws3(0, count) = ws1(i, 1)
ws3(1, count) = ws1(i, 2)
ws3(2, count) = ws1(i, 3)
ws3(3, count) = ws1(i, 17)
ws3(4, count) = ws2(i, 24)
count = count + 1
End If
End If
Next j
Next i
Call PasteArray(transposeArray(ws3), ActiveWorkbook.Sheets("NINO Differences").[A1])
Set ws1 = Nothing
Set ws2 = Nothing
End Sub
Sub PasteArray(data As Variant, rng As Range)
rng.Resize(UBound(data, 1) + 1, UBound(data, 2) + 1) = data
End Sub
Function transposeArray(data)
If IsEmpty(data) Then Exit Function
ReDim r(UBound(data, 2), UBound(data))
For i = LBound(r) To UBound(r)
For j = LBound(r, 2) To UBound(r, 2)
r(i, j) = data(j, i)
Next j
Next i
transposeArray = r
End Function
答案 0 :(得分:2)
在where
中添加query
子句,因为您将从用户表中获取所有行。然后,由于while
循环,您将获得2,3或更多消息和窗口,具体取决于行数。
答案 1 :(得分:0)
通过过滤查询级别的用户名和密码字段,只选择表格的相关记录要简单得多。
还可以使用PreparedStatement
来处理转义值,并防止SQL注入。
以下示例假设您的表格中的相关列名为username
和password
,只需将其更改为列的实际名称。
// just change the column names to fit your table's columns
String query = "SELECT username from xyzlibrary.user_account WHERE username = ? AND password = ?";
// create a PreparedStatement which will escape values correctly
PreparedStatement st = con.prepareStatement(query);
// replace values
st.setString(1,tfusername.getText());
st.setString(2,tfpasswordField.getText());
ResultSet rs=st.executeQuery();
// if the result is not empty, the login is a success
if(rs.next()){
JOptionPane.showMessageDialog(null, "Login successfull!");
Admin ad = new Admin();
ad.setVisible(true);
dispose();
}
else{
JOptionPane.showMessageDialog(null, "Incorrect User ID or Password!");
}
答案 2 :(得分:0)
您可以执行的另一件事是在打印出已接受的登录信息后添加返回语句。这将解决您的直接问题。但从长远来看,我建议您在SQL Select语句中添加 SQL WHERE 子句
答案 3 :(得分:0)
“登录成功”消息将打印多次,因为查询中会显示结果。因此,如果您有2个用户名,它将被打印2次。
为什么不将打印件移出while循环?通过这种方式,无论您获得多少结果,它都只会被打印一次。