File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes my java application doesn't update the table? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "my java application doesn Watch "my java application doesn New topic
Author

my java application doesn't update the table?

Tom Barns
Ranch Hand

Joined: Oct 27, 2000
Posts: 138
Hi all
I'm trying to be more familiar with jdbc,so i created my table
and i could insert data to it and retrieve data from it.
but i'm trying to update it ,for some reason i can't.
here is my code,any help will be appreciated.
Thank you.
===============
import java.sql.*;
import java.net.*;
public class ConnectToDatabaseCreateTable
{

//String s;
//public String getData()
public static void main(String a[])
{

try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//String url = "jdbc dbc:mydatabase";
String url = "jdbc dbc:databse1";
Connection connection =
DriverManager.getConnection(url, "", "");

Statement st=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
//Statement st=connection.createStatement();
/*
st.executeUpdate("CREATE TABLE COFFEES " +
"(COF_NAME VARCHAR(32), SUP_ID INTEGER, PRICE FLOAT, " +
"SALES INTEGER, TOTAL INTEGER)");
st.executeUpdate(
"INSERT INTO COFFEES " +
"VALUES ('Colombian', 101, 7.99, 0, 0)");
st.executeUpdate("INSERT INTO COFFEES " +
"VALUES ('French_Roast', 49, 8.99, 0, 0)");
st.executeUpdate("INSERT INTO COFFEES " +
"VALUES ('French_Roast', 49, 8.99, 0, 0)");

*/
String updateString = "UPDATE COFFEES " +
"SET SALES = 9999 " +
"WHERE COF_NAME LIKE 'Colombian'";
st.executeUpdate(updateString);

/*
String query = "SELECT COF_NAME, SALES FROM COFFEES " +
"WHERE COF_NAME LIKE 'Colombian'";
ResultSet rs = st.executeQuery(query);
while (rs.next()) {
String s = rs.getString("COF_NAME");
int n = rs.getInt("SALES");
System.out.println(n + " pounds of " + s +
" sold this week.");
}
*/
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}

//return "ssssssss";
}
}

Thanks for your help.
hemanth kumar
Ranch Hand

Joined: Mar 15, 2001
Posts: 55
Hi Tom
I copied ur code and ran it.
Guess what , it works.Yup the record is getting updated!!!
So not sure what is ur problem?.
hemanth kumar
Ranch Hand

Joined: Mar 15, 2001
Posts: 55
Hi Tom
I copied ur code and ran it.
Guess what , it works.Yup the record is getting updated!!!
So not sure what is ur problem?.
Does the DB that u r working support like statement.My DB was Access.Which is ur Database?
Replace the like with "=" and check it out.
Regds
Hemanth
Tom Barns
Ranch Hand

Joined: Oct 27, 2000
Posts: 138
Kumar,
thanks for the reply.
my db is access,i changed the LiKE to = and the problem still.
which no update for the table,is there any thing related to setting the odbc data source?
I'm getting always the same table.
any further help will be appreciated.
John Bateman
Ranch Hand

Joined: Mar 09, 2000
Posts: 320
Hi
Since Access uses a file structure and not a true database engine, check the permission on your .mdb file.
You may find that your access is read only.
But you can also test this by 'inserting' a record and seeing what happend.
If you post source code here please surround it by 'code' tags.. see the UBB Code reference.
Thanks.


SOURCE CODE should be SURROUNDED by "code" tags.
Tom Barns
Ranch Hand

Joined: Oct 27, 2000
Posts: 138
Thanks for your reply,i checked it,it is for read and write too.
here is my code:
code
import java.sql.*;
import java.net.*;
public class ConnectToDatabaseCreateTable
{
public static void main(String a[])
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//String url = "jdbc dbc:mydatabase";
String url = "jdbc dbc:databse1";
Connection connection =
DriverManager.getConnection(url, "matt", "matt");
Statement st=connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String updateString = "UPDATE COFFEES " +
"SET SALES = 9999 " +
"WHERE COF_NAME LIKE 'Colombian'";
st.executeUpdate(updateString);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}

}
}
Tom Barns
Ranch Hand

Joined: Oct 27, 2000
Posts: 138
Hi Again
i tried to use insert and when i use two insert as
st.executeUpdate("INSERT INTO COFFEES " +
"VALUES ('DLP', 49, 8.99, 0, 0)");
st.executeUpdate("INSERT INTO COFFEES " +
"VALUES ('ABC', 49, 8.99, 0, 0)");
it works and insert just the first one after the second run for my code.it seems to me there is a command commit takes place after the second run for two consecutif insert call.
any help with this one?
thanks for your time.
hemanth kumar
Ranch Hand

Joined: Mar 15, 2001
Posts: 55
One suggestion
Paste the code in accsss and see whether the record is getting updated
regds
Hemanth
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: my java application doesn't update the table?