Results 1 to 3 of 3

Thread: SQL error

  1. #1
    dewitrydan is offline Member
    Join Date
    Aug 2010
    Posts
    67
    Rep Power
    0

    Default SQL error

    Hey there
    I have got this code that: Stores Yahoo stock quotes for google in a Java DB database.
    But I get this error:
    Java Code:
    SQL Error: Syntax error: Encountered "open" at line 1, column 54.
    Can you help me please?
    Here is the code:
    Java Code:
    import java.io.*;
    import java.net.*;
    import java.sql.*;
    import java.util.*;
    
    public class QuoteSaver {
    	private String ticker;
    
    	public QuoteSaver(String inTicker) {
    		ticker = inTicker;
    	}
    
    	private String retrieveQuote() {
    		StringBuffer buf = new StringBuffer();
    		try {
    			URL page = new URL("http://quote.yahoo.com/d/quotes.csv?s="
    					+ ticker + "&f=sl1d1t1c1ohgv&e=.csv");
    			String line;
    			URLConnection conn = page.openConnection();
    			conn.connect();
    			InputStreamReader in = new InputStreamReader(conn.getInputStream());
    			BufferedReader data = new BufferedReader(in);
    			while ((line = data.readLine()) != null) {
    				buf.append(line + "\n");
    			}
    		} catch (MalformedURLException mue) {
    			System.out.println("Bad URL: " + mue.getMessage());
    		} catch (IOException ioe) {
    			System.out.println("IO Error:" + ioe.getMessage());
    		}
    		return buf.toString();
    	}
    
    	private void storeQuote(String data) {
    		StringTokenizer tokens = new StringTokenizer(data, ",");
    		String[] fields = new String[9];
    		for (int i = 0; i < fields.length; i++) {
    			fields[i] = stripQuotes(tokens.nextToken());
    		}
    		String datasource = "jdbc:derby:quotedata;create=true";
    		try {
    			Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
    			Connection conn = DriverManager.getConnection(datasource);
    			PreparedStatement prep2 = conn.prepareStatement("INSERT INTO "
    					+ "Stocks(ticker, price, quoteDate, change, open, "
    					+ "high, low, volume) " + "VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
    			prep2.setString(1, fields[0]);
    			prep2.setString(2, fields[1]);
    			prep2.setString(3, fields[2]);
    			prep2.setString(4, fields[4]);
    			prep2.setString(5, fields[5]);
    			prep2.setString(6, fields[6]);
    			prep2.setString(7, fields[7]);
    			prep2.setString(8, fields[8]);
    			prep2.executeUpdate();
    			conn.close();
    		} catch (SQLException sqe) {
    			System.out.println("SQL Error: " + sqe.getMessage());
    		} catch (ClassNotFoundException cnfe) {
    			System.out.println(cnfe.getMessage());
    		}
    	}
    
    	private String stripQuotes(String input) {
    		StringBuffer output = new StringBuffer();
    		for (int i = 0; i < input.length(); i++) {
    			if (input.charAt(i) != '\"') {
    				output.append(input.charAt(i));
    			}
    		}
    		return output.toString();
    	}
    
    	public static void main(String[] arguments) {
    		
    		QuoteSaver qd = new QuoteSaver("SUN");
    		String data = qd.retrieveQuote();
    		qd.storeQuote(data);
    	}
    }

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    'open' is a reserved word in many SQL dialects; pick another name.

    kind regards,

    Jos

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I believe you could also reference it as `open` (which is what I do to avoid these potential issues).

Similar Threads

  1. java out of memory error-heap space error
    By elsanthosh in forum NetBeans
    Replies: 4
    Last Post: 06-15-2010, 09:31 AM
  2. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  3. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  4. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  5. Replies: 3
    Last Post: 01-23-2009, 08:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •