3

I am trying to connect my Postgres database in MATLAB and it is throwing me an error stating

"'JDBC Driver Error: org.postgresql.Driver. Driver Not Found/Loaded.'"

Here's my connection method that i have used.

conn = database('postgres','username','password','org.postgresql.Driver', 'jdbc:postgresql://localhost:5432/postgres=postgres');

after that it throws me the error.

I have looked at forums and they told me to add the postgres jar files to the MATLAB directory toolkit textfile, such as below.

C:\Program Files\PostgreSQL\pgJDBC\postgresql-8.4.702.jdbc3.jar 
C:\Program Files\PostgreSQL\pgJDBC\postgresql-8.4.702.jdbc4.jar 

I don't know where else I am going wrong. Please advise.

Thank you.

1 Answer 1

2

You should add jar file with JDBC driver to your dynamic java class path before connecting to database. I believe you can add only one file depending on your requirements. Check the versions difference here.

To avoid warning if a jar file already in the path add some check:

%# add class path (if not in the class path)
p = 'C:\Program Files\PostgreSQL\pgJDBC\postgresql-8.4.702.jdbc3.jar';
if ~ismember(p,javaclasspath)
    javaaddpath(p)
end
Sign up to request clarification or add additional context in comments.

7 Comments

It works. But somehow i can't seem to query an information from the database into MATLAB. Here's the code that i am trying to use : e = exec(conn,'SELECT *FROM cath_2'); e = fetch(e);. This is the error, ??? Undefined function or method 'fetch' for input arguments of type 'struct'.
How about if you use another variable to return fetch result? rs = fetch(e); I usually just do rs = fetch(exec(conn, qry));. If it does not work try to use another JDBC driver.
exec actually returns a cursor object, not a structure. Are you using the Database Toolbox?
My apologies, i wrote the url connection wrong. It's fixed now. I got another problem if you dont mind helping me. I am trying to query a table into MATLAB such as this, rs = fetch(exec(conn, 'SELECT * FROM cath_2'));, and it somehow doesnt recognise the table in the database eventhough it does exists. It states 'Invalid Cursor: ERROR: relation "cath_2" does not exist.
Try query 'show tables'. It should return list of tables in your database. Maybe you connected to a wrong database?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.