Answer the question
In order to leave comments, you need to log in
Can't connect to database by URL?
I ran into a problem - I can not connect to the database by URL. Those. there is a servlet that connects to the database and makes a request. Having changed the URL to the real database and uploading the entire project to the server, I got an error connecting to the database.
I reproduced the problem by creating a new database on the locale with the name est, before that I used the database with the name fermer, in debugging I got the following picture: although I changed the URL, the program still tries to connect to the previous database.
Answer the question
In order to leave comments, you need to log in
Try like this. In the META-INF folder in the webapp, add context.xml with the content:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/est"
auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root"
password="root"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/est?useEncoding=true&characterEncoding=UTF-8"/>
</Context>
public class DaoUtil {
public static Connection getConnection(){
Connection connection = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
Context context = new InitialContext();
Context initContext = (Context )context.lookup("java:/comp/env");
DataSource ds = (DataSource) initContext.lookup("jdbc/est");
connection = ds.getConnection();
}catch (SQLException | NamingException ex){
ex.printStackTrace();
}
return connection;
}
public static void close(Statement statement, ResultSet rs,Connection connection){
try {
if (statement != null) {
statement.close();
}
if (rs != null) {
rs.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException ex){
ex.printStackTrace();
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question