This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Get Programming with Java (MEAP only) and have Peggy Fisher on-line!
See this thread for details.
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!

Mike London

Ranch Hand
+ Follow
since Jul 12, 2002
Cows and Likes
Cows
Total received
13
In last 30 days
0
Total given
0
Likes
Total received
35
Received in last 30 days
0
Total given
1
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Mike London

Claude Moore wrote:Which version of the driver are you using ? Maybe, it's an outdated one, so that autoregistering of the driver doesn't take place. Are you sure you are loading the correct driver class ?
I tried with:



it worked, bu I also got a warning:

"Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary."



I'm using the new driver.

I'll try creating a separate test class shortly and will report back.

Thanks,

-- mike

Tim Holloway wrote:

Claude Moore wrote:From JDBC 4.0, if I'm not wrong, JDBC drivers are automatically registered when present in classpath;



You are not wrong. There's a special line in the Manifest file in the JDBC driver jar that is detected to register the driver class. Class.forName is extremely obsolete at this point. It's harmless, but unnecessary.

The registerDriver method is supposed to be called by the driver itself when this condition exists, not part of user-written code.



Hi Tim,

I am loading the JDBC driver in the Maven POM.XML.

Yet, without the explicit Class.forName.... I get a unregistered driver exception.

-- mike

Claude Moore wrote:I tried this:



And it worked without problems. MySql  Server release is 5.6



That's cool. Thanks.

In any case, I can't get the JDBC logic to work without the Class.forName to explicitly load the driver.

Even with code like below, if I run it, I'll get a "no suitable driver found"

Thanks,

-- mike

Claude Moore wrote:It may silly to ask you this: have you tried using



It should do treat special characters for you.




That's a good idea. I went ahead and instead created a less-powerful user, with no root access, and only SQL access for the database.

Thanks for your reply.

-- mike
Hello,

I have a JDBC MySQL password with lots of special characters, including "%". Now, MySQL works fine using this password from Navicat, WordPress, Drupal, etc., but when I try to connect via a JDBC connection string using this password, I get:

Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: URLDecoder: Incomplete trailing escape (%) pattern

What I've tried:

1. I tried escaping the password in the JDBC connection string like this

final String DATABASE_URL =
               "jdbc:mysql://localhost:3306/lookup?useSSL=false&" +
                       "serverTimezone=America/New_York&user=root&password=\"password_here\"&autoReconnect=true";

2. Different JDBC driver.

3. URLEncoding the password itself. In this case, I get the message that the connection tried three times and gave up -- meaning my URL Encoding wasn't accepted.

---

Would appreciate suggestions how to get around this password issue. I can't change the password itself since there are so many CMS/other clients that actively use the MySQL database.

Thanks very much in advance,

-- mike

Daniel Demesmaecker wrote:Are they on the same host?
This tutorial might help you



Hi Daniel,

Yes, both containers are running on the same Mac.

Thanks,

- mike
1 week ago
I have two docker containers running fine.

1. a Java REST app.
2. MySQL 8

I can access both externally with no problem.

But, when I try to make the Java REST app talk to the MySQL container, I just get a message that after three tries, there was no connection made.

I've tried various ports, host names, docker-created network name, but JDBC just won't connect.

Here's my docker-compose file:



Some of the failed JDBC connections include these below (miketest_default is the docker-created network name):



----

Any suggestions welcome how to get the Java service in a docker container to be able to reach the MySQL container.

Thanks,

- mike

1 week ago
Hello,

I have a simple testing authentication set up, but it's allowing any user role to access the "hello" class. I'm using an online example as my learning point and the author didn't get it working correctly either...

Here's the override for configure:

 

But the URL using the user = demo (and admin role), still gets to the page: localhost:8088/rest/hello

I've tried various combinations of asterisks with the .antMatchers() method above, but I can't get the security to respect only the user role.

To get the password stuff to work at all, I also had to refactor the online example to use a Password encoder.

Thanks in advance,

-- mike

2 weeks ago

Ron McLeod wrote:Is the regex [^A-Za-z|[0-9]*] correct? I've never seen a vertical-bar/pipe character in a character class (I'm not a regex expert though).  Why not just define a class simply composed of the three ranges A-Z, a-z, and 0-9 ?

Your code with the expression [^A-Za-z0-9]:
running on Windows 10 with Java 8 produces this result:



Nice! Thank you.

Yes, your simplified (corrected) RegEx is better. I was confused since it worked fine for me on Java 8 on Mac, but not Java 8 on Windows 7. Same JAR, same Java version. Java 11 worked correctly on both platforms.

