How can I configure persistence.xml for SQL server - java

How can I configure persistence.xml file to connect to my SQL Server. Some information is connected as: Host, DatabaseName, UserName, Password, Port, ...

Setting up a connection pool to MySQL database using a configuration .xml file can be pretty troublesome to someone who has just started like you.
In my opinion, you should take a look at this short guide. You simply need to start Glassfish server, open the browser and surf to http://yourdomain.com:4848 to access the Admin panel. After that, just follow the instruction in that article and to create the JDBC Connection pool and JDBC Resource. At this point, you only need to open the persistence.xml file with your IDE (NetBeans, etc.) and set the Data Source property with name of your JDBC Resource and you're done.
One thing to note is that you must download the latest MySQL Connector/J and copy the file
mysql-connector-java-<version>-bin.jar
into the folder
<GlassFish-install-folder>\glassfish\domains\domain1\lib\ext
Otherwise, you will run into the expcetion Class name is wrong or classpath is not set for : com.mysql.jdbc.jdbc2.optional.MysqlDataSource if you try to ping the database after creating the JDBC Connection pool.

First of all donwload jdbc driver for sqlserver and put that in glassfish lib directory and boot that.
Using Glassfish admin console:
define a jdbc connection pool and specify Host, DatabaseName,
UserName, Password, Port, ...
define a jdbc resource and select connection pool name that you have specified in step 1
then grab the name of jdbc resource and put it in your persistence.xml file : <jta-data-source>YOUR-JDBC-RESOURCE-NAME</jta-data-source>
and now you can connect to data base.
If you are using Netbeans, it has some nice wizards to produce connection pool and jdbc resource in glassfish.
Since you are using JEE6 this is another way using annotation:
import javax.annotation.sql.DataSourceDefinition;
#DataSourceDefinition(name = "java:app/env/myDatasource",
minPoolSize = 0,
initialPoolSize = 0,
className = "your.driver.class",
serverName="localhost",
user = "admin",
password = "admin",
databaseName = "test"
)
public class DbConfiguration {
}
For more info take a look at here.

Related

Glassfish password alias

I need to encapsulate the database passwords inside the glassfish JDBCConnectionPool.
I found in glassfish documentation how to encapsulate user admin password.
Would anyone have an example of the encapsulation of database passwords?
browse http://localhost:4848
Click --> domains --> Password Aliases --> New
Add your connection pool password in password field and Click OK.
4.Create a connection pool.When creating connection pool fill the password filed
as below.
Example :
${ALIAS=[your_password_alias_name]}
${ALIAS=test_pwd_alias}
Start you GF server and go to the admin console (usually localhost:4848) :
Then go to Resources > JDBC > JDBC Connection pools.
Select your connection pool
Go to the Additional Properties tab
Here you will configure all your database connection info : username, database name, host (or database URL, depends on your DBMS), and password

NetBeans with Connector/J

Well I'm working on a GNU/Linux machine and I'm still learning how to connect to my database
I've got the Connector/J File downloaded and edited my CLASSPATH, here's the reusult of echoing the path
/home/user/Connector-J/mysql-connector-java-5.1.30/mysql-connector-java-ver-bin.jar:
I also have my SQL File.. let's say: sqlfile.sql
Threw the documentation and while searching I found how I should connect to the Connector.. My question is: where should I put my SQL file?
Also, Here's a piece of code I found that is used to connect, is it right?
String userName = "root";
String password = "password";
String url = "jdbc:mysql://localhost/somefile";
Class.forName ("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection (url, userName, password);
System.out.println ("Database connection established");
To build an application that connects to a database, you will need:
A database engine (e.g. MySQL, SQL Server, Oracle, etc)
A JDBC driver library (e.g. Connector/J for MySQL, jTDS for SQL Server, ojdbc for Oracle, etc)
Java application itself.
Your code is only creating connection. Even that, the url looks incorrect. The "somefile" should be replaced with the name of database schema you want to connect to.
To interact with database, creating connection is still far from done.
To read, insert, update, delete, etc records from/to database, you need to create statement object by passing your SQL query.
The SQL queries should not be put in a file, but written in your code instead.
Complete JDBC tutorial is here: http://docs.oracle.com/javase/tutorial/jdbc/

GlassFish Server does not connect to SQL database (error w.r.t. SQL driver)

I am using a brand new developing pc and need to test a personal application that runs on a local GlassFish server 3.1.2 and should connect with a local SQL database called 'funkOneDB' (my IDE is NetBeans 7.2.1). But I can't get the GlassFish server to connect with the database, and the problem seems to be related to the (place of the) SQL driver in the GlassFish Server's directories (more problem specifics in a few lines).
I am fairly certain I correctly set up the related JDBC Resource and Connection Pool on the GlassFish Server (as I mimic a set-up already existing and working properly on another developing pc).
The Resource specifics are:
jndi name: jdbc/FunkResource
pool name: FunkPool
The (most important) Pool specifics are:
pool name: FunkPool
resource type: javax.sql.Datasource
datasource classname: com.mysql.jdbc.jdbc2.optional.MysqlDataSource
additional properties correspond to the specifics in the XML GlassFish-resources of the application (username, password, url, etc.; no problems there)
I first placed the necessary SQL driver in the GlassFish Server's directories, i.e. the file mysql-connector-java-5.1.18-bin.jar at ..\GlassFish3\GlassFish\domains\domain1\lib\ext.
Yet, when I perform a ping test from the JDBC Pool 'FunkPool' at the GlassFish server, I get the following error:
Ping Connection Pool failed for FunkPool. WEB9031: WebappClassLoader unable to load resource [com.mysql.jdbc.SQLError], because it has not yet been started, or was already stopped Please check the server.log for more details.
In the server.log I only find the following extra logging exception and failure info:
(i) Exception while creating an unpooled [test] connection for pool [ FunkPool ], WEB9031: WebappClassLoader unable to load resource [com.mysql.jdbc.SQLError], because it has not yet been started, or was already stopped
(ii) RestResponse.getResponse() gives FAILURE. endpoint = 'http://localhost:4848/management/domain/resources/ping-connection-pool.json'; attrs = '{id=FunkPool}'
Note however, that when I ping the database funkOneDB from my IDE NetBeans via jdbc:mysql://localhost:33066/funkOneDB, it's succesful. As already mentioned, the credentials and other data I use for this IDE-based ping are the same data I use in the JDBC Connection Pool.
I searched for the problem also on stackoverflow for some. And I did find some people talking about it, like
Glassfisch MySQL ping ERROR (no answer by anybody), or
Struggling to create MySQL Connection Pool on Glassfish (tried that solution, i.e. putting the SQL driver one level up in ..\GlassFish3\GlassFish\domains\domain1\lib\, but this creates other errors, even after restarting the Glassfish server), or
GlassFish not loading connector
(even tried this solution, no succes).
Can somebody help me solve this problem? Many thanks in advance!
With kind regards,
Heinz
Place the mysql driver in the lib folder of your project. Then do a clean-and-build. It's also helpful to have netbeans communicate directly with your database. This will allow you to view the database structure and the contents of your database right from your IDE. For help integrating MySQL with netbeans, look here: netbeans.org/kb/docs/ide/mysql.html
My friend, i had this same exception:
RestResponse.getResponse() gives FAILURE. endpoint = 'http://localhost:4848/management/domain/resources/ping-connection-pool.json'; attrs = '{id=FunkPool}'
The cause of my error was that, i put wrong credentials. Check your credentials in your client DB App (SQL Developer, for example).
I had the same problem with SQL server and Netbeans. To resolve it, i put the sqljdbc.jar in the java direcory "Java\jdk1.8.0_121\lib directory" and it works :)
I've just spebnt 10 hours on this bug.

