I have this sql script that I would like to execute in a java program. It takes on 3 parameters: dropper_id, to_char(begin_dt), to_char(end_dt). How would I do this?
The program is held on a Unix server.
The sql script is also located on the Unix server.
I think i would most likely want to execute the program by command line, but how do I execute it with parameters?
Process p = Runtime.getRuntime().exec ("psql sql_script.sql");
You can do that using JDBC and PreparedStatement, see these tutorials:
http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html
http://www.commandprompt.com/ppbook/x20921
Here are some examples:
http://www.exampledepot.com/egs/java.sql/InsertPs.html
http://www.roseindia.net/jdbc/jdbc-mysql/TwicePreparedStatement.shtml
http://www.roseindia.net/jdbc/prepared-statement-example.shtml
http://www.heimetli.ch/jdbc/JDBCPreparedStatement.html
If both your Java application and the SQL are located on the same server, then you just need to load it and execute as given above. See examples here:
http://www.coderanch.com/t/306966/JDBC/java/Execute-sql-file-java
How to execute .sql script file using JDBC
Running a .sql script using MySQL with JDBC
If it's just running the SQL (i.e. the whole purpose of your application is to run that SQL and do nothing else), you may want to look at the alternative solutions such as using Ant for the task, see this:
http://ant.apache.org/manual/Tasks/sql.html
or using whatever is your database command line utility, see some examples here:
How to execute sql-script file in java?
This very much depends on your circumstances, so you would have to see what is important in your case to decide which way to go.
In general, the correct way to execute database commands in a Java application is to use a Java-based database connection using JDBC or an even higher-level ORM such as Hibernate. These are preferred for a number of reasons, most importantly because they can report and deal with error paths far more easily than an external command.
However, if you have a reason not to use JDBC, you can use ProcessBuilder as described in this question How To Execute Native Commands In Java?
Related
It seems that there are different command to run Cypher query on Java:
executionEngine.execute("cyper command"), session.run("cyper command"), statement.executeQuery("cypher command") (So far I could find nothing else)
(The first command comes from: https://www.tutorialspoint.com/neo4j/neo4j_cypher_api_example.htm;
the last two commands come from: https://neo4j.com/developer/java/#_the_example_project)
I thought that session.run("cyper command") should be used when there is no result to be return such as when a csv files is imported. However, this proves me wrong: How to load CSV file with cypher in java?
So when should I use which? Or can they be run interchangeably?
It depends on how you use Neo4j.
If you use it embedded, then you have access to the core API such as ExecutionEngine. This provides more information on how to use Neo4j in embedded mode.
If you run a Neo4j Server, then you have many options, one of which is to use the Bolt driver (session.run etc.). See the Drivers section for how to use this.
Then, there are frameworks such as Neo4j OGM and SDN providing convenient ways to access Neo4j. See https://neo4j.com/developer/java/
Sometimes I need to debug production issues at my job. We have a replicated database so that we can see production data, but we don't have the ability to exec procedures or functions. Sometimes the things I need to debug involve a long procedure calling other procedures and functions. Normally I have to open up the first proc, look at what other procs it calls, and eventually put everything into one big script so that I can determine exactly where the production failure happened. I'm looking for a way to programmatically put together that composite SQL.
So I've figured out how to fetch SQL from our SVN using SVNKit in Java. I can parse it to remove the create procedure syntax and everything. The last big obstacle is that sometimes functions are called in-line and those functions may be composed of many lines, loops, or other not-one-in-line-statement friendly SQL.
Is there any way to do this? There is no way we'll receive exec permissions on this server.
My definition of portable is:
When I export my JAVA program to a jar file, others(who use my jar) don't need to install anything except JRE; that is, a portable JAVA SQL package is a jar file and users can use it like a normal package and don't need to install anything else.
Is SQLite for JAVA "portable"? If not, any SQL package is "portable"?
Or there is no such SQL package and users must install some SQL environment?
===========
I think I need a jar file(just a jar package!don't need anything installment!) that accepts SQL statements like creating tables.
My java program is so small so I don't want to install a big big big SQL environment like MySQL.
Just need some basic functions of SQL, like creating tables and querying.
There are some embeded database that can be implemented in the memory.The popular there are :
HSQL,H2 and Derby. (all are similar with mysql)
You can write the "create table.." to build the schema in a sql file, and the app load it and create the database. Of course, you also have to add the sql driver in advance.
But after the app finishes, the data cannot be maintained.
I have collected a source code of a java project from internet where there is a file with .sql extension. I am new to java and don,t know how to work with and get access to .sql file. How can I import the file in Netbeans and how to get access to that database. For your kind information, The project is on inventory management system and I have to get access to that database with a username and password contained inside it. What will I do now? Do I have to install any additional software or something else?
Please give a step by step procedure to do that. Thanks in advance
Doing some simple google searches I found this:
Running a .sql script using MySQL with JDBC
You could do it the way the OP did it, however its not as efficient as the answer. If you close the connection on how the OP did it in that post, and instead of writing the SQL, you can use a filereader to read from the SQL file and then use that data in the execute statement.
The only problem with his method though is that you need to install a JDBC driver.
install a kind of database(such as MySql)
(optional) install a gui tool for your database(such as navicat)
use the .sql files to create tables
install a JDBC driver, which establishes a bridge between your Java code and database
then you can access the database by using JDBC api
I have a java application(runnable jar) and VB scripts which I'm using to telnet to a remote machine and executing some cmds. So, I first execute the vbs files and then run my jar(in all everything is working fine).
But, now I want to integrate scripts and my java jar such that, running the jar should first trigger the script followed by Java related task.
Few thing which I've come across are -
I cannot trigger Vbs from Java(javax.script - correct me if I'm wrong). So, possible options to rewite the script in are -
Javascript(have no idea what my Javascript file would have so that after reading it inside java class I can write it to the Socket output stream.)
PHP(I tried this using Java bridge but it gives some error saying cgi needs to installed. And, I believe it also requires PHP to be installed on the host machine before executing my jar. So I'm not going any futher with this approach.)
Long story short, I don't want to create any dependencies - I am looking for something like where I can package any external lib with my jar(if required) and use it to execute my scripts.
You can execute the VB-Script in an external command. There are a lot of resources on the internet that explain how to do that - for instance this link also explains how to start a VB-Script from within java. However I do not know if you need the output from the script within the Java. If so you'll have to listen to the outputstream of the created process. You should find an example for that as well with that link (using the processbuilder)
If you have the script packaged within your jar, I fear you'll have to unpack it to a temporary folder and execute it there.
The closest I have seen about VB script as a JVM language is in answer here.
Visual Basic or VBScript as Java Scripting Engine
Have you seen this wikipedia entry about JVM languages?
http://en.wikipedia.org/wiki/List_of_JVM_languages
Also, have you considered using Ant and using it programmatically from java?
Another option is to use groovy/Ant from Java.