Java get data source

How to get a DataSource?

A implementation that supports connection pooling produces Connection objects that participate in connection pooling, that is, connections that can be recycled. A implementation that supports distributed transactions produces Connection objects that can be used in a distributed transaction, that is, a transaction that accesses two or more DBMS servers.

How to get a DataSource?

I’m not certain how to get a DataSource object. I was able to use the DriverManager method to obtain a connection to a SQL database running on localhost, but every time I try to use the DataSource method to do so I wind up getting exceptions (mostly for naming).

  1. Is it possible to get a DataSource object for local hosted databases?
  2. Does the DataSource class need to be published, or is it like DriverManager where you just get a connection with no new class creation?
  3. Could you show an example?

A DataSource allows getting a JDBC connection mostly from a pool of connections. A DataSource object represents a particular DBMS or some other data source, such as a file. If a company uses more than one data source, it will deploy a separate DataSource object for each of them. The DataSource interface is implemented by a driver vendor. You externalize DB connection properties file and fetch the object using JNDI. Using a Datasource you need to know only the JNDI name. The Application server cares about the details.

It can be implemented in three different ways:

  1. A basic DataSource implementation produces standard Connection objects that are not pooled or used in a distributed transaction.
  2. A DataSource implementation that supports connection poolingproduces Connection objects that participate in connection pooling, that is, connections that can be recycled.
  3. A DataSource implementation that supports distributed transactions produces Connection objects that can be used in a distributed transaction, that is, a transaction that accesses two or more DBMS servers.

Like, in Spring, you can configure the datasource in an XML file and then (1) either inject it into your bean, (2) get it from ApplicationContext .

DataSource ds = (DataSource) ApplicationContextProvider. getApplicationContext().getBean("myDataSource"); Connection c = ds.getConnection(); 

Suggested Reading:

  1. connecting with datasource objects
  2. why do we use a datasource instead of a drivermanager?
  3. Data access with JDBC
  4. How to retrieve DB connection using DataSource without JNDI?
  5. Best way to manage DB connections without JNDI

Java — How to get a test resource file?, 0. This solution need not lib’s. First create a util class to access the resource files. public class TestUtil (Class classObj, String resourceName) …

Читайте также:  Read json value in java

Get the hostname from a DataSource class

Is there a way to get the hostname from the DataSource class in java? I mean, I have a DataSource object (DS), which is annotated to get the jboss datasource. Anyway, I want to get the hostname used in that DS.

Debugging, i can see it this way: I get the Connection from DS, then I get the DataSourceMetaData and inside of that the is something called Protocol Connection which have the hostname, but I don’t know how to get it.

Anyone here knows how? or another way to get the hostname? Thanks in advance. Kind regards, RDAM

Once you have the DatabaseMetaData, just call the getURL() method which should contain the hostname like so:

dataSource.getConnection().getMetaData().getURL(); 

Java — How to get JRE/JDK with matching source?, If you’re using eclipse, you can bind the JDK to its source if it is not done automatically. This is done in Window > Preferences > Java > Installed …

How to get an Exception source object

I was working on an assigment in Java, and I wondered if it’s possible to know which object throwed an exception.

I know that if you make your custom exceptions, you can modify the constructor and have a reference to the object:

public class MyEx extends Throwable < private MyObject object; public MyEx(MyObject o)< super(); >public MyObject getSource() < return object; >> 

but I don’t know if there exists another way of catching who throwed the exception. Do you know any other way?

If you just want to see from which part of the code an exception is thrown, you have the simple stack trace. You get this by calling printStackTrace() on an exception instance.

This does not however give you the exact object instance that threw the exception. For this you have to implement a custom Exception like you indicated. Note that in some contexts there is no such thing as an instance, like e.g. an exception thrown from a static method.

Oracle’s exception(s) tutorial

Java — Get source of data in clipboard, Looking at the docs, there’s something called a DataFlavor, that seems to have a very rich set of properties such as mimetype. It’s possible that …

How to get source code from .class file( class file version is 1.2)

I used JD GUI de-compiler but it is giving above error.

is there any way i can get source code

