I am trying to automatically create an .accdb database but I get a compile error using the create() function:
I use the code from this SO answer:
Create an Access database file (.mdb or .accdb) using Java
public class JackcessLibrary {
private static Database createDatabase(String databaseName) throws IOException {
return Database.create(new File(databaseName));
}
private static TableBuilder createTable(String tableName) {
return new TableBuilder(tableName);
}
public static void addColumn(Database database, TableBuilder tableName, String columnName, Types sqlType) throws SQLException, IOException {
tableName.addColumn(new ColumnBuilder(columnName).setSQLType(Types.INTEGER).toColumn()).toTable(database);
}
public static void startDatabaseProcess() throws IOException, SQLException {
String databaseName = "C:/Users/abdulwhab/Desktop/database/db.accdb"; // Creating an MS Access database
Database database = createDatabase(databaseName);
String tableName = "Employee"; // Creating table
Table table = createTable(tableName)
.addColumn(new ColumnBuilder("Emp_Id").setSQLType(Types.INTEGER).toColumn())
.addColumn(new ColumnBuilder("Emp_Name").setSQLType(Types.VARCHAR).toColumn())
.addColumn(new ColumnBuilder("Emp_Employer").setSQLType(Types.VARCHAR).toColumn())
.toTable(database);
table.addRow(122875, "Sarath Kumar Sivan","Infosys Limited.");//Inserting values into the table
}
public static void main(String[] args) throws IOException, SQLException {
JackcessLibrary.startDatabaseProcess();
}
}
You use the brand new version 2.1.3 of Jackcess, wheras the five-year old answer you linked uses 1.2.6. The API of Jackcess underwent several changes when introducing version 2.
In the version you use a database is created by using a builder:
DatabaseBuilder.create(FileFormat, File)
For more on how to use the Jackcess API, see http://jackcess.sourceforge.net/cookbook.html.
for others clarity and easiness in future just do following changes in the Database creating function
File file = new File("C:/Users/abdulwhab/Desktop/database/test.accdb");
Database db = new DatabaseBuilder(file).setFileFormat(Database.FileFormat.V2000).create();
return db;
Note: i didn't chnage the jackess jar file version to previous one.
No chnages were made except the code :)
Related
I did a Java OCR project with Tesseract in the Mirth.When I run the jar file from the Mirth,I get this error.When I search it,I found that there is a init() method and also it is a protected void in Tesseract.java.I think that maybe it is the reason for that error.
What should I do?Thank you so much for your helps.
package Tess4jTest;
import java.io.File;
import java.io.IOException;
import net.sourceforge.tess4j.*;
public class TestTess {
public static String Tc;
public static String phone;
public static String date;
public static void main(String[] args) {
//System.out.println(returnText("C:\\Users\\Nevzat\\Desktop\\deneme.pdf"));
}
public static String returnText(String fileName){
File imageFile = new File(fileName);
if(imageFile.exists()){
Tesseract instance = new Tesseract();
instance.setDatapath("C:\\imageRAD\\Onam\\tessdata");
String result = null;
try {
result = instance.doOCR(imageFile);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}
if(result!=null){
int i=result.indexOf("Numarasn: ");
int j=result.indexOf("Tel No:");
int k=result.indexOf("Bilgllendirme Tarihl:");
Tc = result.substring(i+10, i+21);
phone = result.substring(j+8,j+23);
date = result.substring(k+22,k+32);
//System.out.println(result);
}else{
return "Null Error!";
}
}else{
return "Does not found a file!";
}
return Tc+","+phone+","+date;
}
public static String returnTC() throws IOException{
return Tc;
}
public static String returnPhone() throws IOException{
return phone;
}
public static String returnDate() throws IOException{
return date;
}
}
The error you got occurs when you try to create an object with a private constructor. (<init>() is the name of a constructor with no parameters)
Looking at the tess4j source I found a method with the following documentation:
#deprecated As of Release 2.0, use default constructor instead.
Looking at the source before 2.0 reveals the default constructor was private.
This means your problem is most likely that you are compiling against a version newer than 2.0, but your environment is running one older than 2.0.
Either update your environment or downgrade the library you build against to fix it.
I solved the error and have finished the project.I mention step by step
1.You have to use right jar files for tess4j.
2.Add java project all of in the tess4j-3.2.1.zip except tess4j-3.2.1.jar via Build Path.
3.Add tess4j-1.5.jar from this
4.Add tessdata folder,ghost4j-0.5.1.jar,jna-4.1.jar,tess4j.jar and jar file of your java project.
We are using flywayDB for some years to manage a quite large database application running on oracle 12c. This works fine and very reliable.
But recently we are running into performance problems during database migration. The number of database skripts we manage in our versions-table has risen beyond 10,000. The time flyway takes to migrate a single script has increased from some originally milliseconds to currently about a second. It seems that flyway per single migration step selects the full content of the versions table to calculate at client side its installation rank. This doesnt scale well.
Is there any possiblity to speed up flyway, may be by caching the content of the versions table?
We are using the Java-API of flyway version 3.2.1.
For completeness I have written a testcase to demonstrate this behaviour.
#RunWith(Parameterized.class)
public class PerformanceTestcase {
private static Logger LOG = Logger.getLogger( PerformanceTestcase.class.getName() );
#Parameter
public int noOfScripts;
#Before
public void generateLotsOfInstallerSkripts() throws IOException {
LOG.log(Level.INFO, "generating {0} skripts", noOfScripts);
Path baseVersion = getBaseVersionPath();
generateSkripts( noOfScripts, baseVersion, BASE_SKRIPT_NAME );
}
#Test
public void testPerformance() throws IOException, SQLException {
// this one does not scale well with increasing noOfScripts
migrate();
}
private static final String SCHEMA_TABLE_NAME = "test_versions";
private static final String SKRIPT_NAME_FORMAT = "%s.%05d__test.sql";
private static final String SKRIPT_CONTENT = "select %05d from dual;";
private static final String FILESYSTEM = "filesystem:";
private static final String BASE_SKRIPT_NAME = "V00.00.00";
private static final String BASE_DIR = "/tmp/performanceTest";
private void migrate() throws SQLException {
Flyway flyway = new Flyway();
flyway.setDataSource( getDataSource() );
flyway.setLocations( FILESYSTEM + BASE_DIR );
flyway.setTable( SCHEMA_TABLE_NAME );
flyway.setBaselineVersionAsString(BASE_SKRIPT_NAME.substring(1) );
flyway.setBaselineOnMigrate(true);
flyway.setValidateOnMigrate(false);
flyway.migrate();
}
#Parameters(name="noOfScripts={0}")
public static Iterable<? extends Object> data() {
List<Integer> retval = new LinkedList<Integer>();
for ( int i=0; i<16000; i+=1000 ) {
if ( i>0 ) retval.add( Integer.valueOf(i) );
retval.add( Integer.valueOf(i+100) );
}
return retval;
}
private Path getBaseDirPath() throws IOException {
Path base = Paths.get(BASE_DIR);
if ( !Files.exists(base) ) {
Files.createDirectory(base);
}
return base;
}
private Path getBaseVersionPath() throws IOException {
Path base = getBaseDirPath();
Path baseVersion = base.resolve(BASE_SKRIPT_NAME);
if ( !Files.exists(baseVersion) ) {
Files.createDirectories(baseVersion);
}
return baseVersion;
}
private void generateSkripts( int numberOfSkripts, Path baseDir, String baseName ) throws IOException {
for (int i = 0; i < numberOfSkripts; i++) {
Path file = baseDir.resolve( String.format(SKRIPT_NAME_FORMAT, baseName, i) );
Files.write( file
, Arrays.asList( new String[] { String.format( SKRIPT_CONTENT, i ) } )
, StandardOpenOption.CREATE
, StandardOpenOption.TRUNCATE_EXISTING
);
}
}
private DataSource getDataSource() throws SQLException {
OracleDataSource ds = new OracleDataSource();
ds.setURL(CONNECTION_URL);
return ds;
}
}
Update
I just run the testcase with the current version 4.0.3 of flywayDB. It ran about half the time compared to 3.2.1, but the scaling problem still persists. Flyway is selecting the complete versions table per single migration step, which significantly slows down a migration when the version table is quite populated.
Update again
I've looked into the source-code of flywayDB version 4.0.3: In org.flywaydb.core.internal.command.DbMigrate#migrate the MigrationInfoServiceImpl is created and refreshed. This selects the full schema_versions-Table. But after that step only a single migration script is executed. I would expect migrating all pending scripts instead.
I've opened an issue on this one at github.
The issue is fixed. Today I tested flyway version 4.1.2 and the performance problem is gone. Thanks for the great work at boxfuse!
I'm trying to get some text from a website and set it as a String in Java.
I have little to no experience with web connections in Java and would appreciate some help.
Here's what I've got so far:
static String wgetURL = "http://www.realmofthemadgod.com/version.txt";
static Document Version;
static String displayLink = "http://www.realmofthemadgod.com/AGCLoader" + Version + ".swf";
public static void main(String[] args) throws IOException{
Version = Jsoup.connect(wgetURL).get();
System.out.println(Version);
JOptionPane.showMessageDialog(null, Version, "RotMG SWF Finder", JOptionPane.DEFAULT_OPTION);
}
I'm trying to use Jsoup but I keep getting startup errors (it has issues when starting up).
Your problem is not Jsoup related.
You are trying to create a String with Version while Version is not defined.
Change your code to:
public static void main(String[] args) throws IOException{
String url = "http://www.realmofthemadgod.com/version.txt"
Document doc = Jsoup.connect(url).get();
System.out.println(doc);
// query doc using jsoup ...
}
I am trying to retrieve data installed on the database server JAVA DB located in jdk1.7.0_06. I am able to make connection to the server and create the database. But i am getting following error for the compilation and running:
No suitable driver found for jdbc:derby:AddressBook
Please can you help me! Thank you
I stated, "I wonder if you need to set the derby.system.home property as the Java DB tutorials suggest. Have you tried this? Something like, System.setProperty("derby.system.home", DERBY_HOME_PATH); where the second parameter is the path to your database's home directory."
And you replied:
#HovercraftFullOfEels, i think i didn't but i am sure that i have set some envariable variables via command line.
#Dorji: that doesn't set the System properties in your JVM though. I still think that you need to set this property before using your database. For example,
public class Test {
public static final String DERBY_HOME = "derby.system.home";
// ***** the two Strings below will be different for you *****
public static final String DERBY_HOME_PATH = "D:/DerbyDB";
private static final String DB_NAME = "sample";
public static void main(String[] args) {
System.setProperty(DERBY_HOME, DERBY_HOME_PATH);
Connection conn = null;
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
conn = DriverManager.getConnection("jdbc:derby:" + DB_NAME);
} catch (InstantiationException | IllegalAccessException
| ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
if (conn == null) {
System.exit(-1);
}
}
// .... etc...
My derby.system.home directory is D/:DerbyDB, and my database resides in the D/:DerbyDB/sample directory:
This of course will be different for you.
I am using Berkely DB and I have an error which says that mutations are missing. What does this mean?
Exception: com.sleepycat.persist.evolve.IncompatibleClassException: Mutation is missing to evolve class: TopMoveDAO.TopMoveClass version: 0 Error: java.lang.ClassNotFoundException: TopMoveDAO.TopMoveClasscom.sleepycat.persist.evolve.IncompatibleClassException: Mutation is missing to evolve class: TopMoveDAO.TopMoveClass version: 0 Error: java.lang.ClassNotFoundException: TopMoveDAO.TopMoveClass
at com.sleepycat.persist.impl.PersistCatalog.(PersistCatalog.java:365)
at com.sleepycat.persist.impl.Store.(Store.java:180)
at com.sleepycat.persist.EntityStore.(EntityStore.java:165)
at TopMoveDAO.TopMovePut.setup(TopMovePut.java:40)
at TopMoveDAO.TopMovePut.run(TopMovePut.java:59)
at TopMoveDAO.TopMovePut.main(TopMovePut.java:84)
package TopMoveDAO;
import java.io.File;
import java.util.Timer;
import java.util.TimerTask;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.StoreConfig;
public class TopMovePut {
//private static File envHome = new File("C:/dev/je-3.3.75/");
private static File envHome = new File("C:/dev/db/berkeley");
private Environment envmnt;
private EntityStore store;
private TopMoveDA sda;
//Next we create a method that simply opens our database environment and entity store for us.
// The setup() method opens the environment and store
// for us.
public void setup()
throws DatabaseException {
EnvironmentConfig envConfig = new EnvironmentConfig();
StoreConfig storeConfig = new StoreConfig();
envConfig.setAllowCreate(true);
storeConfig.setAllowCreate(true);
// Open the environment and entity store
envmnt = new Environment(envHome, envConfig);
store = new EntityStore(envmnt, "EntityStore", storeConfig);
}
//We also need a method to close our environment and store.
// Close our environment and store.
public void shutdown()
throws DatabaseException {
store.close();
envmnt.close();
}
//Populate the entity store
private void run()
throws DatabaseException {
setup();
// Open the data accessor. This is used to store
// persistent objects.
sda = new TopMoveDA(store);
// Instantiate and store some entity classes
PriceElement pe1 = new PriceElement();
pe1.setSecCode("UNO");
pe1.setLastPrice(1);
sda.pIdx.put(pe1);
shutdown();
}
//main
public static void main(String args[]) {
//SimpleStorePut ssp = new SimpleStorePut();
TopMovePut tmp = new TopMovePut();
try {
//ssp.run();
tmp.run();
} catch (DatabaseException dbe) {
System.err.println("TopMovePut: " + dbe.toString());
dbe.printStackTrace();
} catch (Exception e) {
System.out.println("Exception: " + e.toString());
e.printStackTrace();
}
System.out.println("All done - TopMovePut.");
}
}
You have to write a mutation to evolve your database. Deleting the database will not solve the problem, only circumvent it ( which is fine if you have not yet deployed to production, but if you do not want to lose your existing data then write a mutation.)
Some changes to your persistent entities are handled automatically by Berkley db, such as adding a field. Ones that involve deleting data or renaming fields generally require you to write an explicit mutation. When you start using mutations you will also have to annotate your entities with version numbers which the mutations will refer to - even if the mutation is handled automatically you will have to increment the version number. When you make major structural changes such as using a different primary key, you will have to do an entire store conversion.
Take care when evolving a database in a replicated environment. I would strongly suggest reading the following:
Package com.sleepycat.persist.evolve (Oracle - Berkeley DB Java Edition API)
You have to delete your existing database each time.