This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JDBC and Relational Databases and the fly likes Error while executing the jdbc code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Error while executing the jdbc code" Watch "Error while executing the jdbc code" New topic
Author

Error while executing the jdbc code

ujwwala tem
Ranch Hand

Joined: Feb 10, 2010
Posts: 87
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
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2551
    
  10

Hi ujwwala,

Please use code tags.

Regards, Jan


OCUP UML fundamental and ITIL foundation
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5852
    
    7

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.


JBoss In Action
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error while executing the jdbc code
 
It's not a secret anymore!