In a managed bean that resides in a Database on the server Development I have this code:
s = ExtLibUtil.getCurrentSession();
theMap.put("Server Name", s.getServerName());
when I look at theMap after this has run I see Server Name and the value is blank. After this I get a datbase RepID and then try to open the database by RepID with
appDB = s.getDbDirectory(null).openDatabaseByReplicaID(repID);
if (appDB.isOpen()){
theMap.put(thisKey, repID);
}else{
theMap.put("DB " + thisKey, "Is Not Open");
}
if I have a rep copy of the database locally it opens it, if I remove the local Replica the open fails. If I change the line to:
appDB = s.getDbDirectory("Development").openDatabaseByReplicaID(repID);
the proper appDB opens. So it looks like the session thinks it is running locally because it return null for the server name. This is really strange, am I missing something? For the moment i have just hard coded the server name in the getDbDirectory but that wont work in the real world.
Is this XPiNC? That would consider the database to be running locally unless you've set the application property "Run server-based XPages on server"
String serverName = s.getEnvironmentString("ServerName", true);
or
String serverName = s.getEnvironmentString("ServerKeyFileName_Owner", true);
Related
I'm working on a project and I have put my database folder in project folder. How can I make a database connection to any directory rather than just default MySQL dir in Java?
String MySQLURL = "jdbc:mysql://localhost:3306/C:\\Program Files\\SnakeGame";
String UserName = "root";
String Password = "admin";
Connection con = null;
try {
con = DriverManager.getConnection(MySQLURL,UserName,Password);
if (con != null) {
System.out.println("Database connection is successful !!!!");
}
} catch (Exception e) {
e.printStackTrace();
}
When doing this, I get this error:
java.sql.SQLSyntaxErrorException: Unknown database 'c:\program files\snakegame'
Your connection URL is wrong
String MySQLURL = "jdbc:mysql://localhost:3306/C:\\Program Files\\SnakeGame";
I am not sure why your MySQLURL contains C:\Program Files\SnakeGame
The connection URL for the mysql database is
jdbc:mysql://localhost:3306/[DatabaseName]
Where jdbc is the API, mysql is the database, localhost is the server name on which mysql is running (we may also use the server's IP address here), 3306 is the port number, and [DatabaseName] is the name of the database created on the MySQL server.
Replace the [DatabaseName] name accordingly after creating the database in MySQL server
Combining localhost:3306/ with C:\\Program Files\\SnakeGame makes little sense for any database - either you're trying to connect to a file-based database (in which case the localhost... part makes no sense) or you're working with a server-based one (in which case the C:\... part makes no sense.
Also, this connection string would make little sense for a file-based database either because you didn't specify a specific file, just a path.
Incidentally, MySQL is server-based, not file-based. It's expecting a database name after the localhost:3306/ part, not a path (hence the error). The physical location of the actual database program is an installation/configuration issue - it has nothing to do with how you actually connect to the database server once it's already running.
Think about it this way: when you call an external database, web service, or web site, do you need to know which physical folder it's deployed to? Obviously not. The physical folders involved are completely irrelevant when calling MySQL or another database like this.
One of the comments pointed this out, but did you intend to use SQlite or some other file-based database here instead?
I'm trying to get all the database on my server. But specifying my server name in getDbDirectory() parameter as NALLN304/40/LLN/IBM gives me a error.
Directory NALLN304/40/LLN/IBM!! does not exist
it always add two exclamation mark at the end. I tried also as server name and mail file adding .nsf format at the end of mail file. NALLN304/40/LLN/IBM!!data0\126\1000031540.nsf also gives me the same error.
Snipper code below:
Session session = null;
Database db = null;
DbDirectory dir = null;
try
{
NotesThread.sinitThread();
session = NotesFactory.createSession();
System.out.println("User = " + session.getUserName());
dir = session.getDbDirectory("NALLN304/40/LLN/IBM");
System.out.println(dir.getName());
db = dir.getFirstDatabase(DbDirectory.DATABASE);
do
{
System.out.println("Title: " +db.getTitle());
}
while(dir.getNextDatabase() != null);
}
catch(NotesException ex)
{
ex.printStackTrace();
}
The error always points out to the db = dir.getFirstDatabase(DbDirectory.DATABASE); because dir.getFirstDatabase(DbDirectory.DATABASE) expects .nsf file even I specify the file format. Any reasons why I got this error?
Your problem is that your ID is not authenticating with the server. You need to take the output from this line:
System.out.println("User = " + session.getUserName());
And take it to your server administrators, ask why it is not being allowed to access the server, and ask them to assist you either by granting the necessary permissions or by giving you another ID that you can use.
Check the logs for server connection errors. Even if the server has to connect to itself. Faced the same problem. The server gave an error because it did not find a route for the connection. Added a new connection for the server to itself and everything was fixed
I have a lot of Lotus Notes / Domino (version 7) database to migrate to a new software.
On my workstation (with Lotus Notes installed), I'm using a standalone Java application to connect to a local replica an extract data.
However the replication of the distant database is still a manual process. I'd like to automatise it.
My java code basically looks like this :
Session localSession = NotesFactory.createSession(); // With Notes thread initialized
Session remoteSession = NotesFactory.createSession(SERVER, USER, PASSWORD);
Database localDb = localSession.getDbDirectory(null).openDatabase("local_name", true);
Database remoteDb = remoteSession.getDbDirectory(null).openDatabaseByReplicaID(REPLICA);
// ***EDITED CALLING INSTANCE BELOW***
remoteDb.createReplica(null, "local_name"); // Error thrown here
However the last line throws an exception (from memroy, but something like)
CN=****/***** does not have the right to create database on a server
How is it possible that I don't have the right to create database on my local computer ?
Is there any other way to programmaticly create a local replica from a distant database ?
Edit: changed calling instance of create replica to match my code causing the issue
My guess is that it's just giving you the wrong error message. One thing that's definitely wrong is that he first argument for createReplica should be an empty string, not a null pointer. I.e., try this:
localDb.createReplica("", "local_name");
Ok it looks like I found the answer.
AFAIU I had to open the database on the target server, using my local session, and run the createReplica() from here. This way, the createReplica is executed on my local Lotus Notes server, and the replica is created locally.
Session localSession = NotesFactory.createSession((String)null, (String)null, PASSWORD);
DbDirectory remoteDbDirectory = localSession.getDbDirectory(remoteSession.getServerName());
Database localSessionRemoteDatabase = remoteDbDirectory.openDatabaseByReplicaID(REMOTE_REPLICA_ID);
localSessionRemoteDatabase.createReplica("", LOCAL_FILE_NAME);
#Richard Schwartz Can you confirm this is ok ?
The only weird thing, is that it opens a prompt (like when it's expecting password) but the replica is created.
The process is executed within Eclipse.
I have created a java application in netbeans, and I intend to create an installer for the project.
For this I have created a jar file of the application, but I'm using the mysql database localhost.
How can I generate Jar File with Mysql localhost .
Can anyone help me please?
Thanks and regards
-----------------------------Edit---------------------------------------
Maybe not the best way to express myself, what I mean is that the database is created locally (localhost).
The connection of the application with the database is done this way:
Class.forName("com.mysql.jdbc.Driver");
return driverManager.getConnection("jdbc:mysql://localhost/database","root", "root");
I want to create a jar file of my application that has a database created locally.
I am going to explain a few things:
You do not need to hard - code the Connect URL into your code. This is why you are asking for ways of creating the database as localhost. I suggest you do not hard code the Connect URL in the code. Instead write it in an editable File either a Properties file or even a text file. Let the Application read the Editable file and pass the Parameters to the Code.
An Application running in your Local Machine where the database is will connect using Localhost. But a the same Application running remotely from another Machine whether in the Internet or Local access network will not Connect this way.That is why I am insisting on NOT Hard-Coding the Connect String.
The database Name, user, and Password Including the Host will change from time to Time depending on which environment the Application is running in. So again if the environment changes and the variables are not the same the Application will Not Connect to the database.
Suggestion:
User a Properties file:
db.host=192.168.1.23
db.user=root
db.password=root
db.dbname=database
Load the file as a Properties file:
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("config.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("db.host"));
System.out.println(prop.getProperty("db.user"));
System.out.println(prop.getProperty("db.password"));
System.out.println(prop.getProperty("db.dbname"));
//PASS YOUR CONNECT STRING
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://" + prop.getProperty("db.host") + "/" + prop.getProperty("db.dbname"), prop.getProperty("db.user"), prop.getProperty("db.password"));
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
This way you will never have to worry about what database the application is running on as you will just have to edit the config.properties and the Application will do the rest.
I hope I gave you an answer or better still other ideas on how to handle your situation.
I am using JDBC with the IBM toolbox to connect to tables on an AS400 server. When I establish a connection, I do not supply a username or a password to the URL, so I want the system to prompt the user. I don't want to hard-code any passwords or automatically put the admin password in. Bad idea. So prompt it is. I shove all my other settings into a property object and send it off:
prop = setProperties(false, //ensure the server always prompts for logon information
"","",SCHEMA);
connection = DriverManager.getConnection("jdbc:as400://" + IP_ADDRESS, prop);
My property-setting function looks like this:
private Properties setProperties(boolean local,String USER_ID,
String PASSWORD,String SCHEMA)
{
Properties prop = new Properties();
prop.put("naming", "system");
prop.put("errors", "full");
prop.put("libraries", SCHEMA + ", *LIBL");
if (!local) {
prop.put("user", USER_ID);
prop.put("password", PASSWORD);
}
return prop;
} // serverProperties
Since I don't supply a un or pw, I get this nifty little box:
Where does this box come from? Is this a JDBC thing or an IBM toolbox thing? And can I intercept it? I'm curious to know if I can skin it. When I translate this project from desktop to android, I want to be able to spawn an android UI (since android doesn't know swing or awt).
if I check those two checkboxes, no matter how many connections I open and close, whatever I put into those un/pw fields persists. Is there a way to grab that un/pw data from somewhere? I tried grabbing the connection properties object, but all the fields come back blank to me. IBM Toolbox properties documentation lists what properties to expect (I'm only interested in the general properties at this moment), but doesn't actually tell me where I can retrieve those properties.
This
Properties prop = connection.getClientInfo();
System.out.print(prop.toString());
yields me this{ClientProgramID=, ClientUser=, ApplicationName=, ClientHostname=, ClientAccounting=}
Not even close to what I should be expecting. I'm not sure where my properties go.
Thanks for any info!
That is the toolbox default prompt when no explicit user or password is provided in the connection URL:
If you want to skin or otherwise have more control you should create your own custom user interface and specify the user and password properties when you open the connection.
See the IBM Toolbox for Java JDBC properties for more information.