JDBC Connection URL For Embedded Derby in a Webapp

I have a derby database that is deployed along with my webapp to WEB-INF/classes/myDb
What should my jdbc.connection url be to connect so that I can write to the database?
I am trying
jdbc:derby:myDb;
and it can not find the database. I need to be able to modify the database. If i put classpath:myDb, it finds it, but it is unfortunately read only per the derby docs.
i solved it by setting my jdbc connection url at runtime and using:
StringBuilder derbyUrl = new StringBuilder("jdbc:derby:");
derbyUrl.append(servletContext.getRealPath("/"));
derbyUrl.append("/WEB-INF/classes/myDb;");
dataSource.setUrl(derbyUrl.toString());

Database connection encryption and integrity with ColdFusion and Oracle thin client

As ColdFusion datasource we are using the Oracle thin client to connect with the database. So, basically we are using a JDBC URL such as jdbc:oracle:thin:#... and as Driver Class oracle.jdbc.OracleDriver
This works successfully however we would like to set encryption and integrity parameters as well. In Java this is done similarly by setting a Properties object prior to getting a connection as follows:
Properties prop = new Properties();
prop.put("oracle.net.encryption_client", "REQUIRED");
prop.put("oracle.net.encryption_types_client", "( DES40 )");
prop.put("oracle.net.crypto_checksum_client", "REQUESTED");
prop.put("oracle.net.crypto_checksum_types_client", "( MD5 )");
...
OracleDataSource ods = new OracleDataSource();
ods.setProperties(prop);
ods.setURL("jdbc:oracle:thin:#localhost:1521:main");
Connection conn = ods.getConnection();
...
Is there a way that I can pass these parameters to the ColdFusion datasource. Ideally, I would love to do this centrally in such way that a change to all the cfquery or cfstoredproc is not needed.
I also know that in application servers such as Oracle AS there is an option when creating a datasource which says "Add Properties". In there you can add such properties. So, I was thinking of maybe creating a JNDI DS in the app. server and then magically connecting to it but this may have some impacts on the app.
Besides this I was also thinking of communicating with the CF datasource through the CF admin API (cfide.adminapi.administrator) and also the option of extending the Oracle driver so that when CF connects with it these params are already set.
I would love to have your professional opinion and suggestions on this.
I know this is an old question...
You absolutely can pass connection string properties to ANY ColdFusion datasource.
Once the datasource is open in CF ADMIN, open the Advanced Settings. The first option you can change in the Advanced Settings tab is "Connection String". This would be all the name-value pairs of parameters separated by ampersand (&) to be passed on the connection to the database.
For example:
encryption_client=REQUIRED&encryption_types_client=DES40&crypto_checksum_client=REQUESTED&crypto_checksum_types_client=MD5`
However, the answer to the OP, is that you can pass parameters along on the JDBC URL as well.
For example:
Using the Progress Datadirect driver, your JDBC URL might look like this:
jdbc:datadirect:oracle://server;SID=someSID;encryption_client=REQUIRED;encryption_types_client=DES40;crypto_checksum_client=REQUIRED;crypto_checksum_types_client=MD5
Just remember, that your params are separated by semicolons, not commas.
When defining a datasource, make sure you are using the right KIND of driver... Some features aren't available from the native datasource setup screens, so to use more advanced features, you may need to use OTHER.
All data sources in ColdFusion can be configured with a connection string. I would see if it is possible to pass your properties as part of the connection string.
To change the connection string open the data source in the CF admin and go to 'Advanced Settings'. There is a box there you can fill out.
If you figure that out then the whole process should be transparent those those using the data source.
I hope that helps some.

Categories