package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DriverExample {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("org.firebirdsql.jdbc.FBDriver");
Connection bd = DriverManager.getConnection("jdbc:firebirdsql:embedded:database.fdb");
Statement st = bd.createStatement();
st.execute("create table if not exists 'TABLE1' ('name1' int, 'name2' text, 'name3' text);");
st.execute("insert into 'TABLE1' ('name1', 'name2', 'name3') values (1, 'name1', 'name2'); ");
st.execute("insert into 'TABLE1' ('name1', 'name2', 'name3') values (2, 'name3', 'name4'); ");
st.execute("insert into 'TABLE1' ('name1', 'name2', 'name3') values (3, 'name5', 'name6');");
ResultSet rs = st.executeQuery("select * from TABLE1");
while (rs.next()) {
System.out.print(rs.getString(1) + " ");
System.out.print(rs.getString(2) + " ");
System.out.println(rs.getString(3));
}
}
}
I receive the following error:
Exception in thread "main" org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544344. I/O error for file CreateFile (open) ""
Error while trying to open file
null
Reason: I/O error for file CreateFile (open) ""
Error while trying to open file
null
at org.firebirdsql.jdbc.FBDataSource.getConnection(FBDataSource.java:122)
at org.firebirdsql.jdbc.FBDriver.connect(FBDriver.java:131)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:243)
at util.DriverExample.main(DriverExample.java:26)
at org.firebirdsql.gds.GDSException: I/O error for file CreateFile (open) ""
Error while trying to open file
null
at org.firebirdsql.gds.impl.jni.JniGDSImpl.native_isc_attach_database(Native Method)
at org.firebirdsql.gds.impl.jni.BaseGDSImpl.iscAttachDatabase(BaseGDSImpl.java:135)
at org.firebirdsql.jca.FBManagedConnection.<init>(FBManagedConnection.java:86)
at org.firebirdsql.jca.FBManagedConnectionFactory.createManagedConnection(FBManagedConnectionFactory.java:477)
at org.firebirdsql.jca.FBStandAloneConnectionManager.allocateConnection(FBStandAloneConnectionManager.java:69)
at org.firebirdsql.jdbc.FBDataSource.getConnection(FBDataSource.java:119)
at org.firebirdsql.jdbc.FBDriver.connect(FBDriver.java:131)
at java.sql.DriverManager.getConnection(DriverManager.java:579)
at java.sql.DriverManager.getConnection(DriverManager.java:243)
at util.DriverExample.main(DriverExample.java:26)
Java Result: 1
I can't find any tutorials on Firebird embedded. Should I set environment variables for embedded?
The error message indicates that the file does not exist. The fact that it shows 'null' instead of the actual filename might be a mismatch between embedded version and Jaybird version.
To create a database you need to use the following code (and handle the exceptions it throws in a correct manner):
FBManager manager = new FBManager(GDSType.getType("EMBEDDED"));
manager.start();
manager.createDatabase("database.fdb", "sysdba", "");
manager.stop();
Be aware that the DDL you are using to create the table is not valid Firebird SQL. You will need to use RECREATE TABLE (which always drops and creates the table) or plain CREATE TABLE and handle (ignore) the error code that indicates the table already existed (isc_no_meta_update
or 335544351
). Also, Firebird does not have a type called text
.
Since Jaybird 6, you can also create a database by specifying the connection property createDatabaseIfNotExist
with value true
in the JDBC URL or on the data source. See also Create database if it does not exist in the Jaybird JDBC Driver Java Programmer’s Manual.
Full disclosure: I am one of the developers of Jaybird (the Firebird JDBC driver).