Vaadin: How do I display data from a MySQL database? - java

I'm developing a Vaadin Flow (version 14.1) app and I have this issue, I don't get it to connect directly with MySQL database.
I have set up the JDBC connection with maven, I've also created a singleton class I call Datasource where I store my values and methods. However right now it only has one as I'm testing this, this is what I want to do:
Click on a button on the app and update a label
Here's the button click listener:
button.addClickListener(click -> {
label.setText(Datasource.getInstance().getUsername());
});
Here's the Datasource class method:
public String getUsername() {
String username = "QUERY-FAILED";
try {
start();
statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select * from names");
rs.next();
username = rs.getString(1);
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
return username;
}
But the label doesn't update, if I comment the try block it updates to QUERY-FAILED which is the string I put to test if it failed, but if it isn't commented the label just stays the same.
I also tried to add a main method to the Datasource class and run it as a Java application, and the method works fine, it does return a string with the username. So I'm guessing I'm stuck somewhere in between the connection with the vaadin app. Also, If I try to get the username String in my vaadin app when I'm starting the app (and not with a click listener) I got an long stack of errors with the Datasource indicating a nullpointerexception here:
statement = conn.createStatement();
Thanks in advance!

I cannot spot any problem with your code. But I can provide an entire working example app for you to compare
My example app goes along the lines laid out in your Question's code. A Vaadin Button performs a database query using a DataSource object from a table of user names. The value from the first row found is displayed in a Vaadin Label widget on the web page.
This app was built and run with Vaadin 14.1.5 using a "Plain Java Servlet" flavor of a starter project provided by the Vaadin.com site. Running on macOS Mojave with the bundled Jetty web container.
My only changes to their Maven POM file was to change to Java version 13, and to add a dependency for H2 Database Engine to make this a self-contained example using an in-memory database.
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
</dependency>
I used the hook for Vaadin starting, to establish my DataSource object and initialize the database. Following the manual, nested in the resources folder, I created folders META-INF > services. Per the Java SPI facility, there I created a file named com.vaadin.flow.server.VaadinServiceInitListener containing a single line to specify the name of my class that implements the interface named in the name of this file:
work.basil.example.ApplicationServiceInitListener
That is, my ApplicationServiceInitListener class implement the Vaadin interface VaadinServiceInitListener. My class will be automatically instantiated and its method invoked via that Java SPI facility when my Vaadin web app launches.
My ApplicationServiceInitListener class:
package work.basil.example;
import com.vaadin.flow.server.ServiceInitEvent;
import com.vaadin.flow.server.VaadinServiceInitListener;
import org.h2.jdbcx.JdbcDataSource;
public class ApplicationServiceInitListener implements VaadinServiceInitListener
{
#Override
public void serviceInit ( ServiceInitEvent serviceInitEvent )
{
System.out.println( "DEBUG Running `serviceInit` of " + this.getClass().getCanonicalName() );
// Database work.
prepareDataSource();
App.INSTANCE.provideDatabase().initializeDatabase();
}
private void prepareDataSource ( )
{
JdbcDataSource ds = new JdbcDataSource();
ds.setURL( "jdbc:h2:mem:demo;DB_CLOSE_DELAY=-1" );
ds.setUser( "scott" );
ds.setPassword( "tiger" );
App.INSTANCE.rememberDataSource( ds );
}
}
That class calls my App class which acts as a sort of service locator. Designed as a singleton via enum.
package work.basil.example;
import javax.sql.DataSource;
import java.util.Objects;
public enum App
{
INSTANCE;
// -------| DataSource |---------------------------------
private DataSource dataSource;
public DataSource provideDataSource ( )
{
return this.dataSource;
}
public void rememberDataSource ( DataSource dataSource )
{
this.dataSource = Objects.requireNonNull( dataSource );
}
// -------| Database |---------------------------------
private Database database;
public Database provideDatabase ( )
{
return new Database();
}
}
That class calls my Database class. In real work, Database would be an interface with various concrete implementations for testing versus deployment. I ignored that here for demonstration purposes.
package work.basil.example;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Database
{
public String getFirstUserName ( )
{
String userName = "QUERY-FAILED";
String newline = "\n";
StringBuilder sql = new StringBuilder();
sql.append( "SELECT name_ from user_ ; " ).append( newline );
System.out.println( "sql = " + sql );
try (
Connection conn = App.INSTANCE.provideDataSource().getConnection() ;
Statement statement = conn.createStatement() ;
ResultSet resultSet = statement.executeQuery( sql.toString() ) ;
)
{
while ( resultSet.next() )
{
userName = resultSet.getString( "name_" );
break; // Go no further. We need only the first row found.
}
}
catch ( SQLException e )
{
e.printStackTrace();
}
return userName;
}
public void initializeDatabase ( )
{
System.out.println( "DEBUG Running `initializeDatabase` of " + this.getClass().getCanonicalName() );
String newline = "\n";
// Create table.
StringBuilder sql = new StringBuilder();
sql.append( "CREATE TABLE user_ ( " ).append( newline );
sql.append( "pkey_ IDENTITY NOT NULL PRIMARY KEY , " ).append( newline ); // `identity` = auto-incrementing long integer.
sql.append( "name_ VARCHAR NOT NULL " ).append( newline );
sql.append( ") " ).append( newline );
sql.append( ";" ).append( newline );
System.out.println( "sql = " + sql );
try (
Connection conn = App.INSTANCE.provideDataSource().getConnection() ;
Statement statement = conn.createStatement() ;
)
{
statement.executeUpdate( sql.toString() );
}
catch ( SQLException e )
{
e.printStackTrace();
}
System.out.println("DEBUG Finished `CREATE TABLE` statement.");
// Populate table.
sql = new StringBuilder();
sql.append( "INSERT INTO user_ ( name_ ) " ).append( newline );
sql.append( "VALUES " ).append( newline );
sql.append( "( 'Alice' ) , " ).append( newline );
sql.append( "( 'Bob' ) , " ).append( newline );
sql.append( "( 'Carol' ) " ).append( newline );
sql.append( ";" ).append( newline );
System.out.println( "sql = " + sql );
try (
Connection conn = App.INSTANCE.provideDataSource().getConnection() ;
Statement statement = conn.createStatement() ;
)
{
int rowsAffected = statement.executeUpdate( sql.toString() );
System.out.println( "DEBUG Inserted rows into name_ table: " + rowsAffected );
}
catch ( SQLException e )
{
e.printStackTrace();
}
System.out.println("DEBUG Finished `INSERT` statement.");
}
}
And lastly, the MainView class. I disable the #PWA annotation as we are not using that feature for progressive web apps.
package work.basil.example;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
/**
* The main view contains a button and a click listener.
*/
#Route ( "" )
//#PWA ( name = "Project Base for Vaadin", shortName = "Project Base" )
public class MainView extends VerticalLayout
{
private Label label;
private Button button;
public MainView ( )
{
// Widgets
this.label = new Label( "User: ?" );
this.button = new Button(
"Get user" ,
event -> {
Notification.show( "Getting user." );
String userName = App.INSTANCE.provideDatabase().getFirstUserName();
this.label.setText( "User: " + userName );
}
);
add( button );
// Arrange
this.add( label , button );
}
}

You should check if the ResultSet rs actually has results. When calling rs.next(), look if it returns false. How to check if ResultSet is empty
public String getUsername() {
String username = "QUERY-FAILED";
try {
start();
statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select userName from names");
if(rs.next() != false){
username = rs.getString(1);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
close();
}
return username;
}
Another thing I noticed is that from the query select * from names, one cannot be sure that the userName attribute is the first column, which you are reading with rs.getString(1). Make sure to write your query more precise to avoid hard-to-find bugs: select userName from names; Do this even if the table only has one column, because what if somebody prepended another column? it could break your application.

Related

Java tomcat application hangs, can't restart tomcat after stopping

I have deployed a mature web application to a new server on tomcat 7. The database the system uses is quite empty as we're in the early stages of configuring it for use.
Going to the app, you get a login page. Log in and it usually takes you to the main page of the app.
But after coming in the following morning, we always get the same problem:
We bring up the login screen - no problem
Enter our username and password - system hangs
We go to tomcat and using
the system tray, stop the service.
The stopping service progress bar appears then goes away, but the status
on the tomcat properties dialog still shows 'Started' and both the Start
and Stop buttons are disabled.
We check the tomcat logs and there are no errors
We restart the server and it works ok again
There is nothing obvious we can see. A tomcat 'Find Leaks' request shows nothing, and looking at the heap sizes on VisualVM shows a consistent pattern of heap takeup followed by garbage collection bringing it back down to the same low level (so no apparent leaks)
I thought it may be mysql connections timing out, but that shouldnt be the case because if I log in with the wrong password, the system goes to the database to check the password and returns as expected with 'wrong password'. The only point at which it fails is if you enter the correct password.
The only clue we have is that there is an error when logging in, where the system uses some custom code to figure out the users' host name:
2019-02-14 08:10:14,277 08:10:14.277 [http-bio-8080-exec-9] ERROR com.sw.app.ui.UserAuthenticatedWebSession - Unknown host!
java.net.UnknownHostException: null
at java.net.Inet6AddressImpl.getHostByAddr(Native Method) ~[na:1.8.0_201]
at java.net.InetAddress$2.getHostByAddr(Unknown Source) ~[na:1.8.0_201]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_201]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_201]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_201]
at com.sw.app.data.utils.NetUtilities.getHostName(NetUtilities.java:114) ~[app-data-4.0.15.1-SNAPSHOT.jar:na]
This is only invoked if the user logs in successfully to store where they are logging in from, but the exception is caught in the code and then just logged rather than propagated upwards, and then we use a default 'unknown' host name. This is the code:
public static String getHostName( InetAddress inaHost ) throws UnknownHostException
{
try {
Class<? extends InetAddress> clazz = Class.forName( "java.net.InetAddress" ).asSubclass( InetAddress.class );
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
constructors[0].setAccessible( true );
InetAddress ina = (InetAddress)constructors[0].newInstance();
Field[] fields = ina.getClass().getDeclaredFields();
for( Field field : fields ) {
// Depends on the version of java we are dealing with:
// Older version - single nameservice
if( field.getName().equals( "nameService" ) ) {
return getHostName( field.get( null ), inaHost );
} else if( field.getName().equals( "nameServices" ) ) {
// newer version - multiple name services possible
StringBuilder builder = new StringBuilder();
field.setAccessible( true );
// A list of nameservice objects
#SuppressWarnings( "unchecked" )
List<Object> nameServices = (List<Object>)field.get( null );
for( Object nameService : nameServices ) {
String hostName = getHostName( nameService, inaHost );
if( builder.length() > 0 ) {
builder.append( ", " );
}
builder.append( hostName );
}
return builder.toString();
}
}
} catch( ClassNotFoundException cnfe ) {
throw new InvalidOperationException( "Class not found when looking up host name", cnfe );
} catch( IllegalAccessException iae ) {
throw new InvalidOperationException( "Cannot access method/field", iae );
} catch( InstantiationException ie ) {
throw new InvalidOperationException( "Cannot instantiate class", ie );
} catch( InvocationTargetException ite ) {
throw (UnknownHostException)ite.getCause();
}
return null;
}
/**
* Get the host name using reflection on the hidden class implementation of the InetAddress details.
* #param p_nameService
* #param p_address
* #return
* #throws IllegalAccessException
* #throws InvocationTargetException
*/
private static String getHostName( Object nameService, InetAddress address ) throws IllegalAccessException, InvocationTargetException {
Method[] methods = nameService.getClass().getDeclaredMethods();
for( Method method : methods ) {
// The nameService is assumed to have a method, getHostByAddr, which takes the byte[] inet address
if( method.getName().equals( "getHostByAddr" ) ) {
method.setAccessible( true );
return (String)method.invoke( nameService, address.getAddress() );
}
}
return "";
}
Does anyone have similar issues?
-- Edit --
Here is the database configuration bean class.
#Configuration
public class AppPersistence {
private static final Logger LOGGER = LoggerFactory.getLogger( AppPersistence.class );
protected static final String INTERNAL_IP_DOMAIN = "*******";
protected static final String JDBC_PROTOCOL = "jdbc:mysql://";
protected static final String DEFAULT_DATABASE_NAME = "*******";
/** The path for context-based property lookups */
protected static final String CONTEXT_LOOKUP_PATH = "java:comp/env";
/** This is the default location for the database - on the same machine as the deployment */
protected static final String DB_LOCAL = JDBC_PROTOCOL + "localhost:3306/" + DEFAULT_DATABASE_NAME;
#Bean
public DataSource createDataSource() throws Exception {
BasicDataSource source = new BasicDataSource();
// allow for parameterised config
source.setDriverClassName( Driver.class.getName() );
source.setUrl( getProperty( "app.database.url", DB_LOCAL ) );
source.setUsername( getProperty( "app.database.username", "*****" ) );
source.setPassword( getProperty( "app.database.password", "****" ) );
LOGGER.warn( "Connecting to: " + source.getUrl() );
return source;
}
protected String getProperty( String name, String default ) {
// first check system properties
String val = System.getProperty( name );
if( val != null ) {
logLookup( "System Properties", name, val );
return val;
}
// check environment variables
val = System.getenv( name );
if( val != null ) {
logLookup( "System Environment Variables", name, val );
return val;
}
// if we are deployed to a container, check the environment variables in that.
try {
Context context = InitialContext.doLookup( "java:comp/env" );
if( context != null ) {
Object valObj = context.lookup( name );
if( valObj != null ) {
logLookup( "Context", name, valObj.toString() );
return valObj.toString();
}
}
} catch( NamingException e ) {
// if running on a dev machine this will probably happen
LOGGER.warn( "Could not find context for lookup of " + p_name + " - assuming running in dev mode with defaults. Error was: " + e.toString( true ) );
LOGGER.info( "Error received on lookup of " + name + ":", e );
}
return p_default;
}
protected void logLookup( String source, String lookup, String value ) {
if( value.contains( "password" ) ) {
// avoid displaying any password info
LOGGER.warn( "Successfully looked up sensitive value from " + source + " for name '" + lookup + "': [******]" );
} else {
LOGGER.warn( "Successfully looked up value from " + source + " for name '" + lookup + "': '" + value + "'" );
}
}
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory( DataSource dataSource ) {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setPersistenceUnitName( "com.sw.app.data.persistence" );
entityManagerFactory.setDataSource( dataSource );
entityManagerFactory.setJpaVendorAdapter( new HibernateJpaVendorAdapter() );
entityManagerFactory.setLoadTimeWeaver( new InstrumentationLoadTimeWeaver() );
entityManagerFactory.setJpaDialect( new HibernateJpaDialect() );
entityManagerFactory.setPackagesToScan( "com.sw.app.data", "com.sw.app.rawimport",
"com.sw.app.view", "com.sw.app.warranty" );
entityManagerFactory.setJpaPropertyMap( hibernateJpaProperties( dataSource ) );
return entityManagerFactory;
}
private Map<String, ?> hibernateJpaProperties( DataSource dataSource ) {
HashMap<String, String> properties = new HashMap<>();
// Need to copy these values over, otherwise c3p0 can't see them.
if( dataSource instanceof BasicDataSource ) {
BasicDataSource source = (BasicDataSource)p_dataSource;
properties.put( "hibernate.connection.driver_class", source.getDriverClassName() );
properties.put( "hibernate.connection.url", source.getUrl() );
properties.put( "hibernate.connection.username", source.getUsername() );
properties.put( "hibernate.connection.password", source.getPassword() );
}
// Added to avoid some merge problems when updating entities (eg contact to custimport)
properties.put( "hibernate.event.merge.entity_copy_observer", "allow" );
// Second level cache
properties.put( "hibernate.cache.use_second_level_cache", "true" );
properties.put( "hibernate.cache.use_query_cache", "true" );
properties.put( "hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider" );
properties.put( "hibernate.cache.region.factory_class", EhCacheRegionFactory.class.getName() );
properties.put( "hibernate.generate_statistics", "false" );
properties.put( "hibernate.show_sql", "false" );
properties.put( "hibernate.format_sql", "false" );
// validate | update | create | create-drop -->
properties.put( "hibernate.hbm2ddl.auto", "update" );
properties.put( "hibernate.dialect", MySQL5Dialect.class.getName() );
// [main] WARN org.hibernate.cfg.AnnotationBinder - HHH000457: Joined inheritance hierarchy [com.sw.system4.data.collateral.AbstractCollateral] defined explicit #DiscriminatorColumn. Legacy Hibernate behavior was to ignore the #DiscriminatorColumn. However, as part of issue HHH-6911 we now apply the explicit #DiscriminatorColumn. If you would prefer the legacy behavior, enable the `hibernate.discriminator.ignore_explicit_for_joined` setting (hibernate.discriminator.ignore_explicit_for_joined=true) -->
properties.put( "hibernate.discriminator.ignore_explicit_for_joined", "true" );
//properties.put("hibernate.hbm2ddl.import_files", "insert-data.sql");
//properties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
// This wasnt required in persistence.xml, but for some reason is here.
properties.put( "hibernate.connection.provider_class", C3P0ConnectionProvider.class.getName() );
// just adding c3p0 props was enough in persistence.xml, but not here.
properties.put( "hibernate.c3p0.min_size", "5" );
properties.put( "hibernate.c3p0.max_size", "20" );
properties.put( "hibernate.c3p0.timeout", "300" ); // 5mins
properties.put( "hibernate.c3p0.max_statements", "50" );
properties.put( "hibernate.c3p0.idle_test_period", "100" );
properties.put( "hibernate.c3p0.preferredTestQuery", "select 1" );
properties.put( "hibernate.c3p0.testConnectionOnCheckout", "true" );
properties.put( "hibernate.c3p0.numHelperThreads", "12" );
properties.put( "hibernate.c3p0.maxStatementsPerConnection", "25" );
properties.put( "hibernate.c3p0.statementCacheNumDeferredCloseThreads", "1" );
return l_properties;
}
#Bean
public JpaTransactionManager transactionManager( EntityManagerFactory emf ) {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory( emf );
return jpaTransactionManager;
}
}
What is your database? Is it a Cloud Database? I had a similar problem with CloudSQL. What happened was when some active connections didn't do anything with the database, from the database side, it rejects the connection after some hours. But on the application side, you will see it as an active connection. I have used Apache DBCP pool, and I was able to solve the problem using this in the database configurations.
dataSource.setValidationQuery("SELECT 1");
dataSource.setTestOnBorrow(true);
dataSource.setTestWhileIdle(true);
Because you are using C3P0, following commands should work for you.
hibernate.dbcp.testOnBorrow=true
hibernate.dbcp.testOnReturn=true
hibernate.dbcp.validationQuery=SELECT 1

