I'm trying to run the Apache Derby db from a flash drive. I copied the relevant .jar files and managed to start up the network server. But how to I specify the connection URL in connecting to the server? the database is in the flash drive labeled as G.
Used the following code, but came across an exception:
DriverManager.getConnection("jdbc:derby://localhost:1527");
java.sql.SQLException: The URL 'jdbc:derby://localhost:1527' is not properly formed.
How can I connect and use it as a normal database?
Thank You!
The presence of the flash drive is immaterial to this question. The most pertinent point is whether the Derby server is running in the embedded mode or in the network server mode.
From the URL used, it appears that you intend to connect to Derby running as a network server. This would be the case if you've started Derby using the startNetworkServer shell scripts, available in the Derby installation. If so, the connection URL format, as defined in the Derby documentation is as shown below. Note the presence of the databaseName parameter, which is missing in the URL posted in the question.
jdbc:derby://server[:port]/databaseName[;attributeKey=value]..
If you didn't want to start Derby in the network server mode, but instead as an embedded database, then the connection URL format is different. Note the absence of the port number, and the reliance on a subprotocol whose value are one of directory, classpath or jar. Examples of this format can also be found in the documentation.
jdbc:derby:[subsubprotocol:][databaseName][;attribute=value]*
Related
I just want to know how I can start derby in network server mode and still be able to get an embedded connection?
Thank you.
You need to launch Derby in "embedded server mode". If you are already using Derby in embedded mode, this can be enabled by providing the necessary files in your classpath, then specifying a handful of command line arguments when launching the application.
First make sure the following jars are in your application's runtime classpath.
derby.jar derbynet.jar
Then add the following command line options to the Java command used to launch your application. If the class files are missing, these options will have no effect.
-Dderby.drda.startNetworkServer=true
-Dderby.drda.portNumber=8011
I'm running Derby from within a servlet hosted by Tomcat, so I added these options to the catalina.bat file.
Start up your application and check the list of open network sockets.
netstat -an | find "8011"
You should now see Derby listening for connections on 8011. Its now possible to connect to the database using Derby's client driver (derbyclient.jar). The instructions at http://docs.oracle.com/javadb/10.3.3.0/adminguide/radminembeddedserverex.html cover this part pretty well.
It was hinted that running Derby in this mode may be discouraged. I don't believe that to be the case. Your application will continue to access the database using the embedded driver, while other software is now permitted access using the client driver.
The Embedded Server mode sounds like what you are asking for. It allows you to start a network server when you start the embedded database.
It sounds contradictory that you want to start derby in network server mode and get the embedded driver. Even if this might be possible, it is definitely discouraged. You should decide on whether you want to use Apache Derby in the network mode using the DRDA or as an embedded driver and stick to that decision.
Here you'll find a tutorial on how to use the network driver:
http://db.apache.org/derby/papers/DerbyTut/ns_intro.html
Some one correct me if i am wrong, Both will run on separte ports. So you can connect to the required one using the proper connectionName, right?
#pawelocue: Sorry, but this is wrong. Using the embedded server mode is perfectly alright and sometimes very useful. It is definitely not discouraged.
I am fairly new to H2 Database. As a part of a PoC, I am using H2 database(version : 1.4.187) for mocking the MS SQL Server DB. I have one application, say app1 which generates the data and save into H2. Another application, app2, needs to read from the H2 database and process the data it reads. I am trying to use Auto Server mode so that even if one of the application is down, other one is able to read/write to/from the database.
After reading multiple examples, i found how to build the h2 url and shown as below:
jdbc:h2:~/datafactory;MODE=MSSQLServer;AUTO_SERVER=TRUE;
Enabled the tcp and remote access as Below:
org.h2.tools.Server.createTcpServer("-tcpAllowOthers","-webAllowOthers").start()
With this, I am able to write to the database. Now, I want to read the data using the h2-web-console application. I am able to do that from my local machine. However, I am not able to understand how I can connect to this database remotely from another machine.
My plant is to run these two apps in an ubuntu machine and I can monitor the data using the web console from my machine. Is it not possible with this approach?
How can I solve this ?
Or do I need to use server mode and explicitly start the h2 server? Any help would be appreciated.
By default, remote connections are disabled for H2 database for protection. To enable remote access to the TCP server, you need to start the TCP server using the option -tcpAllowOthers or the other flags -webAllowOthers, -pgAllowOthers
.
To start both the Web Console server (the H2 Console tool) and the TCP server with remote connections enabled, you will have to use something like below
java -jar /path/to/h2.jar -web -webAllowOthers -tcp -tcpAllowOthers -browser
More information can be found in the docs here and console settings can be configured from here
Not entirely sure but looking at the documentation and other questions answered previously regarding the same topic the url should be something like this:
jdbc:h2:tcp://<host>:<port>/~/datafactory;MODE=MSSQLServer;AUTO_SERVER=TRUE;
It seems that the host may not be localhost and the database may not be in memory
Is there a need for the H2 web console?
You can use a different SQL tool using the TCP server you have already started. I use SQuirreL SQL Client (http://squirrel-sql.sourceforge.net/) to connect to different databases.
If you need a web interface you could use Adminer (https://www.adminer.org/) which can connect to different database vendors, including MS SQL, which happens to be mode you're running H2. There is an Adminer Debian package that should work for Ubuntu.
Sorry Maybe this is the second time I am Asking this question because of not getting any answers .
this is my Code
try{
File f = new File("Database.sql");
if(f.exists()){
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/"+f.getName(),"","");
}else{
f.createNewFile();
System.out.println("file created");
//also do the connection
}
}catch(Exception ex){
System.out.println(ex.getMessage());
}
and Here is the error :
Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
The purpose of this question is:
I am creating an Application that is for many users, the problem is they don't know anything about computer and I must make it as simple as possible.
So is there any way to connect with MYSQL like MS ACCESS via Directory Path ?
OR is there any other suggestion instead ?
Thanks .
AFAIK, you can't plug in a file name in the JDBC url for MySQL. MySQL needs to be running, and you need to connect to it via its TCP port. Something like:
jdbc:mysql://localhost:3306/yourDatabaseName
See http://dev.mysql.com/doc/refman/5.6/en/connector-j-reference-configuration-properties.html
Preliminary definitions
"Connect to a MySQL database file" is improper phrasing.
One actually connects to a MySQL database server, which allows access to a database. And so there must be a MySQL server to connect to, more about that below.
Your Database.sql file is a database dump file, that is to say, a dumb (plain text) file. You need a specialized process to extract data from it, after interpreting your SQL queries: a database server.
You might be assuming that one can connect to a file because you are used to working with MS Access files. I am not an expert neither in Java nor in MS Access, but it is my undestanding that accessing a MS Access "database" file from Java actually means connecting to some middleware server, such as this ODBC thingy from Microsoft.
The answer
There is no way to connect to a MySQL database server via a directory path. The only native ways are:
TCP
Local socket (Unix servers only)
Named pipes (Windows servers only)
Shared memory (Windows servers only)
There could be some third-party pieces of software around that provide other protocols, which I am not aware of, but they all reduce to the same problem: there must be a MySQL sever running somewhere.
On second thought there is actually one way to access MySQL data without an external MySQL server running: the embedded MySQL server C library. I never tried it myself, but it looks like a viable option for stand-alone applications. I do not believe, however, that it is a desirable solution if you plan to share the same MySQL data across several processes or computers.
The workarounds
Now I understand you are building a Java desktop application based on data that you have in the form of a SQL dump file, probably dumped from a MySQL server. If you want your users to be able to access this data from this Java application, I can see a few options:
Install a MySQL server on their computers and load this dump into it. Obvious as hell, but impractical, if I hear you well. Although I guess this installation could certainly be performed automatically by your Java application.
Install a MySQL server on a machine of your own, and make it accessible from your users' computers. Major drawback: it requires your users to be connected. You would also probably want to create a distinct database for each user.
Use an actually serverless database engine such as SQLite. This seems to be the best option for you. Its SQL syntax is virtually identical to MySQL for usual operations. There must be plenty of JDBC drivers for it. Again, I am not the best advisor in Java, but this one seems to be a serious candidate.
I made a page using struts. When running on localhost (glassfish) it is working fine.
However, when I copy WAR file to a remote server and run the application from there login form does not work. That means something is wrong when connecting on mysql database.
How could I solve this?
Code snippet:
Connection conn = null;
Statement stmt = null;
try{
InitialContext ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("jdbc/SB");
conn = ds.getConnection();
stmt = conn.createStatement();
ResultSet rs = null;
if(stmt.execute("SELECT * FROM users WHERE username='"+loginform.getUsername()+"' AND geslo=MD5('"+loginform.getPassword()+"') LIMIT 1;"))
{
rs = stmt.getResultSet();
}
rs.next();
Unfortunately I cannot read from the server console. What else should I do to see the exception (remote logging?)?
Thanks on replies.
UPDATE:
- Both locally and remotely GlassFish 3.0.1 is running.
- My working environment is NetBeans 6.9.1.
- Deploying locally is done simply in NetBeans. I just click on the project name and click publish. For remote deployment, admin gave me access to the FTP server where I just copy the WAR file.
- MySQL server is located somewhere else. The point is, it is accessible from the localhost web application but not from the same remote web application.
Assuming you have admin rights on the remote server, you need to
Verify that the datasource "jdbc/SB" exists. (Resources / JDBC / JDBC Resources)
Verify that the "poolname" in the datasoucre exists (Resources / JDBC/ Connection Pools)
Open the appropriate connection pool and click on the "ping" button.
If ping fails verify the connection params under the Additional Properties tab.
There could be a dozen other issues as well. You really need to get access to the server log so you see what the specific exception is. It's unlikely it's your code and more likely that the remote server is missing the driver dependency or isn't properly configured (the connection pool an datasource)
Some ideas are
- log to file instead of console
- Print log on your struts page
- maybe you don't have grants or firewall problem to access DB
I'd bet that you don't have authority to access the DB...
Try this as a test... install the MySQL client on another machine and then connect to the MySQL server from there.
If that fails then:
Say you log in with:
User: test
Password: password
Then open the Mysql client and enter:
use nameOfDatabase;
GRANT ALL ON nameofDatabase.* TO 'userName'#'ipaddressOfServerOrDNSName';
That should do it.
There are other ways to give access see http://dev.mysql.com/doc/refman/5.1/en/grant.html
Are you sure your database is setup correctly on your remote server? It sounds like maybe a firewall issue.
Unfortunately I cannot read from the server console.
If you have shell access to the remote server, the solution is simple:
Change the logging configs so that your webapp logs to a file instead of / as well as the console.
Login and read the file.
(Indeed, if you were using Tomcat, stuff written to System.out / System.err is written to a file anyway. Maybe this is true for Glassfish also.)
If you don't have shell access, you may have to modify your webapp to provide an extra page that can be used to download or view the log file from your web browser. If you have to resort to this, pay attention to the obvious security concerns.
This is my weg (wild eyed guess)....
Your local app server has the MySql driver installed. The JDBC resource is also defined correctly. The MySql server is probably running on your localhost. You have developed your app locally in NetBeans.
When you deploy an app from NetBeans onto a local server, the IDE makes sure that server has the DB driver installed. NetBeans cannot do that when you deploy to a remote host. If you use some other deployment method, driver installation is not automagic. It is an easy step to forget.
You can find out if the MySql drive is installed on your server by writing a web app that attempts to load or use one of the classes from the driver jar file. If the access generates an exception, you can probably get something that works by including the MySql drive in your webapp's WEB-INF/lib.
NetBeans also helps folks create and manage their JDBC resource definitions. If you create an app that targets a database on your localhost, the JDBC resource will have localhost as part of the URL. This will work great when the app is deployed onto the same server as the DB server. If the app is moved to another server, the JDBC resource needs to be changed to account for the possibility that the DB server is NOT on the localhost.
If you do not have admin access to the remote server, you will have trouble getting the resources defined. If your remote server was a GlassFish 3.1 domain/instance, you could leverage application scoped resources to get the resources defined for your app.
Regarding access to the log file.
If you are using asadmin to deploy onto the remote server, this command will help you access the server log if you are working with GlassFish v2.x: http://docs.sun.com/app/docs/doc/821-0179/display-log-records-1?l=en&a=view. You can also access the log via the admin console.
If you are using rcp to deploy your app, by copying it into the autodeploy directory of the remote server, you may want to see if you can copy the log onto your local machine.
For example... if your admin told you to deploy using an ftp command like this:
ftp> cd /a/b/glassfish/domains/domain1/autodeploy
ftp> put MyGreatWebapp.war
you may be able to get the log by doing something like this:
ftp> cd /a/b/glassfish/domains/domain1/logs
ftp> get server.log
Experiment....
I am having a problem with Java DB that I just don't know how to resolve. I am creating a DB and connecting to it using Java DB's native JDBC driver. If I relocate that database physically and try to connect to it using its new path, I consistently get XJ004 errors:
ERROR XJ004: Database 'blahblah' not found.
I am sure I am using the correct connection string. Is there any possibility the DB is somehow getting corrupted? Or is there some encoding of the DB path in the DB such that if you relocate a Java DB it gets confused?
I'm really at a loss here. :( Please help!
Jim
Have you verified that this error message isn't also used when there's no listener on the host machine ... and were you using JavaDB on your local machine before the relocation? Many database systems (and I'm not that familiar with JavaDB) ship set-up to only allow connections from localhost for security reasons. On PostgreSQL for instance, you have to allow TCP connections and bounce the daemon to obtain a remote connection.
Anyway ... since the problem started when you when remote, look for issues related to that first! (And if you can run your application on the remote machine, does that work?)
There must be a file named derby.log somewhere. Check the error there. If it is not detailed enough, try setting derby.stream.error.logSeverityLevel to a lower value. See the manual for more information.