This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Problems with my code, any help?

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot seem to get the insert statement right somehow, select works fine, but here is the code if anyone can help, oh and i'm using DAO, JDBC.
note, this is an extract from some of the classes i'm having trouble with, and i'm using mySQL.
Thanks
Jamie

///////////////////code below///////////////
public class Db_Connection {

Connection myConnection;
Rate rateSQL;
}

public Db_Connection()
{
rateSQL = new Rate();
}

public void testsomething() throws SQLException
{
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}
catch (Exception e)
{
System.out.println("Failed to load MySQL driver");
System.exit(-1);
}
//initial connection to the database
System.out.println("My conn");
myConnection = DriverManager.getConnection(URL, USER, PASSWORD);
insertRate();
closeStatements();
}

public void insertRate() throws SQLException {
RateData rate = new RateData();
//Parameters:- timestamp in format yyyy-mm-dd hh:mm:ss.fffffffff
Timestamp ts = new Timestamp(0);
ts = Timestamp.valueOf("2001-07-06 14:25:29.9") ;
rate.setDate(ts);
rate.setLocalrate(new Integer(3));
rate.setUSDrate(new Integer(2));
rateSQL.insert(myConnection, rate);
}

//from rateSQL class
private PreparedStatement myInsertPreparedStatement;

public void insert(Connection theConnection, RateData theInsertData) throws SQLException
{
SQLException exception = null;
if (myInsertPreparedStatement == null)
{
myInsertPreparedStatement = theConnection.prepareStatement("INSERT INTO `microtech`.`rate` ( `Date`, `Local_rate`, `USD_rate`) VALUES (?, ?, ?)");
}

// Set Date
if (theInsertData.getDate() == null)
{
throw new SQLException("Error ate has not been set, and cannot be null");
}
myInsertPreparedStatement.setTimestamp(1, theInsertData.getDate());

// Set Local_rate
if (theInsertData.getLocalrate() == null)
{
myInsertPreparedStatement.setNull(2, java.sql.Types.INTEGER);
}
else
{
myInsertPreparedStatement.setInt(2, theInsertData.getLocalrate().intValue());
}

// Set USD_rate
if (theInsertData.getUSDrate() == null)
{
myInsertPreparedStatement.setNull(3, java.sql.Types.INTEGER);
}
else
{
myInsertPreparedStatement.setInt(3, theInsertData.getUSDrate().intValue());
}
myInsertPreparedStatement.executeUpdate();
}
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what error are you getting,is there any exception ??

If yes can you paste that

thanks
 
Jamie Buchan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There currently is no errors thrown, thats the odd thing, just nothing is added to the database, i'm going to put some more try/catch blocks in anyway to find something out.
 
Shailesh Chandra
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you commiting after insert or how are you maintaining transaction ?
[ March 20, 2005: Message edited by: Shailesh Chandra ]
 
Jamie Buchan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its not that dumn a question, as I don't have a clue what you are talking about

the code was generated with jdatabase wizard as it would take me a long time to generate the whole thing, but it looks like I might have to in the end.
 
Jamie Buchan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I figured it out in the end, as I did not understand what you meant by this

Basically i'm still very new to JDBC so I did a search and then checked my code and noticed that I was not closing the connection properly, so it was that simple, thanks for your help as I wouldn't have found out without it

Jamie
[ March 19, 2005: Message edited by: Jamie Buchan ]
 
author & internet detective
Posts: 39790
797
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamie,
Thanks for posting the solution about closing the connection. It helps people reading the thread in the future. And welcome to JavaRanch!

Shailesh,
"There's no such thing as a dumb question" here. JavaRanch is a friendly place for Java greenhorns. Remember we were all new to Java at some point.
 
Shailesh Chandra
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jamie Buchan:
its not that dumn a question, as I don't have a clue what you are talking about



Jamie,

I didn't mean to say that your question is dumb, I wanted to say that I am asking a dumb question that are you comitting your transaction.

It was never intended to you.
I apologize for same

Jeanne:
I agree more that 100% that "There's no such thing as a dumb question"

I have edited my post.It was not for any java ranch memeber but for me only

thanks
Shailesh
 
Jamie Buchan
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apoligies if you read my reply wrong, I was refering to your comment on whether you were stating a dumn question, I found that dumn question very usefull in searching for other help and you helped me sort it out.



Thanks Jamie
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!