How to run dql search query in java?

I want to check whether a file is present in a particular folder in documentum using Java.
Following is my code,
import com.documentum.com.DfClientX;
import com.documentum.com.IDfClientX;
import com.documentum.fc.client.DfQuery;
import com.documentum.fc.client.IDfClient;
import com.documentum.fc.client.IDfCollection;
import com.documentum.fc.client.IDfFolder;
import com.documentum.fc.client.IDfQuery;
import com.documentum.fc.client.IDfSession;
import com.documentum.fc.client.IDfSessionManager;
import com.documentum.fc.common.DfException;
import com.documentum.fc.common.DfId;
import com.documentum.fc.common.IDfLoginInfo;
import com.documentum.operations.IDfDeleteOperation;
public class CountFiles {
// Documentum target repository where the files will be imported
private static final String REPO_NAME = "rep";
public static void main( String[] args ) throws Exception {
try {
String username = "user";
String password = "pwd";
System.out.println( "Starting to connect ..." );
IDfSessionManager sessMgr = createSessionManager( );
addIdentity( sessMgr, username, password);
IDfSession sess = sessMgr.getSession(REPO_NAME );
System.out.println( "Successfully connected to the server.");
queryDocumentum(sess);
} catch( Exception ex ) {
System.out.println( ex );
ex.printStackTrace( );
}
}
private static void queryDocumentum(IDfSession sess) throws DfException {
IDfQuery query = new DfQuery();
String queryStr= "select count(*) from dm_document WHERE FOLDER ('/XXX/YYY',DESCEND) search document contains 'abc.pdf' ";
query.setDQL(queryStr);
IDfCollection coll = query.execute(sess,IDfQuery.DF_EXEC_QUERY);
while(coll.next())
{
System.out.println("Result of method: " + coll.getValueAt(0));
}
coll.close();
}
/**
* Creates a new session manager instance. The session manager does not have
* any identities associated with it.
*
* #return a new session manager object.
* #throws DfException
*/
private static IDfSessionManager createSessionManager( )
throws Exception {
IDfClientX clientX = new DfClientX( );
IDfClient localClient = clientX.getLocalClient( );
IDfSessionManager sessMgr = localClient.newSessionManager( );
System.out.println( "Created session manager." );
return sessMgr;
}
/**
* Adds a new identity to the session manager.
*
*/
private static void addIdentity( final IDfSessionManager sm,
final String username, final String password )
throws Exception {
IDfClientX clientX = new DfClientX( );
IDfLoginInfo li = clientX.getLoginInfo( );
li.setUser( username );
li.setPassword( password );
// check if session manager already has an identity.
// if yes, remove it.
if( sm.hasIdentity( REPO_NAME ) ) {
sm.clearIdentity( REPO_NAME );
System.out.println( "Cleared identity on :" + REPO_NAME );
}
sm.setIdentity( REPO_NAME, li );
System.out.println( "Set up identity for the user." );
}
}
I am getting the following exception - [DM_QUERY_E_SYNTAX]error: "A Parser Error (syntax error) has occurred in the vicinity of "select count(*) from dm_document WHERE FOLDER ('/XXX/YYY',DESCEND) search document contains 'abc.pdf'". what is the issue in the query/code?
Well, obviously your DQL query is wrong:
select count(*) from dm_document WHERE FOLDER ('/XXX/YYY',DESCEND) search document contains 'abc.pdf'
if you just want to check if some document named 'abc.pdf' is present in path /XXX/YYY and all folders below (keyword 'descend') then the DQL should be more like this, you don't need Full Text search capabilities for that:
select count(*) from dm_document where folder('/XXX/YYY', DESCEND) and object_name = 'abc.pdf'
If you don't know exact file name you can use "LIKE", for example:
select count(*) from dm_document where folder('/XXX/YYY', DESCEND) and object_name LIKE '%abc.pdf'

