简体   繁体   中英

import .sql file in postgres using \i command from java

I am trying to import database from .sql file in postgres using "\\i E:/dump.sql" command , its working fine from postgres command prompt but when i try the same from java it raise an error at "\\" , my code is

connection = DriverManager.getConnection( "jdbc:postgresql://localhost:5432/database","postgres", "passwd");
PreparedStatement ps3 = connection.prepareStatement("\\i E:/dump.sql");
boolean res3= ps3.execute();
System.out.println("imported succesfully .."+res3);

With the JDBC driver/interface you can only talk SQL, what you're trying is to issue PostgreSQL commandline tool ( psql ) specific commands. That won't work.

If you insist on doing this, you could use the Runtime.getRuntime().exec(...) approach, something like

Runtime.getRuntime().exec( "psql -f dump.sql" );

Cheers,

Long story short - you can't. \\i is not PostgreSQL command (as in: PostgreSQL database engine). It's command of psql - which is command line tool for interacting with database.

If you're connecting to database via JDBC you're not using psql, so you can't use its commands ( \\i , \\o and alike).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM