Home » Developer & Programmer » JDeveloper, Java & XML » JDBC and query as command-line argument
JDBC and query as command-line argument [message #243312] Wed, 06 June 2007 18:14 Go to next message
cb153
Messages: 7
Registered: May 2007
Junior Member
I need help with this java program that is supposed to "accept two command-line arguments. The first argument is a filename indicating the file that stores the username and password for the program to connect to the database. The second argument is a SQL query that is to be sent by the program to the database server to be processed. After the query is executed, the Java program displays the query result in a table on the screen."

The program is expected to be used with:

java P10 mypswdfile.txt “select * from room r, hotel h where r.hno=h.hno”

But when I try this I get the error "ORA-00936: missing expression".

What am I doing wrong?

 
import java.sql.*;
import java.util.*;
import java.io.*;
public class P10
{
public static void main (String [] args)
{
String file = args[0];

try
{

BufferedReader input = new BufferedReader(new FileReader(file)); 
 
String line = input.readLine(); 
 
StringTokenizer tk = new StringTokenizer(line); 
String username = tk.nextToken();
String password = tk.nextToken();  

String dburl = new String
("jdbc:oracle:thin:@reason.levels.unisa.edu.au:1000:UNISACIS");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con =
DriverManager.getConnection(dburl,username,password);
Statement stmt = con.createStatement();
ResultSet r = stmt.executeQuery(args[1]);
ResultSetMetaData meta = r.getMetaData();
int ncols = meta.getColumnCount();
for (int col=1; col<=ncols; col++)
System.out.print(meta.getColumnName(col) +" - ");
System.out.println("");

while (r.next())
{
for (int col=1; col<=ncols; col++)
System.out.print(r.getString(col) +" - ");
System.out.println("");
}
System.out.println("End SQL");
stmt.close();
}
// On IO exception, display system error message
catch (IOException e)
{
System.out.println(e.getMessage());
}
// On SQL exception, display SQL error message
catch (SQLException e)
{
System.out.println(e.getMessage());
}
// On ClassNotFound exception, display error message
catch (ClassNotFoundException e)
{
System.out.println(e.getMessage());
}
}
}

Re: JDBC and query as command-line argument [message #243351 is a reply to message #243312] Thu, 07 June 2007 01:04 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
D:\Work>java P10 cred.txt "select * from emp"
D:\Work>java P10 cred.txt "select * from emp e,dept d where d.deptno = e.deptno"
EMPNO - ENAME - JOB - MGR - HIREDATE - SAL - COMM - DEPTNO - DEPTNO - DNAME - LOC -
7369 - SMITH - CLERK - 7902 - 1980-12-17 00:00:00.0 - 800 - null - 20 - 20 - RESEARCH - DALLAS -
7499 - ALLEN - SALESMAN - 7698 - 1981-02-20 00:00:00.0 - 1600 - 300 - 30 - 30 -SALES - CHICAGO -
7521 - WARD - SALESMAN - 7698 - 1981-02-22 00:00:00.0 - 1250 - 500 - 30 - 30 - SALES - CHICAGO -
7566 - JONES - MANAGER - 7839 - 1981-04-02 00:00:00.0 - 2975 - null - 20 - 20 - RESEARCH - DALLAS -
7654 - MARTIN - SALESMAN - 7698 - 1981-09-28 00:00:00.0 - 1250 - 1400 - 30 - 30 - SALES - CHICAGO -
7698 - BLAKE - MANAGER - 7839 - 1981-05-01 00:00:00.0 - 2850 - null - 30 - 30 - SALES - CHICAGO -
7782 - CLARK - MANAGER - 7839 - 1981-06-09 00:00:00.0 - 2450 - null - 10 - 10 - ACCOUNTING - NEW YORK -
7788 - SCOTT - ANALYST - 7566 - 1982-12-09 00:00:00.0 - 3000 - null - 20 - 20 - RESEARCH - DALLAS -
7839 - KING - PRESIDENT - null - 1981-11-17 00:00:00.0 - 5000 - null - 10 - 10 - ACCOUNTING - NEW YORK -
7844 - TURNER - SALESMAN - 7698 - 1981-09-08 00:00:00.0 - 1500 - 0 - 30 - 30 - SALES - CHICAGO -
7876 - ADAMS - CLERK - 7788 - 1983-01-12 00:00:00.0 - 1100 - null - 20 - 20 - RESEARCH - DALLAS -
7900 - JAMES - CLERK - 7698 - 1981-12-03 00:00:00.0 - 950 - null - 30 - 30 - SALES - CHICAGO -
7902 - FORD - ANALYST - 7566 - 1981-12-03 00:00:00.0 - 3000 - null - 20 - 20 - RESEARCH - DALLAS -
7934 - MILLER - CLERK - 7782 - 1982-01-23 00:00:00.0 - 1300 - null - 10 - 10 - ACCOUNTING - NEW YORK -
End SQL


No problems here, so your code looks alright.
Maybe you could do a println of args[1], to make sure it comes through without a problem.
Re: JDBC and query as command-line argument [message #243443 is a reply to message #243351] Thu, 07 June 2007 05:44 Go to previous messageGo to next message
cb153
Messages: 7
Registered: May 2007
Junior Member
When I do a println of args[1], the only thing that comes through is "select" (without the quotes). But I don't know why.
Re: JDBC and query as command-line argument [message #243488 is a reply to message #243443] Thu, 07 June 2007 08:35 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
maybe a stupid question, but you didn't forget the double quotes around the sql (I know you indicated it SHOULD be used that way.)
Re: JDBC and query as command-line argument [message #243681 is a reply to message #243488] Fri, 08 June 2007 06:47 Go to previous message
cb153
Messages: 7
Registered: May 2007
Junior Member
No, I did put the double quotes. (Though I did have a try without them, just in case)
Previous Topic: Another question: how to free temporary lob when using store procedure
Next Topic: Help with XML output 10g- Closing issue
Goto Forum:
  


Current Time: Fri Apr 19 14:16:28 CDT 2024