Try this at the command line. It will also show the JDK version used to compile the class, in the beginning , like: major version: 52

Читайте также:  What is sqlite database in java

Try javadecompilers.com, I’ve successfully decompiled some class files there.

Java — Get the hostname from a DataSource class, 50. Once you have the DatabaseMetaData, just call the getURL () method which should contain the hostname like so: dataSource.getConnection …

Источник

Java get data source

A DataSource object has properties that can be modified when necessary. For example, if the data source is moved to a different server, the property for the server can be changed. The benefit is that because the data source’s properties can be changed, any code accessing that data source does not need to be changed.

A driver that is accessed via a DataSource object does not register itself with the DriverManager . Rather, a DataSource object is retrieved through a lookup operation and then used to create a Connection object. With a basic implementation, the connection obtained through a DataSource object is identical to a connection obtained through the DriverManager facility.

An implementation of DataSource must include a public no-arg constructor.

Method Summary

Gets the maximum time in seconds that this data source can wait while attempting to connect to a database.

Sets the maximum time in seconds that this data source will wait while attempting to connect to a database.

Methods declared in interface javax.sql.CommonDataSource

Methods declared in interface java.sql.Wrapper

Method Detail

getConnection

Connection getConnection() throws SQLException

getConnection

Connection getConnection​(String username, String password) throws SQLException

getLogWriter

PrintWriter getLogWriter() throws SQLException

Retrieves the log writer for this DataSource object. The log writer is a character output stream to which all logging and tracing messages for this data source will be printed. This includes messages printed by the methods of this object, messages printed by methods of other objects manufactured by this object, and so on. Messages printed to a data source specific log writer are not printed to the log writer associated with the java.sql.DriverManager class. When a DataSource object is created, the log writer is initially null; in other words, the default is for logging to be disabled.

setLogWriter

Sets the log writer for this DataSource object to the given java.io.PrintWriter object. The log writer is a character output stream to which all logging and tracing messages for this data source will be printed. This includes messages printed by the methods of this object, messages printed by methods of other objects manufactured by this object, and so on. Messages printed to a data source- specific log writer are not printed to the log writer associated with the java.sql.DriverManager class. When a DataSource object is created the log writer is initially null; in other words, the default is for logging to be disabled.

setLoginTimeout

Sets the maximum time in seconds that this data source will wait while attempting to connect to a database. A value of zero specifies that the timeout is the default system timeout if there is one; otherwise, it specifies that there is no timeout. When a DataSource object is created, the login timeout is initially zero.

Читайте также:  Python нормальная случайная величина

getLoginTimeout

Gets the maximum time in seconds that this data source can wait while attempting to connect to a database. A value of zero means that the timeout is the default system timeout if there is one; otherwise, it means that there is no timeout. When a DataSource object is created, the login timeout is initially zero.

createConnectionBuilder

default ConnectionBuilder createConnectionBuilder() throws SQLException

Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.

Источник

Interface DataSource

A factory for connections to the physical data source that this DataSource object represents. An alternative to the DriverManager facility, a DataSource object is the preferred means of getting a connection. An object that implements the DataSource interface will typically be registered with a naming service based on the Java Naming and Directory (JNDI) API.

  1. Basic implementation — produces a standard Connection object
  2. Connection pooling implementation — produces a Connection object that will automatically participate in connection pooling. This implementation works with a middle-tier connection pooling manager.
  3. Distributed transaction implementation — produces a Connection object that may be used for distributed transactions and almost always participates in connection pooling. This implementation works with a middle-tier transaction manager and almost always with a connection pooling manager.

A DataSource object has properties that can be modified when necessary. For example, if the data source is moved to a different server, the property for the server can be changed. The benefit is that because the data source’s properties can be changed, any code accessing that data source does not need to be changed.

A driver that is accessed via a DataSource object does not register itself with the DriverManager . Rather, a DataSource object is retrieved through a lookup operation and then used to create a Connection object. With a basic implementation, the connection obtained through a DataSource object is identical to a connection obtained through the DriverManager facility.

An implementation of DataSource must include a public no-arg constructor.

Источник

Оцените статью