• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Error while executing the jdbc code

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,


I am calling simalateDb.java from action

public boolean saveToDb(CustomerAction c) throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException


{

String url= "jdbc:mysql://localhost:3306/";
String dbName="mysql";
String driver="com.mysql.jdbc.Driver";
String userName="uj";
String password="uj";
Connection conn=null;
PreparedStatement pstmt=null;
boolean isAdded = false;
int i=0;



try {

Class.forName(driver).newInstance();
conn= DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");



pstmt=conn.prepareStatement(INSERT_CUSTOMER);


pstmt.setString(i++,c.getName());
pstmt.setInt(i++,c.getAge());
pstmt.setString(i++,c.getEmail());
pstmt.setInt(i++,c.getTelephone());
pstmt.execute();
//conn.commit();
isAdded=true;
System.out.println("successfully Added");


}
catch (SQLException e) {
System.out.println("Error inserting the customer data");
try {
if (!(conn == null)) {
conn.rollback();
}



}

catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}


}
return isAdded;
}
public static final String INSERT_CUSTOMER =
" insert into customer (name, age, email,phone "+")values(?,?,?,?)";

com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Can't call rollback when autocommit=true

I haven't set any autocommit to true if by default its trute so what should I do here?

Please reply
 
Bartender
Posts: 2661
19
Netbeans IDE C++ Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ujwwala,

Please use code tags.

Regards, Jan
 
author
Posts: 5856
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use: Connection.setAutocommit()
http://java.sun.com/j2se/1.5.0/docs/api/java/sql/Connection.html#setAutoCommit(boolean)

The javdocs are your friend! You should have them bookmarked.
 
What are you doing? You are supposed to be reading this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic