| |

VerySource

 Forgot password?
 Register
Search
View: 1340|Reply: 10

Why stmt.executeUpdate returns 1 without inserting into the database

[Copy link]

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-3-13 01:30:01
| Show all posts |Read mode
Why stmt.executeUpdate returns 1 without inserting into the database
I took the same sql statement and run it in the query analyzer of sql server, but the data was inserted normally.
Even more strange is if I run the program again and then run the same sql statement in the query analyzer, the automatic number of the inserted record is 2 larger than the previous one, that is, if the automatic number of a previous record is 27, the number of the inserted one is 29, but the one in the program is not inserted! Didn't return any error message, is this what happened? ? ?
Have you ever encountered such a situation?
I took the connection from the datasource and inserted it with a statement
Reply

Use magic Report

2

Threads

8

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 China

Post time: 2020-6-7 23:45:01
| Show all posts
slq posted ~! Also look at your connection. No problem!~
Reply

Use magic Report

0

Threads

63

Posts

42.00

Credits

Newbie

Rank: 1

Credits
42.00

 China

Post time: 2020-6-8 07:45:01
| Show all posts
Did you commit, is it normal
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-6-12 23:45:01
| Show all posts
The connection is definitely no problem, because the connection is used before
The insertUser method in the User class:
public int insertUser(DataSource ds){
int result=0;
Connection conn;
try {
conn = ds.getConnection();
PreparedStatement pstmt=conn.prepareStatement("insert into tb_customer(username,password,cusTrueName,"+
"cusAddress, cusPostcode, cusProvince, cusCity, cusTel, cusEmail, cusCardType, cusCardNo) "+
"values(?,?,?,?,?,?,?,?,?,?,?)");
pstmt.setString(1,username);
pstmt.setString(2,password);
pstmt.setString(3,cusTrueName);
pstmt.setString(4,cusAddress);
pstmt.setString(5,cusPostcode);
pstmt.setString(6,cusProvince);
pstmt.setString(7,cusCity);
pstmt.setString(8,cusTel);
pstmt.setString(9,cusEmail);
pstmt.setString(10,cusCardType);
pstmt.setString(11,cusCardNo);
pstmt.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}

The execute method in registerAction:
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response){
The
RegisterForm registerForm = (RegisterForm) form;
The
String username=registerForm.getUsername();
String password=registerForm.getPassword();
String cusTrueName=registerForm.getCusTrueName();
String cusAddress=registerForm.getCusAddress();
String cusPostcode=registerForm.getCusPostcode();
String cusProvince=registerForm.getCusProvince();
String cusCity=registerForm.getCusCity();
String cusTel=registerForm.getCusTel();
String cusEmail=registerForm.getCusEmail();
String cusCardType=registerForm.getCusCardType();
String cusCardNo=registerForm.getCusCardNo();
The
ActionMessages errors=new ActionMessages();
DataSource ds=getDataSource(request);
DBUtil dbu=new DBUtil(ds);
if(UserDao.userExist(dbu,username)){
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.username.exist"));
if(!errors.isEmpty()){
saveErrors(request,errors);
}
dbu.close();
return mapping.findForward("failure");
}
dbu.close();
The
User user=new User(username,
password,
cusTrueName,
cusAddress,
cusPostcode,
cusProvince,
cusCity,
cusTel,
cusEmail,
cusCardType,
cusCardNo);

int result=0;
result=user.insertUser(ds);
if(result!=1){
errors.add(ActionMessages.GLOBAL_MESSAGE,
new ActionMessage("errors.insert.failed"));
if(!errors.isEmpty()){
saveErrors(request,errors);
}
return mapping.findForward("failure");
}
HttpSession session=request.getSession();
session.setAttribute("username",username);
The
return mapping.findForward("success");
}

}
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-6-13 22:30:01
| Show all posts
Sorry, I just changed these two paragraphs just now. I accidentally commented out the executeUpdate of pstmt, so I didn’t paste it. It is the same to add executeUpdate before pstmt.
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-6-16 19:45:01
| Show all posts
There is a new discovery, if I change the Connection conn = ds.getConnection() in insertUser to:
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String URL="jdbc:jtds:sqlserver://localhost:1433/db_webshop;user=sa;password=";
Connection conn=DriverManager.getConnection(URL);
The rest is unchanged, so that data can be inserted, which shows that the connection conn = ds.getConnection() has a problem with the conn here, but this conn is not null, and I used this conn in the previous, it can connect to the database normally Yes, what's going on?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-19 13:30:01
| Show all posts
Data source configuration file

Is it configured to automatically submit to false,
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-20 13:00:02
| Show all posts
It feels like there is no submission.
Reply

Use magic Report

1

Threads

6

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

 Author| Post time: 2020-6-25 15:00:01
| Show all posts
My configuration file looks like this:
<data-source type="org.apache.commons.dbcp.BasicDataSource">
      <set-property property="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
      <set-property property="url" value="jdbc:jtds:sqlserver://localhost:1433/db_webshop"/>
      <set-property property="username" value="sa"/>
      <set-property property="password" value=""/>
      <set-property property="maxActive" value="10"/>
      <set-property property="maxWait" value="5000"/>
      
      <set-property property="minCount" value="4"/>
      <set-property property="maxCount" value="10"/>
      
      <set-property property="defaultAutoCommit" value="false"/>
      <set-property property="defaultReadOnly" value="false"/>
    </data-source>
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-6-25 21:30:01
| Show all posts
<set-property property="defaultAutoCommit" value="false"/>

This requires you to submit things in the program [conn.commit()]

If nothing special, set it to true
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list