• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

I'm new to jdbc,please help me with this code!

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!Is it possible to send data into a database through command line arguments?
The following is my code which is giving me error.I am using MSAccess database.
Thanks.
import java.sql.*;
class MyArgs
{
public static void main(String arg[])
{
Connection con;
Statement st;
ResultSet rs;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc:apri");
st=con.createStatement();
if(arg.length>2)
System.out.println("Too many arguments");
else if(arg.length==2)
int i=st.executeUpdate("insert into kwality values(arg[0],arg[1])");
if(i!=-1)
{
System.out.println(i+" rows inserted!");
}
rs=st.executeQuery("select * from kwality");
while(rs.next())
{
String s=rs.getString("iname");
String x=rs.getString("i_id");
System.out.println(s+" "+x);
}
rs.close();
st.close();
con.close();
}
catch(Exception se)
{
System.out.println(se.getMessage());
}
}
}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't tested the rest of the code, but offhand it looks like the problem is this line:
<code><pre> int i=st.executeUpdate("insert into kwality values(arg[0],arg[1])");</pre></code>
The problem is that arg[0] and arg[1] would be understood by your Java program, but not by the database. By including them as part of the query string, you are telling Java to pass "arg[0],arg[1]" on to the db and try to make sense of it - which doesn't work. What you want is to pass the values of arg[0] and arg[1]:
<code><pre> int i = st.executeUpdate("insert into kwality values(\""
+ arg[0] + "\", \""
+ arg[1] + "\")");</pre></code>
Note the use of the escape sequence \" to make a " part of the string itself, rather than interpreting the " as the beginning or end of a string. So if arg contains values of "Frodo" and "Sam", this executes the following SQL statement:
<code><pre> insert into kwality values("Frodo", "Sam")</pre></code>
Assuming the table "kwality" has two columns which can take strings, then this should work. If not, you can read the error messages you get and find alternatives. You may also want to give the statement to the DB directly (outside Java) to see what it says. How to do this depends on the DB, but there should be some sort of interactive mode where you can enter queries and commands directly. That way you don't have to keep recompiling and running to see what happens, and often the error messages are clearer this way. (And you know that it's an issue between your query and the database, not a but elsewhere in your Java program.)
 
Aparanji Raju
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jim,I will try it and let u know again.
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic