I developed a web application that runs on my computer on localhost. Then I loaded the war file into catalina home on a remote server. Web app runs but it stops when it try to connect to database on server. The connection is a jbdc connection on localhost, the database is mysql. When I do a connection on my computer, no problems occour.
String connectionString="jdbc:mysql://192.168.0.100:3306/"+request.getSession().getAttribute("dbname");
Connection con=null;
try {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException | IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
response.sendRedirect("Errore.html");
return;
};
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=(Connection) DriverManager.getConnection(connectionString,"root","root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
The Connection con is null,DriveManager.getConnection doesn't work and I don't know why. I also tried with postgresql connection but the problem is the same. Must I configure something in remote server? The Server is debian 9.2 like my computer.
我认为您需要将连接字符串定义为不是本地主机,而是完整的IP: String connectionString="jdbc:mysql://xx.xx.xx.xx:3306/"+request.getSession().getAttribute("dbname");
Try the following code to test the connection. Its a Maven project.
The pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>my.test</groupId>
<artifactId>mavenproject2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
and the src/main/java/App.java (please replace the connection data)
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class App {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String host = "localhost";
String db = "mysql";
String user = "root";
String password = "root";
String connectionString = String.format("jdbc:mysql://%s:3306/%s", host, db);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(connectionString, user, password);
System.out.println("Everthing looks alright!");
}
}
If you compile this project you will get a mavenproject2-0.0.1-SNAPSHOT-jar-with-dependencies.jar Upload the JAR to your remote system and try it there
java -jar mavenproject2-0.0.1-SNAPSHOT-jar-with-dependencies.jar
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.