12/06/05
In my previous note about QuantumDB, I explained how to use an SQL database within Eclipse. But now, the question is how to automate the run of a SQL script using Ant...
Note: to illustrate this, we will use a MySQL datatbase but it's the same idea with another database.
This is the process to run SQL commands using Ant:
- If not yet done, please download MySQL Connector/J 3.1, the JDBC connector for MySQL database. Uncompress the zip under c:\Program files\mysql-connectorj.
- Create an SQL file called instructions.sql for example and put the SQL commands to peform in this file.
Open a build.xml file and the write the following instructions:
<project name="myproject" default="db.build"> <target name="db.build"> <sql driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8" userid="username" password="password" > <classpath> <pathelement path="c:/program files/mysql-connectorj/mysql-connector-java-3.1.12-bin.jar"/> </classpath> <transaction src="${sql.path}/db_structure.sql"/> </sql> </target> </project>Under Eclipse, right click on the build.xml and choose Run as | Ant build. The commands are played against the database. You should read something like this in the console:
db.build: [sql] Executing file: C:\Mes documents\eclipse\myproject\sql\instructions.sql [sql] 9 of 9 SQL statements executed successfully
Note 1: ?useUnicode=true&characterEncoding=UTF-8 is only to add in the url element if your database is using the UTF-8 charaset encoding.
Note 2: there is perhaps a pretty solution to avoid the password to be in clear text in the build file, but I don't know how to do that. If someone has got an idea...
Comments:
[Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Line 1: Incorrect syntax near 'go'.
the syntax is"
"
and just make sure to set the properties before that statement. One way to do this is by having ant include a properties file (presumably that ISN'T checked into your source control system) like this:
# Lines beginning with # are comments
some.username=yourusernamehere
some.password=yourpasswordhere
If this file was called "sqllogin.properties", you include it like this:
This way, the ant build file gets checked into your source control system, but the file with username/password doesn't.
Finally, if it makes you uncomfortable to store your password in a .properties file on your machine, you always have the option of using ant's "input" task to set a property by prompting the user.
(1) [sql driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8"
userid="${some.username}" password="${some.password}" ]
(2) {property file="sqllogin.properties"]
mk
There's a new Eclipse Magazine (www.eclipsemag.net) out there. The inaugural issue of the magazine is available for FREE. You can download it from here. Once you register, you also have FREE access to the magazine for the months of September, October, and November 2006.
If you are seeking for a writer for your magazine, you can contact me.
FYI, I will speak at Java User Group conference (Thursday September 28th, 2006 in Strasbourg) about Eclipse RCP.
Serge Baccou
The page you refered too in the post above isn't available to everyone. Is it normal ?
Furthermore, are the slides downloadable somewhere ?
Thanks in advance
Joseph
I do not understand what page you are talking about. Can you be more precise?
About the slides, are you talking about the Java User Group conference held in Strabourg?
http://www.baccoubonneville.com/index.php?option=com_content&task=view&id=48&Itemid=35
When trying to open it I get :
"You are not authorized to view this resource.
You need to login."
Regarding the slides, I'm indeed refering the JUG conference held in Strasbourg.
Best regards,
Joseph
le formulaire de prise de contact là :
http://www.baccoubonneville.com/index.php?option=com_contact&task=view&contact_id=1&Itemid=44
me renvoit (avec Firefox) à la page d'erreur ayant l'url http://www.baccoubonneville.com/index.php?option=com_contact&Itemid=44 et avec comme texte :
Precondition Failed
The precondition on the request for the URL /index.php evaluated to false.
C'est dommage !
The slides are now on our web site http://www.baccoubonneville.com.
For the problem you have about the contact form, I will investigate what is going wrong with this.
Serge
Ex ;
<sql driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/database?useUnicode=true&characterEncoding=UTF-8"
userid="username" password="password" delimiter='GO' >
.....
Leave a comment:
Pingbacks:
No Pingbacks for this post yet...