Your Regex version fixes it universally. I just wasn't thinking about the Regex being the culprit.

Thanks Ron.

- mike
4 weeks ago
Problem seems to be the Java 8 version vs. Java 11 Open JDK.

Java 11 gives expected Regex result. Java 8 (even version 191) does not (per the posting above).

Thanks,

- mike
4 weeks ago
Hello,

I have a simple method in a SparkJava GET method that is supposed to extract only uppercase and numbers from a passed string.

So, "123 SOMEWHERE street" would return: "123 SOMEWHERE".

The code below works as expected (from example above) on the Mac both in Terminal and in Firefox.

Yet, that same code (the JAR file) running on Windows server, does not. The Windows result is "SOMEWHERE". For some reason, the "123" is omitted even though the received URL looks the same as on the Mac. (It's the exact same JAR file).

Here's the code:




WINDOWS SERVER TEST: http://localhost:4567/extractUpperCase/123%20SOMEWHERE%20STREET
RETURNS: SOMEWHERE STREET

----

Mac TEST: http://localhost:4567/extractUpperCase/123%20SOMEWHERE%20STREET
RETURNS: 123 SOMEWHERE STREET

Any ideas why Windows, running the exact same JAR file as the Mac , omits the 123 in the test string?

Windows Server running (java -version): JDK 1.8.0_172
Mac running ($java -version): openJDK 11.

I don't understand how the run time version difference could/should make any difference.

(Baffled.)

Thanks in advance,

- mike
4 weeks ago

Tim Holloway wrote:

Because, as I said, starting an entire connection pool is even more work than starting a connection from scratch. And if we assume that the point of a micro-service is to be lightweight, that would mean that it's better to be provided with a ready-made source of connections than have to repeatedly create and destroy the whole shebang.



Well, it takes about 20ms to do a basic query with 1,000 records with the current code and I don't see any examples of how to create connections up front but I like your idea at least in principle.

Thanks Tim,

- mike
1 month ago

Tim Holloway wrote:I'm not sure what you're asking. But a Connection Pool is a waste of resources for a single-threaded app. Pools are more suited for multi-threaded and/or multi-user apps where you don't want a given task to hang on to Connections, and you don't want to create a Connection from the ground up every time a task needs one.

My objections to the sample code you pointed to initially were based on the sloppiness of the design and code, not its utility. Both it and the H2 sample are merely trying to show how to set up a Connection Pool and use it. They're illustrative, not real-world uses. In the real world there'd be a lot more going on. And yes, normally you create the pool(s) at or near start-up and would only destroy them at shutdown. Because creating a Connection Pool is at least as much overhead as creating a raw connection, and depending on whether it builds a minimum pool in advance, potentially much more. Pools only pay for themselves when you're asking for Connections multiple times and releasing them in between.



Use-case:
In a micro-service, for example, you have local micro-service method with lifetimes the same as any other method (variables go away at the end of the method). So, on those methods, my question really: "shouldn't you really, instead, create the connection pool instance outside a micro-service (local) method and THEN create connections from that external (persistent) instance from inside the local methods so the connection pool persists?"

Sorry for my lack of clarity. I hope this use-case helps clarify my initial question.

Thanks,

-- mike
1 month ago

Tim Holloway wrote:

Mike London wrote:
Aside from the Class.forName, the code is quite similar to that on the H2 site itself:

http://www.h2database.com/javadoc/org/h2/jdbcx/JdbcConnectionPool.html



Well, yes. It's not exactly rocket science there. The difference is that on the H2 site, they're setting up, running an example, and shutting down. They didn't write code that leaks previous instances of a pool only to create a new pool instance every time you ask for it.



I implemented the connection pool logic in a static method so it's more or less the same.

You never answered my actual question, that I see:  "Does that strategy make sense for a "Connection Pool"? I mean, shouldn't a connection pool be a persistent object future connections call where the Pool manages connections? Or do you look at the Pool from the point of view that in a multi-user environment, multiple users will run the same method and at that point the Connection Pool comes into play?"

Thanks,
1 month ago

Tim Holloway wrote:Your first warning should have been this line:

It's probably been 15 years now since that was necessary. This person obviously isn't as clever as he thinks he is.

Secondly, it looks like the getConnectionPool method was attempting to return a singleton object. It obviously fails to do so. And to add insult to injury, it's not even in its own factory class - it's lumped in with the application code.

Moral of story: Not everything you find on the Internet is gold. No matter how high up in a Google search it might appear.



Aside from the Class.forName, the code is quite similar to that on the H2 site itself:

http://www.h2database.com/javadoc/org/h2/jdbcx/JdbcConnectionPool.html
1 month ago