GraalVM native-image and Oracle ojdbc11-21.1 - java

I am currentliy experimenting with the native-image tool from GraalVM and the Oracle-driver. The source code compiles and generates an exe File without errors. But when I start the program it gets a java.lang.RuntimeException: Missing character set id 170 not loaded at image build time.
I am connecting to a database with characterset NLS_CHARACTERSET = EE8MSWIN1250.
When I use a database with NLS_CHARACTERSET = AL32UTF8 the connection works fine.
I am using GraalVM CE 21.0.0.2 (build 11.0.10+8-jvmci-21.0-b06 and ojdbc11-21.1.0.0.jar on a Windows 10 64bit Computer.
Below ist the error message and the source code. I used the sample code from https://github.com/oracle/oracle-db-examples/blob/master/java/jdbc/ConnectionSamples/DataSourceSample.java
Exception in thread "main" java.lang.RuntimeException: Missing character set id 170 not loaded at image build time at oracle.sql.CharacterSet.make(CharacterSet.java:121) at oracle.jdbc.driver.DBConversion.init(DBConversion.java:184) at oracle.jdbc.driver.DBConversion.(DBConversion.java:137) at oracle.jdbc.driver.T4CConnection.doCharSetNegotiation(T4CConnection.java:2607) at oracle.jdbc.driver.T4CConnection.connect(T4CConnection.java:2176) at oracle.jdbc.driver.T4CConnection.logon(T4CConnection.java:644) at oracle.jdbc.driver.PhysicalConnection.connect(PhysicalConnection.java:1069) at oracle.jdbc.driver.T4CDriverExtension.getConnection(T4CDriverExtension.java:90) at oracle.jdbc.driver.OracleDriver.connect(OracleDriver.java:681) at oracle.jdbc.datasource.impl.OracleDataSource.getPhysicalConnection(OracleDataSource.java:569) at oracle.jdbc.datasource.impl.OracleDataSource.getConnection(OracleDataSource.java:355) at oracle.jdbc.datasource.impl.OracleDataSource.getConnectionInternal(OracleDataSource.java:2014) at oracle.jdbc.datasource.impl.OracleDataSource.getConnection(OracleDataSource.java:330) at oracle.jdbc.datasource.impl.OracleDataSource.getConnection(OracleDataSource.java:291) at DataSourceSample.main(DataSourceSample.java:24)
DataSourceSample.java
/* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.*/
import java.sql.SQLException;
import java.util.Properties;
import oracle.jdbc.pool.OracleDataSource;
import oracle.jdbc.OracleConnection;
import java.sql.DatabaseMetaData;
public class DataSourceSample {
final static String DB_URL= "jdbc:oracle:thin:#****:1525/****";
final static String DB_USER = "****";
final static String DB_PASSWORD = "****";
public static void main(String args[]) throws SQLException {
Properties info = new Properties();
info.put(OracleConnection.CONNECTION_PROPERTY_USER_NAME, DB_USER);
info.put(OracleConnection.CONNECTION_PROPERTY_PASSWORD, DB_PASSWORD);
info.put(OracleConnection.CONNECTION_PROPERTY_DEFAULT_ROW_PREFETCH, "20");
OracleDataSource ods = new OracleDataSource();
ods.setURL(DB_URL);
ods.setConnectionProperties(info);
try (OracleConnection connection = (OracleConnection) ods.getConnection()) {
DatabaseMetaData dbmd = connection.getMetaData();
System.out.println("Driver Name: " + dbmd.getDriverName());
System.out.println("Driver Version: " + dbmd.getDriverVersion());
System.out.println("Default Row Prefetch Value is: " +
connection.getDefaultRowPrefetch());
System.out.println("Database Username is: " + connection.getUserName());
System.out.println();
}
}
}

I'm not 100% sure that it's the culprit, but here's the general idea about native image and charsets.
Native image doesn't include all possible charsets by default, you can configure it to do so by using the following option:
-H:+AddAllCharsets
Another possible option is to initialize the CharSet onject in a class that is configured to be initialized at build time of the native image. Then the charset object will be saved in the "image heap" and will be available at runtime.
Here's a sample application that illustrates this behavior: https://github.com/shelajev/workshop/tree/main/3
Of course it might be something different, but I think this should solve the problem you're experiencing.

Related

How do I connect to the database MS Access?

I have a Maven Project and I am trying to create a connection to my MS Access db. The problem is that it does not open.
I do not receive any type of error, but the program remains active without returning the connection. I tried to stay on hold two hours but nothing. The databaseProduction WellSys is linked to ProdWheelTableMasterSys and WhellDemand.
My code is:
package com.sealed.air.SealedAir;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class App {
public static void main(String[] args) {
String conex = "jdbc:ucanaccess://";
String url = "C:/DB/ProductionWhellSys.accdb";
try {
System.out.println("Connecting");
Connection con = DriverManager.getConnection(conex+url);
System.out.println("Connected");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
And the result in the console is:
Connecting
My DB MS access properties:
console.bat output:
saved query in Access:
I have tried changing the "" in '' but I do not understand because it gives me the same error. Another mistake I found was:
Error message was: unexpected token: , required: )
It looks like you reported two different issues:
the first one is that the "the program remains active without
returning the connection" but seeing your App test, this doesn't seem due to ucanaccess... did you set Openexclusive=true?

How to force renaming of variable names in eclipse mars

How can I force eclipse mars to rename variable names?
When I try, I get
This refactoring cannot be performed correctly due to syntax errors in
the compilation unit.
The dialog only offers "Cancel".
It was possible to do this in older versions of eclipse, and I used the feature extensively, for example after copy&paste of code snippets found on the net.
Note this is not a duplicate of Refactoring variable names in Eclipse .
Edit 3 (summary of what happened):
In the code (shown below) were not only those common errors like missing imports or undeclared variables, but also a missing ";", thus a true syntax error. This, at first hidden among several other compiling issues, caused eclipse to refuse the refactoring.
As it turned out, this is not a special feature of mars but also of older versions of eclipse.
Edit: here comes my example code. It is mainly based on the examples from tutorialspoint for mongodb but very probably doesn't have anything to do with mongo.
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.MongoCredential;
import com.mongodb.client.MongoDatabase;
public class MongoDBJDBC2 {
private static String myUserName;
private static String myPassword;
private static String myHost = "localhost";
private static String myDatabaseName = "mydb";
private static MongoDatabase db;
public MongoDBJDBC2() {
initDb();
// TODO Auto-generated constructor stub
}
public static void main(String args[]) {
MongoDBJDBC2 mo = new MongoDBJDBC2();
}
private static void initDb() {
MongoClientURI uri = new MongoClientURI(
"mongodb://" + myUserName + ":" + myPassword + "#" + myHost + "/?authSource=db1");
try (MongoClient mongoClient = new MongoClient(uri);) {
db = mongoClient.getDatabase(myDatabaseName);
System.out.println("Connect to database successfully");
// boolean auth = db.authenticate(myUserName, myPassword);
} catch (Exception e) {
System.err.println(e.getClass().getName() + ": " + e.getMessage());
}
}
public static void main4( String args[] ) {
try{
// To connect to mongodb server
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
// Now connect to your databases
DB db = mongoClient.getDB( "test" );
System.out.println("Connect to database successfully");
boolean auth = db.authenticate(myUserName, myPassword);
System.out.println("Authentication: "+auth);
DBCollection coll = db.getCollection("mycol");
System.out.println("Collection mycol selected successfully");
DBCursor cursor = coll.find();
while (cursor.hasNext()) {
DBObject updateDocument = cursor.next();
updateDocument.put("likes","200")
col1.update(updateDocument);
}
System.out.println("Document updated successfully");
cursor = coll.find();
int i = 1;
while (cursor.hasNext()) {
System.out.println("Updated Document: "+i);
System.out.println(cursor.next());
i++;
}
}catch(Exception e){
System.err.println( e.getClass().getName() + ": " + e.getMessage() );
}
}
}
I try to rename db to myDb in
private static MongoDatabase db;
Previously I used eclipse Helios and never encountered this kind of "feature".
Edit2: I have located the fatal error. In method "main4" a semicolon is missing after
updateDocument.put("likes", "200")
Still don't understand why this upsets eclipse so much that it refuses to refactor, and I still would like to know if there is a way to force refactoring despite of errors.
Compilers issue two kinds of errors: syntax errors and all other kinds of errors, like "type mismatch" and "symbol not found". Eclipse complains about a syntax error. Are you sure that in previous occasions when Eclipse agreed to refactor your code despite the fact that it contained errors, it was syntax errors that your code contained? You see, there is a big difference.
Refactoring symbol names in java is far more involved than a simple text search and replace, the structure of your code has to be taken into account.
But in the case of a syntax error, the compiler has given up parsing your file, so it does not know the structure of your code: it does not know which tokens are variables, which tokens are types, which tokens are methods, etc. so it really cannot do the refactoring that you want.
So, if you must really proceed with your refactoring despite having syntax errors, then I am afraid that text search and replace is the way to go for you in this particular case.
But fixing the syntax errors before attempting to refactor would be the most prudent thing to do.
This happens when there is compilation issue in your code.
Fix the compilation issue than you can refactor your code.Yes this feature is recently introduced in newer version of eclipse.

Run LDAP queries on AD through JAVA Code

I have configured ADDC on windows server 2012 R2 and I have added two users into DC - one is windows 8 and another one is ubuntu.
Windows server 2012 username - DC
Windows 8.1 username - Win
Ubuntu username - Linux
I am trying to achieve this - I want to write java program in ubuntu, that will connect to ADDC and sends back, detailed user information on windows 8.1
My program is like -
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
public class LdapSearch {
public static void main(String[] args) throws Exception {
Hashtable env = new Hashtable();
String sp = "com.sun.jndi.ldap.LdapCtxFactory";
env.put(Context.INITIAL_CONTEXT_FACTORY, sp);
String ldapUrl = "ldap://server.com, dc=com";
env.put(Context.PROVIDER_URL, ldapUrl);
DirContext dctx = new InitialDirContext(env);
String base = "ou=name";
SearchControls sc = new SearchControls();
String[] attributeFilter = { "cn", "mail" };
sc.setReturningAttributes(attributeFilter);
sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
String filter = "(&(sn=W*)(l=Criteria*))";
NamingEnumeration results = dctx.search(base, filter, sc);
while (results.hasMore()) {
SearchResult sr = (SearchResult) results.next();
Attributes attrs = sr.getAttributes();
Attribute attr = attrs.get("cn");
System.out.print(attr.get() + ": ");
attr = attrs.get("mail");
System.out.println(attr.get());
}
dctx.close();
}
I am referring to above program and trying to achieve connection to AD through LDAP java. I dont know how to get ou, cn, etc.. I am very much new to the concepts of LDAP, ADDC.
Any idea on this? Please let me know.
Thanks,
saurabh
I've done a similar scenario in C# so am not sure about the connection settings in Java but as for similarities you should create a directory entry for the LDAP and provide the path, user name and password of authorized user who can access the active directory, i didnt provide DC in the path just the LDAP path and then the query filter parameters that searched based upon user first name was
Filter = "(& (SAMAccountName=" + name + ") (| (&(objectCategory=person)(objectClass=user)(!(homeMDB=*))(!(msExchHomeServerName=*)))(&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*))) ))";
then it would provide you with an result in an arraylist like object so you would query the rest of information you like by just providing the attribute name, you would find a list of LDAP attributes here
LDAP attributes

Java cast oracleconnection eclipse

please tell me if I have to provide more information/code, becasue I am a newbie in java and maybe don't know which informations you need.
I have to add a functionality to an existing Eclipse RCP application. In this I need to register an OracleDatabaseChangeListenet to my database connection.
The problem is, the connection already exists and is of the self-designed class Connection (is designed the way, that connections to other databases except Oracle are possible) that implements self-designed interface IConnection, but basically this connection is to an Oracle 11g database.
So I need to cast this connection to OracleConnection, but get an CastExceptionError. How can I manage to cast the connection?
public void openConnection( IConnection connection) throws JeffException {
URI conURI = connection.getUri();
String con_System = conURI.getPath().split( "/")[1]; //$NON-NLS-1$
String jdbcUrl = "jdbc:oracle:thin:#//" + conURI.getHost() + ":" + conURI.getPort() + "/" + con_System; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String Jeff_user = connection.getProperty( DatabaseConstants.JEFF_USERNAME);
String Jeff_password = connection.getProperty( DatabaseConstants.JEFF_PASSWORD);
//derby-embedded
// jdbcUrl="jdbc:derby:embeddedDB1;create=true";
String Jeff_proxy_password = connection.getProperty( DatabaseConstants.JEFF_PROXY_PASSWORD);
String Jeff_proxy_username = connection.getProperty( DatabaseConstants.JEFF_PROXY_USERNAME);
OracleConnection conn=(OracleConnection) connection;
The error I get is
java.lang.ClassCastException: mapackage.projectmanager.model.Connection cannot be cast to oracle.jdbc.OracleConnection
Thanks,
Jeff

classnotfoundexception sun.jdbc.odbc.jdbcodbcdriver and mapping_data_source ::init error : file not found

//java program to insert a row into a table using Statement![the output of the program is shown in the image provided by the link given at the bottom
import java.io.*;
import java.lang.*;
import java.sql.*;
class stmtinserts
{
public static void main(String a[])throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:test");
String s = "insert into cricket values(10, 'name1', 10000)";
Statement st=con.createStatement();
int i = st.executeUpdate(s);
if(i > 0)
System.out.println("data inserted ");
else
System.out.println("data is not inserted");
st.close();
con.close();
}
}
Click this link to view the image
My operating system is windows 7 ,32 bit system.
java version is 1.8.0_05.
also give me suggestions about error "mapping_data_source ::init error : file not found".
when ever i type java or javac, i find this error.
Help from any one is appreciated.thanks in advance.

Categories

Resources