• 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

my java application doesn't update the table?

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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";
}
}
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Tom Barns
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One suggestion
Paste the code in accsss and see whether the record is getting updated
regds
Hemanth
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic