How to determine database type for a given JDBC connection?

I need to handle resultsets returning stored procedures/functions for three databases (Oracle, sybase, MS-Server). The procedures/functions are generally the same but the call is a little different in Oracle.

statement.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
ResultSet rs = (ResultSet)statement.getObject(1);

JDBC doesn't provide a generic way to handle this, so I'll need to distinguish the different types of DBs in my code. I'm given the connection but don't know the best way to determine if the DB is oracle. I can use the driver name but would rather find a cleaner way.

Asked by: Dainton862 | Posted: 28-01-2022

Answer 1

I suspect you would want to use the DatabaseMetaData class. Most likely DatabaseMetaData.getDatabaseProductName would be sufficient, though you may also want to use the getDatabaseProductVersion method if you have code that depends on the particular version of the particular database you're working with.

Answered by: Ada725 | Posted: 01-03-2022

Answer 2

You can use org.apache.ddlutils, class Platformutils:

databaseName = new PlatformUtils().determineDatabaseType(dataSource)

Answered by: Lenny397 | Posted: 01-03-2022

Similar questions

java - How to determine an incoming connection is from local machine

I have a SocketServer accepting incoming connections. For security reasons I should only allow local connections (connections from the machine on which server is running). How can I determine if an incoming connection is from another machine? Is the following code safe for this? Socket socket = someServerSocket.accept(); String remoteAddress = socket .getInetAddress().getHostAddress(); if (!fromThis...

java - Is there a way to determine that a Connection is managed?

I have some legacy JDBC code that is used within an EJB, in this code a call to setAutocommit() is made (which is not allowed for managed transactions, for understandable reasons). I would like to skip this method call if the code is used within a managed transaction, but let the call remain if used in an un-managed context. Is there a standardised way to detect if a JDBC Connection object is "managed" or n...

java - How do I determine if client's internet connection is down?

When I do a it gives me when the server is down. However, I've found out that if my internet connection is down, the function also throws ...

sockets - How to determine if server closed connection (RST packet received) in Java?

I'm using Socket class for my TCP connection. But my current problem is to determine exactly disconnect reason. In both cases (if there's connection timeout or server closed connection) I receive SocketException with "Broken pipe" message. So how I can exactly determine disconnect reason? Thanks!

java - ftp connection to determine if file is there

I am trying to check if the file exist in the FTP. When testing with one user it seems to be fine. But with the scenario of multiple users it seems to throw the below exception : Exception in thread "main" Welcome message: 421. Below is the code which we use to check if file is there and we have closed all the connection but still it throws ...

java - Is there a way to determine how socket to the connection?

WOndering count of determine a way there is one of the ways to determine

java - Determine Network Connection Bandwidth (speed) wifi and mobile data

I want to get Network Connection Bandwidth in kbps or mbps. if the device is connected to wifi then it should returns the network bandwidth(speed) as well as mobile data. it will returns wifi capablity rate but i want exact data transfer rate. public String getLinkRate() { WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE); WifiInfo wi = wm.getConnectionInfo(); return ...

sqlite - SQLite3 in Java - How can I determine if my connection is valid and a table exists?

I'm making a game in Java where I store some data in a single SQLite table. Since a db connection isn't mandatory for the game, I want it playable if some error happens, but while hiding anything that would lead to a database query or update. Therefore I want to determine if the connection is valid and if my table is present. Here's my existing code for connecting to it:

sockets - java connection to jabber

What type of stream I need to use to write a connect request message over a tcp socket, to jabber. I'mwriting a string with xml format. It should worked with the default socket stream or a dataoutputstream but it doesn't.

java - Connection to Oracle without a username or password

Oracle has this concept of allowing database users to be identified by the operating system user who is running the program that is connecting to Oracle. See here. This allows you to do, as that user on a unix machine for example, a command such as: sqlplus / I am attempting to write a Java...

Check LDAP connection (Java)

I'm using javax naming to connect to an LDAP database. Is there a good way to check if a connection is still valid? I'm looking for something really efficient here because it may need to be done often. After some web searching all I have found is a suggestion to do a quick search, is there any more lightweight way? /mac

spring - How can I set up an LDAP connection pool in a Java EE Container?

I need to put an LDAP contextSource into my Java EE container's JNDI tree so it can be used by applications inside the container. I'm using Spring-LDAP to perform queries against ORACLE OVD. For development, I simply set up the contextSource in the Spring xml configuration file. For production, however, I need to be able to use a JNDI lookup to grab the connection/context from the container (as suggested here:

Java connection to / between Microsoft Windows Office Suite?

I read elsewhere (a response by "hazzen" here) that .NET has "a binding for the entire Office suite outlined here that allows you to write COM-based methods that you can call from Office. It is intended for automation, but you can write any managed code you want and have Excel call into it." I'...

java - Android HTTP Connection

Can anybody tell my why this doesn't work in the Android emulator? From the browser I have access and the server is internal. All I can think of is that I'm missing some configuration on my app so it can access the network layer. try { InetAddress server = Inet4Address.getByName("thehost"); //Doesn't work either //or InetAddress server2 = Inet4Address.getByAddress(new String("").getB...

java - How to create an SSL connection using the Smack XMPP library?

I'm building a small program that acts as an XMPP client and I am using the Smack library. Now, the server I am connecting to requires SSL (in Pidgin I have to check "Force old (port 5223) SSL"). I'm having trouble getting Smack to connect to this server. Is it possible?

java - HTTP Connection Pooling in GlassFish

In my webservice, I need to place some HTTP calls. Is it possible to do some connection pooling, like I do JDBC connection pooling? In the Admin Console of GlassFish I see the configuration items Connector Connection Pool and Connector Resources. Can I use these?

java - Proxool maximum connection count

I'm using proxool java connection pool (version 0.9.1). Everything works fine till I reach the maximum connection count. If the maximum connection count is reached proxool immediately throws an SQLExcepion: java.sql.SQLException: Couldn't get connection because we are at maximum connection count (n/n) and there are none av...

java - How can multiple webapps in the same tomcat instance share database connection pool?

Having in mind that each webapp has its own separate database (but all databases are in the same database server). The scenario is that I have a multi-tenant saas application - I deploy the same application for each customer. Each customer works on a database that is called db_cid, where cid is the customer id, i.e. a a unique customer identifier. Working with hibernate.

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)