File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and the fly likes I'm new to jdbc,please help me with this code! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "I Watch "I New topic

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

Aparanji Raju
Ranch Hand

Joined: Apr 02, 2000
Posts: 65
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.
import java.sql.*;
class MyArgs
public static void main(String arg[])
Connection con;
Statement st;
ResultSet rs;
con=DriverManager.getConnection("jdbc dbc:apri");
System.out.println("Too many arguments");
else if(arg.length==2)
int i=st.executeUpdate("insert into kwality values(arg[0],arg[1])");
System.out.println(i+" rows inserted!");
rs=st.executeQuery("select * from kwality");
String s=rs.getString("iname");
String x=rs.getString("i_id");
System.out.println(s+" "+x);
catch(Exception se)
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
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.)

"I'm not back." - Bill Harding, Twister
Aparanji Raju
Ranch Hand

Joined: Apr 02, 2000
Posts: 65
Thanks Jim,I will try it and let u know again.
I agree. Here's the link:
subject: I'm new to jdbc,please help me with this code!