PostgreSQL 8.3 + Eclipse

0

Witam
Chciałbym nauczyć się pracy z relacyjnymi bazami danych.

W Eclipse mam program testujący poprawne połączenie z baza :
Test.java

package Oppp;

import java.sql.*;
import java.io.*;
import java.util.*;

public class Test 
{
	public static void main(String args[])
	{
		try
		{
			runTest();
		}
		catch (SQLException ex)
		{
			while(ex != null)
			{
				ex.printStackTrace();
				ex = ex.getNextException();
			}
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
	}
	
	public static void runTest() throws SQLException, IOException
	{
		Connection conn = getConnection();
		try
		{
			Statement stat = conn.createStatement();
			
			stat.execute("CREATE TABLE Greetings (Message CHAR(20))");
			stat.execute("INSERT INTO Greetings VALUE ('HEllo, word!')");
			
			ResultSet result = stat.executeQuery("SELECT * FROM Greetings");
			result.next();
			System.out.println(result.getString(1));
			stat.execute("DROP TABLE Greetings");
		}
		finally
		{
			conn.close();
		}
	}
	
	public static Connection getConnection() throws SQLException, IOException
	{
		Properties props = new Properties();
		FileInputStream in = new FileInputStream("database.properties");
		props.load(in);
		in.close();
		
		String drives = props.getProperty("jdbc.driver");
		if ( drives != null)
			System.setProperty("jdbc.drivers",drives);
		
		String url = props.getProperty("jdbc.url");
		String username = props.getProperty("jdbc.username");
		String password = props.getProperty("jdbc.password");
		
		return DriverManager.getConnection(url,username,password);
	}
}

database.properties

jdbc.drivers=org.postgresql.JDBCDriver
jdbc.url=jdbc:postgresql:COREJAVA
jdbc.username=postgres
jdbc.password=sql8

Bazę mam w PostgreSQL 8.3
A w czasie kompilacji rgramu wyskakuje

org.postgresql.util.PSQLException: ERROR: syntax error at or near "VALUE"
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:1592)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1327)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:192)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:451)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:336)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:328)
at Oppp.Test.runTest(Test.java:37)
at Oppp.Test.main(Test.java:13)

już nie mam siły co z tym zrobić ??? proszę o pomoc

0

values

0

Witam Cię nav. To że jest cos z „values” to wim ale nie wiem o co chodzi i jak naprawić ;/ pomożesz ? będę bardzo wdzieczny.

0

Chodzi o to że źle napisałeś polecenie SQL, należało napisać values zamiast value.

stat.execute("INSERT INTO Greetings VALUE ('HEllo, word!')");

IMHO Powinieneś się tego sam domyślić na podstawie odpowiedzi.

0

Bardzo ale to bardzo dziękuje i za podpowiedz. Racja powinienem zrozumieć o co chodzi.

0

Mam takie pytanie gdzie sie ustawia to database properities, bo wlasnie zaczynam z java i bazami.
dzieki z góry.

0

database jest to plik tekstowy z rozszerzeniem properties, czyli database.properties

1 użytkowników online, w tym zalogowanych: 0, gości: 1