MySQL DB connection without using JDBC in Java(JSP/Servlets) [duplicate]
All, We are testing our new web application, built using Java JSP with MySQL Server as the DB, in a shared hosting environment (Linux). The DB connection is made using JDBC. The web application runs perfectly in our l开发者_运维百科ocal machines. But we are unable to connect to the MySQL server when running the web application from the shared hosting environment. When we raised a ticket the support people told us that it is not possible to use JDBC to connect to MySQL Server DB in shared hosting environment. They also gave the below PHP sample DB connection code and asked us to introduce a similar DB connection method in our JSP code.
$dbh = mysql_connect ("localhost", "username", "") or die('Cannot connect to the database because: ' . mysql_error());
mysql_select_db ("DBname");
The below line of code is being used to connect to the DB server in the shared hosting environment. dbURL = "jdbc:mysql://01.01.01.001/,'UserName','PWD'" As you can see only the ip Address has been specified in the connection string and not the port number because we were provided only with the ip address and not the port number of MySQL server.
Pls let us know on how to overcome this issue?And why do you think that the JDBC connection has not been enabled by the shared hosting people? Is port number essential to connect the MySQL server DB?
The other details are:
Tomcat 5.5 JSP/Servlets 2.0 MySQL 5.0 MySQL Database Engine: InnodbThanks
That's nonsense. JDBC is the way of connecting to a database. You problem lies elsewhere. Provide the code you are using and the exception.
The URL sure doesn't look right. You don't specify username and password like that. And the IP address doesn't look right either. As the PHP example uses localhost you should use that as well in the JDBC url (or 127.0.0.1)
A correct URL looks like this:
jdbc:mysql://localhost/yourdbname?user=dbusername&password=yourpassword
The port is not necessary unless MySQL was installed to use a different port.
It is also important how and where you configure this URL.
- Are you using a JNDI DataSource?
- Do you define the connection in a Hibernate configuration file?
- Some home-grown connection factory?
For more details check the manual:
http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html
It may be that the quotes are incorrect in your connection string.
dbURL = "jdbc:mysql://01.01.01.001/,'UserName','PWD'"
to
dbURL = "jdbc:mysql://01.01.01.001/","UserName","PWD"
Here are the lines of code to connect, though this is pretty inefficient, a connection pool is much better.
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost/mydb","myuser","mypass");
Hope some of this is helpful.
You can do something like this spring.datasource.url="jdbc:${CUSTOM_DB_URL}"
.
Where env CUSTOM_DB_URL
is mysql://localhost:3306/databaseName
.
If you also want user and password you can have something like this
spring.datasource.url="jdbc:${CUSTOM_DB_URL}?user=${CUSTOM_DB_USER}&password=${CUSTOM_DB_PASSWORD}"
I assumed that you wanted to extract the database url into an environment variable.
精彩评论