Neo4j - Browser Visualization Error

I am trying to use the 'Hello World' for Neo4j. The problem is that I when I boot up the server and check the neo4j's browser (localhost:7474), I cannot see any graphic visualisation of my nodes.
import java.io.File;
import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
public class MyFirstMain
{
private static final String DB_PATH = "/neo4j/data/graph.db";
public String greeting;
// START SNIPPET: vars
GraphDatabaseService graphDb;
Node firstNode;
Node secondNode;
Relationship relationship;
// END SNIPPET: vars
// START SNIPPET: createReltype
private static enum RelTypes implements RelationshipType
{
KNOWS
}
// END SNIPPET: createReltype
public static void main( final String[] args )
{
MyFirstMain hello = new MyFirstMain();
hello.createDb();
// hello.removeData();
// hello.shutDown();
}
void createDb()
{
// START SNIPPET: startDb
graphDb = new GraphDatabaseFactory().newEmbeddedDatabase( DB_PATH );
registerShutdownHook( graphDb );
// END SNIPPET: startDb
// START SNIPPET: transaction
try ( Transaction tx = graphDb.beginTx() )
{
// Database operations go here
// END SNIPPET: transaction
// START SNIPPET: addData
firstNode = graphDb.createNode();
firstNode.setProperty( "message", "Hello, " );
secondNode = graphDb.createNode();
secondNode.setProperty( "message", "World!" );
relationship = firstNode.createRelationshipTo( secondNode, RelTypes.KNOWS );
relationship.setProperty( "message", "brave Neo4j " );
// END SNIPPET: addData
// START SNIPPET: readData
System.out.print( firstNode.getProperty( "message" ) );
System.out.print( relationship.getProperty( "message" ) );
System.out.print( secondNode.getProperty( "message" ) );
// END SNIPPET: readData
greeting = ( (String) firstNode.getProperty( "message" ) )
+ ( (String) relationship.getProperty( "message" ) )
+ ( (String) secondNode.getProperty( "message" ) );
// START SNIPPET: transaction
tx.success();
}
// END SNIPPET: transaction
}
// START SNIPPET: shutdownHook
private static void registerShutdownHook( final GraphDatabaseService graphDb )
{
// Registers a shutdown hook for the Neo4j instance so that it
// shuts down nicely when the VM exits (even if you "Ctrl-C" the
// running application).
Runtime.getRuntime().addShutdownHook( new Thread()
{
#Override
public void run()
{
graphDb.shutdown();
}
} );
}
// END SNIPPET: shutdownHook
}
I do NOT want to use anything else, except the neo4j's built in browser.
How should I proceed?
Thanks in advance.
You must shut down the server, then create your data, then start the server again, you cannot use the same data directory from two database processes at the same time.
Your DB_PATH is also wrong, you can't have a star in there.

