当前位置: 技术问答>java相关
我想插入一条记录到库中,但有要判断username 是否存在于库中/(插入我以实现)<大家来看看)
来源: 互联网 发布时间:2015-01-26
本文导语: 这是我的程序代码:(绝对正确我以测试了),我想判断我"正在插入的新的username"是否 存在于库savemoney中,如果存在,就弹出对话框,说username以存在了,请重新输入, //触发事件对"新用户的提交:" void jButton1_actio...
这是我的程序代码:(绝对正确我以测试了),我想判断我"正在插入的新的username"是否
存在于库savemoney中,如果存在,就弹出对话框,说username以存在了,请重新输入,
//触发事件对"新用户的提交:"
void jButton1_actionPerformed(ActionEvent e) {
oEreport = new ereport();//为取出id作准备
try{
oEreport.connectToDB();//取出记录数
}
catch(Exception e1)
{
System.out.println(e1);
}
strName = this.jTextFieldnewname.getText();
floMoney = Float.valueOf(jTextFieldmoney.getText());
strType = this.jComboBoxtype.getSelectedItem().toString();
if(strType =="用户名") {
String U="U";
strType = U;
}
if( strType== "组名"){
String G="G";
strType = G;
}
ResultSet resDate = null;//为取出id作准备
try{
String strSql = "select max("id") as maxnum from Savemoney";
resDate = oEreport.executeQuery(strSql);
if(resDate.next()){
iCount = resDate.getInt("maxnum");
iCount=iCount+1;
}
}
catch(Exception e1)
{
System.out.println(e1);
}
try{
//插入记录
strSql = "Insert into Savemoney "+
"Values ("+iCount+",'"+strName+"','"+strType+"',getdate(),"+floMoney +")";
// System.out.println(strSql);
int iOK=oEreport.executeUpdate(strSql);
if(iOK==1){
JOptionPane.showMessageDialog(null, "更新成功!", "更新成功!", JOptionPane.ERROR_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null, "更新失败!", "更新失败!", JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e1){
System.out.println(e1);
}
}
存在于库savemoney中,如果存在,就弹出对话框,说username以存在了,请重新输入,
//触发事件对"新用户的提交:"
void jButton1_actionPerformed(ActionEvent e) {
oEreport = new ereport();//为取出id作准备
try{
oEreport.connectToDB();//取出记录数
}
catch(Exception e1)
{
System.out.println(e1);
}
strName = this.jTextFieldnewname.getText();
floMoney = Float.valueOf(jTextFieldmoney.getText());
strType = this.jComboBoxtype.getSelectedItem().toString();
if(strType =="用户名") {
String U="U";
strType = U;
}
if( strType== "组名"){
String G="G";
strType = G;
}
ResultSet resDate = null;//为取出id作准备
try{
String strSql = "select max("id") as maxnum from Savemoney";
resDate = oEreport.executeQuery(strSql);
if(resDate.next()){
iCount = resDate.getInt("maxnum");
iCount=iCount+1;
}
}
catch(Exception e1)
{
System.out.println(e1);
}
try{
//插入记录
strSql = "Insert into Savemoney "+
"Values ("+iCount+",'"+strName+"','"+strType+"',getdate(),"+floMoney +")";
// System.out.println(strSql);
int iOK=oEreport.executeUpdate(strSql);
if(iOK==1){
JOptionPane.showMessageDialog(null, "更新成功!", "更新成功!", JOptionPane.ERROR_MESSAGE);
}
else{
JOptionPane.showMessageDialog(null, "更新失败!", "更新失败!", JOptionPane.ERROR_MESSAGE);
}
}
catch(Exception e1){
System.out.println(e1);
}
}
|
select count(username) from Savemoney where username = ".."
如返回纪录数>0 即纪录存在
如返回纪录数>0 即纪录存在
|
实际上
ResultSet resDate = null;
String strSql = "select * from Savemoney where username = '" + username + "'";
resDate = oEreport.executeQuery(strSql);
if(resDate!=null)
{
说明有纪录
}
ResultSet resDate = null;
String strSql = "select * from Savemoney where username = '" + username + "'";
resDate = oEreport.executeQuery(strSql);
if(resDate!=null)
{
说明有纪录
}
|
一个简单的办法是设置username为primary key。在插入失败后不处理(插入不成功,就是key重复。
另一个办法是,每次插入前检查:)
另一个办法是,每次插入前检查:)