What exactly is a data source? What difference does it make? - java

When I tried to establish a connection with the Oracle Database, I had to write
Connection CON = DriverManager.getConnection("jdbc:odbc:Dan", "system", "noodles");
Here, Dan is the data source name, isn't it? What if I created a table called cBC when the data source was Dan and what if I rename the data source and enter further rows into the table? What difference does it make?

Dan is the name of a ODBC connection configured on your machine. The name itself does not matter, as long as the database that it is configured to connect to is the same, it doesn't matter if you call it Dan, MyDatabase or foobar.
Note that this specific way to access a database that is configured externally is not a thing that JDBC does in general, it's a specific behaviour of the JDBC-ODBC bridge (which lets you access ODBC connections via JDBC).
Other JDBC drivers (such as MySQL) use a different syntax where the necessary configuration for accessing the database is encoded in the URL: jdbc:mysql://myDbServer/myDbName.
Note also that the JDBC-ODBC bridge was never intended for production-quality DB connections (it will even be removed in Java 8!). It is just a quick way to use an existing setup.
For Oracle DB connections you should instead use the appropriate Type 4 driver from Oracle. Those drivers use an URL in the form jdbc:oracle:thin:#//<host>:<port>/ServiceName (generally speaking, the part after jdbc: identifies the JDBC driver to be used).

Related

What does JDBC Connection really mean? Wanted to know internals of the same

Is "opening the JDBC Connection" related to open a Socket with the DataBase software?
Databases provides connections to client eg. a GUI Administrative tool to manage related database like SQLyog for MySQL and so in JDBC connection we are requesting a connection from our application as client through JDBC API.
These connection as TCP connections so a client is required to know the port no and ip of the database server to talk to it.
what you must be wondering at is what is JDBC? if so then
JDBC is an interface provided by java that manages database operations and most awesome thing about this interface you will be writing exactly same code whether you are querying a MySQL or a PostgreSQL database servers.
It depends on what type of JDBC driver you are using. According to
http://en.wikipedia.org/wiki/Java_Database_Connectivity
you may have four types of JDBC drivers which usually (but not necessarily always) rely on TCP/Socket connections.
A JDBC connection can actually be anything - it's a generic way to talk to databases.
In most cases it is a TCP/IP connection but that is not always the case. For example you can embed a Derby Databse within your Java application and talk directly to that without even leaving your process.
If you find out what your database and driver is that should allow you to answer your question.
Here is what you require.This should answer all your queries. Cheers.

Can I change the 'appName' on an already open JTDS connection?

I'm looking for a way to pass web-application transaction information on to the underlying database process. In my Java code I might have a transactional method ReservationService#search(), which runs one or several SQLs. On the DBMS I just see a SPID along with some locks. I'm looking for a way to add a tag "ReservationService#search" to the database process.
jTDS / Sybase ASE have an appName which can be passed in as a connection property. As we're using a connection pool, existing connections are re-used, but to my knowledge the appName is only read on establishing a new connection.
How can I re-set the appName on an already existing connection (without closing/opening)? Or, if that simply is impossible, are there any other ideas to get transactional context information from Java to the DBMS?
Tomcat Webapplication (Java 6)
C3P0 Connection Pool (only supports JDBC 3)
jTDS connecting to Sybase ASE 15
Thanks
Simon
Unfortunately not, it seems that you can only specify that in the URL parameters when you open up the connection but can't be altered afterwords.
You can aways pass in a SessionID of some kind from your Java/Tomecat to all your Sybase queries. For me, this was easy as I use stored procedures for all communications between my Java application and the SQL server. I based my SessionID in Java on the J2EE session.

Can we connect to SQL Server 2005 database from code with out having the installation of client and server

I need to retrieve data from the database. I have the database name, username, password, servername, but I don't have the software installed in my machine. Can I connect to the db and retrieve the data from the db from database from Java code?
you need to have database drivers atleast like ODBC or JDBC drivers on your system where application runs to access the DB from other machine.
Yes. The jdbc driver .jar can be used stand alone. It logs a warning about a missing .dll, but that can be ignored unless you need to use windows auth.
From Java, it's easy as long as there is some kind of driver installed (for example, ODBC.) Try Googling connecting to db using odbc.

Accessing Access over JDBC (using ODBC?)

I'm looking for a way to open an Access MDB file inside a Java App (using JDBC).
A quick Google Search suggests that I need the JDBC-ODBC Bridge for this...
Does this mean that I need to configure every system I want to run my app on to provide a ODBC DSN for the MDB I want to open?
And one more question (since I've never used ODBC before): will the communication happen over some sort of a socket (in a client/server-style), or through method/function calls (like with an embeded Derby db)?
1) You won't need to configure every system with a SYSTEM or USER ODBC DSN to access the MDB you want. You can still provide all the information you need in your JDBC URL:
jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=c:/yourdb.mdb
But keep in mind that the system will need to have the driver you are using installed.
2) The communication will happen the way your ODBC driver communicates. If it opens a socket to the server (the way an Oracle ODBC connection takes place) it will open a socket. If it uses library function calls, it will communicate through library function calls.
JDBC to ODBC communication uses JNI to communicate.

What's the right way in Java to connect to a Microsoft Access 2007 database?

I'm trying to create a simple connection using the jdbc-odbc bridge:
public static Connection getConnection() {
Connection con =null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String conStr = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=" +
"c:\\myfolder\\accesdbfile.accdb";
con = DriverManager.getConnection(conStr);
} catch(Exception e) {
e.printStackTrace();}
return con;
}
But then I get this exception:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]General error Unable to open registry key Temporary (volatile) Ace DSN for process 0xa4 Thread 0xec0 DBC 0x2f8574c Jet'.
Any ideas?
Update 24/Mar/2009: Now it's working. Created a User Data Source, and for some reason the exception went away.
As a general question, What would be the best way to handle database connections in Java?
In general, the best way to work with an RDBMS in Java is by using a JDBC driver that is designed to connect directly to the database. Using the JDBC-ODBC bridge tends to be sloww.
If you are trying to do basic read/write manipulations with an Access database, I would also recommend taking a look at the Jackcess library.
To answer your general question I would say the best way to handle database connections in Java is to avoid the JDBC-ODBC bridge. Its OK for testing or learning about JDBC but not for real production use. Also, if you have a data source that does not have its own JDBC driver but it has an ODBC driver then you may not have a choice.
The main reason why I suggest that you stay away from it though is that it makes it difficult to deploy your application. You have to set up the Data Source on the machine that you are running your application from. If you have access to the machine no problem, but suppose you are sending the application off to the client? The pure Java JDBC driver works better for this because it is included as part of your application so once your application is installed it is ready to connect to the data source.
Of course depending on your requirements there are two other types of drivers but thats another discussion.
Go to control panel -- > Administrative tool --> ODBC Data Source Administrator
Add database --> Select "Microsoft Driver(*.mdb, *.accdb)"
Dobule click on new database --> Under "Database" click on "select" --> Select your *.accdb file which you hv created as MS access database.
Say OK and go to your java code
Use:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:filename");
It will surely resolve all your problem.

Categories