c3p0 and Oracle object type problem

had several apps with jdbc and Oracle 10g. Now I´m changing the apps for use c3p0. But I have some problems working with Oracle types.
I Have this Oracle type:
CREATE OR REPLACE
TYPE DATAOBJ AS OBJECT
(
ID NUMBER,
NAME VARCHAR2(50)
)
And this Oracle function:
CREATE OR REPLACE FUNCTION F_IS_DATA_OBJECT (datar in DATAOBJ) RETURN varchar2 IS
tmpVar varchar2(150);
BEGIN
tmpVar := 'Data object:';
if datar.id is not null then
tmpVar := tmpVar || 'id=' || datar.ID;
end if;
if datar.name is not null then
tmpVar := tmpVar || 'name=' || datar.name;
end if;
return tmpVar;
EXCEPTION
WHEN NO_DATA_FOUND THEN
NULL;
WHEN OTHERS THEN
RAISE;
END F_IS_DATA_OBJECT;
then I have a app in Java with c3p0 with next classes:
Dataobj.class to represent the object type:
package c3p0pruebas.modelo;
import java.io.Serializable;
import java.sql.SQLData;
import java.sql.SQLException;
import java.sql.SQLInput;
import java.sql.SQLOutput;
public class Dataobj implements SQLData, Serializable {
private String name;
private Integer id;
public Dataobj() {
}
public String getSQLTypeName() {
return "DATAOBJ";
}
public void writeSQL(SQLOutput stream) throws SQLException {
stream.writeInt(id.intValue());
stream.writeString(name);
}
public void readSQL(SQLInput stream, String typeName) throws SQLException {
id = new Integer(stream.readInt());
name = stream.readString();
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
... and its gets and sets ....
And the main class and main method:
Connection connection = DBConnectionManager.getInstance().getConnection("Mypool"); //I use a class to get connection
CallableStatement cs = null;
String error = "";
try {
/*
//First I made a NativeExtractor of the connection, but the result is the same, I got it from Spring framework.
//C3P0NativeJdbcExtractor extractor = new C3P0NativeJdbcExtractor();
//OracleConnection newConnection = (OracleConnection) extractor.getNativeConnection(connection);
//cs = (OracleCallableStatement) newConnection.prepareCall("{? = call F_IS_DATA_OBJECT(?)}");
*/
//Creates the object
Dataobj obj = new Dataobj();
obj.setId(new Integer(33));
obj.setName("myName");
cs = connection.prepareCall("{? = call F_IS_DATA_OBJECT(?)}");
cs.registerOutParameter(1, OracleTypes.VARCHAR);
cs.setObject(2, obj);
cs.execute();
error = cs.getString(1);
System.out.println("Result: " + error);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
closeDBObjects(null,cs,null);
}
closeDBObjects(null, null, connection); //Close connection
The execution gets:
Data object: id=33.
I cant get the String (Varchar2) value, the name string.
With oracle arrays of object type, I have the same problem, It worked nice with JDBC. When I worked with Arrays, also, it hasn´t the string values:
//Here I use a NativeConnection ...
Dataobj arrayOfData[] = new Dataobj[myDataObj.size()];
... //Makes the array of DataObj.
ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("OBJ_ARRAY", newConnection);
ARRAY arrayDatas = new ARRAY(descriptor, newConnection, arrayOfData);
//In this step, objects of arrayDatas haven´t the name string...
Thanks!!!
OK, It finally works.
Searching, We found out the answer:
We change data definition in the database and now it works:
CREATE OR REPLACE
TYPE "DATAOBJ" AS OBJECT
(
vid NUMBER,
vname NCHAR(50)
)
Thanks!
I had the same problem and i solved without change VARCHAR2 to NCHAR, because for me, the NCHAR doesn't appear the String in the Oracle, stay "?" in all the positions.
I changed the oracle driver of the WAR to the version of my database, in my case was 11.2.0.1.0:
http://www.oracle.com/technetwork/database/enterprise-edition/jdbc-112010-090769.html
And i put another driver, that is the NLS for Oracle Objects and Collections:
http://download.oracle.com/otn/utilities_drivers/jdbc/112/orai18n.jar
With this, i solved the problem and the VARCHAR2 worked fine.
Good luck.

how to create table if it doesn't exist using Derby Db

I am new to apache derby and I cant seem to make work
CREATE TABLE IF NOT EXISTS table1 ...
as can be achieved in MySql etc. I am getting a 'Syntax error: Encountered "NOT" at line 1, column 17.', when I try to run this SQL statement in my Java program.
I checked in the documentation page for Derby Db Create Statements, but couldn't find such an alternative.
Create the table, catch the SQLException and check SQL status code.
The full list of error codes can be found here but I couldn't find Table <value> already exists; it's probably X0Y68. The code you need is X0Y32.
Just run the code once and print the error code. Don't forget to add a test to make sure the code works; this way, you can catch changes in the error code (should not happen ...).
In my projects, I usually add a helper class with static methods so I can write:
} catch( SQLException e ) {
if( DerbyHelper.tableAlreadyExists( e ) ) {
return; // That's OK
}
throw e;
}
Another option is to run a SELECT against the table and check the status code (which should be 42X05). But that's a second command you need to send and it doesn't offer any additional information.
What's worse, it can fail for other reasons than "Table doesn't exist", so the "create-and-ignore-error" is better IMO.
Derby does not support that sql-statement.
In my program I parse all the Tables from the Database into a Set and check if the table exists there.
Like this:
private Set<String> getDBTables(Connection targetDBConn) throws SQLException
{
Set<String> set = new HashSet<String>();
DatabaseMetaData dbmeta = targetDBConn.getMetaData();
readDBTable(set, dbmeta, "TABLE", null);
readDBTable(set, dbmeta, "VIEW", null);
return set;
}
private void readDBTable(Set<String> set, DatabaseMetaData dbmeta, String searchCriteria, String schema)
throws SQLException
{
ResultSet rs = dbmeta.getTables(null, schema, null, new String[]
{ searchCriteria });
while (rs.next())
{
set.add(rs.getString("TABLE_NAME").toLowerCase());
}
}
the query you are executing does not supported by Derby db. Instead, if you know the name of the table you can find if table exists or not quite easily.
public boolean isTableExist(String sTablename) throws SQLException{
if(connection!=null)
{
DatabaseMetaData dbmd = connection.getMetaData();
ResultSet rs = dbmd.getTables(null, null, sTablename.toUpperCase(),null);
if(rs.next())
{
System.out.println("Table "+rs.getString("TABLE_NAME")+"already exists !!");
}
else
{
System.out.println("Write your create table function here !!!");
}
return true;
}
return false;
}
Catch is to specify name of the table in Uppercase else you won't be able to find table name in metadata.
to check if table is exist :
Connection con = DriverManager.getConnection(url);
ResultSet res = con.getMetaData().getTables(null, Schema_Name, table_name.toUpperCase(), null);//Default schema name is "APP"
if(res.next())
{
//do some thing;
}else{
JOptionPane.showMessageDialog(null, table_name +" not exist");
}
to show all tables name :
Connection con = DriverManager.getConnection(url);
ResultSet res = con.getMetaData().getTables(null, Schema_Name, "%", null);//Default schema name is "APP"
while(res.next())
{
JOptionPane.showMessageDialog(null, res.getString(3) + " is exist");//Show table name
}else{
JOptionPane.showMessageDialog(null, table_name +" not exist");
}
Following Aaron Digulla's lead with a DerbyUtils class to check if the table exists, this is the solution I came up with :
Calling class
public void createTable(String name) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = daoFactory.getConnection();
String sql = String.format(SQL_CREATE_TABLE, name);
preparedStatement = connection.prepareStatement(sql, Statement.NO_GENERATED_KEYS);
preparedStatement.execute();
} catch (SQLException e) {
if(DerbyUtils.tableAlreadyExists(e)) { //check if the exception is because of pre-existing table.
logger.info("Talbe " + name + " already exists. No need to recreate");
} else {
logger.error(e.getMessage() + " : " + e.getStackTrace());
}
} finally {
close(connection, preparedStatement); //DAOUtils silently closes
}
}
DerbyUtils
public class DerbyUtils {
public DerbyUtils() {
//empty constructor -- helper class
}
public static boolean tableAlreadyExists(SQLException e) {
boolean exists;
if(e.getSQLState().equals("X0Y32")) {
exists = true;
} else {
exists = false;
}
return exists;
}
}
See also
https://db.apache.org/derby/docs/10.2/ref/rrefexcept71493.html
I know this was marked with an answer but in case anyone wanted another way of checking I wanted to post anyway. Here I check the table metadata with a method that returns a boolean, true if exists, false if it doesn't. Hope it helps others if they are looking.
private static Connection conn = null;
private static Statement st = null;
private static ResultSet rs = null;
private static DatabaseMetaData dmd;
public Boolean firstTime()
{
try
{
dmd = conn.getMetaData();
rs = dmd.getTables(null, "APP", "LOGIN", null);
return !rs.next();
} catch (SQLException ex)
{
Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex);
return false;
}
}
Another solution with 2 conditions:
Willing to drop table before creating each time, with the same being present in a .sql file
Are using Spring and hence willing to use spring-test as a Maven dependency, your life can become much simpler with it's #Sql annotation
So, firstly adding this as a dependency to your pom:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.2.5.RELEASE</version>
<scope>test</scope>
</dependency>
Secondly, assuming you have an sql that drops, creates table a in a file
rectangle.sql:
DROP TABLE rectangles;
CREATE TABLE rectangles (
id INTEGER NOT NULL PRIMARY KEY,
width INTEGER NOT NULL,
height INTEGER NOT NULL
);
And you have a test class BlahTest that should run this sql before doing whatever test it is to run, simply add the following #Sql annotation to your class:
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.jdbc.SqlConfig;
import org.springframework.test.context.jdbc.SqlConfig.ErrorMode;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes=XyzClientConfig.class)
#Sql(scripts="/sql/ddl/rectangle.sql", config=#SqlConfig (errorMode=ErrorMode.IGNORE_FAILED_DROPS))
public class BlahTest {
...
}
The specified config attribute value's #SqlConfig has the magic that makes it skip the drop statement errors in case the table doesn't exist. I believe it's been written to specifically target these types of databases that don't support IF EXISTS for dropping / table creation (which derby really should, even if it's not part of the SQL standard at the moment)
This answer is way late, but it might be helpful for someone.
The following Java (standard JDBC) code can be used to check whether a table exists or not, and if it does then it can be created;
String query = "SELECT TRUE FROM SYS.SYSTABLES WHERE TABLENAME = ? AND TABLETYPE = 'T'"; // Leave TABLETYPE out if you don't care about it
PreparedStatement ps = connection.prepareStatement(query);
ps.setString(1, "TABLE_NAME"); // Must be in capitals
ResultSet rs = ps.executeQuery();
if ( rs.next() && rs.getBoolean(1) )
{
// Table exists
}
else
{
// Table does NOT exist ... create it
}
Here is a solution that will you can script in SQL.
Create a Class like the following:
package user.fenris.spring.extensions;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
public class SqlCreateIfNotExists {
private static Log log = LogFactory.getLog(SqlCreateIfNotExists.class);
public static void createTableIfNotExists(String tablename, String ddl) throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:default:connection");
if (conn != null) {
JdbcTemplate template = new JdbcTemplate(new SingleConnectionDataSource(conn, true));
int count = template.queryForInt("select count(*) from SYS.SYSTABLES where TABLENAME = ?", tablename);
log.debug("Count: " + count);
if (count == 0) {
log.debug("Executing sql statement: " + ddl);
template.execute(sql);
} else {
log.debug("Table exists. Skipping sql execution...");
}
}
}
}
Note: you don't have to use spring, you can write it in straight JDBC, but then you have to know how to do it correctly. (Left as an exercise for the reader). Also, you could rewrite this to parse out the table name from the ddl parameter. Another thing would be to do proper error handling.
Make sure the class is compiled and placed in the classpath of the VM the database will be running in.
Write your SQL script:
-- 2K for ddl statement should be enough. You want more? Seriously?
create procedure CreateTableIfNotExists(in tablename varchar(128), in ddl varchar(2048))
PARAMETER STYLE JAVA
MODIFIES SQL DATA
language java
external name 'user.fenris.spring.extensions.SqlCreateIfNotExists.createTableIfNotExists';
call CreateTableIfNotExists('TABLE_NAME_MUST_BE_ALL_CAPS',
'create table TABLE_NAME_MUST_BE_ALL_CAPS
(entry_id int generated always as identity not null,
entry_timestamp timestamp,
username varchar(128) not null,
note varchar(1024) not null,
primary key (entry_id))');
-- you don't have to drop this, but you would have to create a similar
-- procedure to create the CreateTableIfNotExists procedure,
-- (i.e. CreateProcedureIfNotExists) but then it's turtles all the way down
drop procedure CreateIfNotExists;
???
profit
try {
connection.createStatement().execute("create table channels(channel varchar(20),topic varchar(20))");
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
}
Surround the create statement by try-catch.and make sure comment the e.printstacktace();
if it is already exists it does not show error ,otherwise it create table..!!

Categories