我目前正在学习Java EE,并被要求开发一个后端RESTful Java API,该API将与存储在云解决方案(IBM Bluemix)上的数据库进行通信。
我目前使用Eclipse Photon作为IDE,并且不是计算机的管理员用户。
我被要求使用IBM Was Liberty服务器插件测试我的代码以进行Eclipse,但是在设置服务器时遇到了一些麻烦。 我想我可能还不了解该插件的工作原理,也无法按照我的意愿使其工作。
问题是我无法通过我的api或管理中心访问存储在该服务器上的数据库(甚至无法登录管理中心)。
这是我使用(修改)并在此站点上找到的Java代码
package connection;
//STEP 1. Import required packages
import java.sql.*;
public class FirstExample {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/ibm/api/validator/dataSource/SAMPLEDB";
// Database credentials
static final String USER = "admin";
static final String PASS = "admin";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
//STEP 2: Register JDBC driver
Class.forName(JDBC_DRIVER);
//STEP 3: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//STEP 4: Execute a query
System.out.println("Creating statement...");
stmt = conn.createStatement();
String sql;
sql = "SELECT * FROM *";
ResultSet rs = stmt.executeQuery(sql);
//STEP 5: Extract data from result set
while(rs.next()){
//Retrieve by column name
String data = rs.getString("last");
//Display values
System.out.println("Data Retreived " + data);
}
//STEP 6: Clean-up environment
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
//Handle errors for JDBC
System.out.println("\nJDBC ERROR ! \n");
se.printStackTrace();
}catch(Exception e){
//Handle errors for Class.forName
System.out.println("\nClass.forName ERROR ! \n");
e.printStackTrace();
}finally{
//finally block used to close resources
try{
if(stmt!=null)
stmt.close();
}catch(SQLException se2){
System.out.println("\nSQL ERROR ! NOTHING TO DO \n");
}// nothing we can do
try{
if(conn!=null)
conn.close();
}catch(SQLException se){
System.out.println("\nJDBC ERROR ! MAYBE SOMETHING TO DO \n");
se.printStackTrace();
}//end finally try
}//end try
System.out.println("\nGoodbye!");
}//end main
}//end FirstExample
至于我的Server.xml,这是更新的代码:
<server description="Liberty beta">
<featureManager>
<feature>webProfile-8.0</feature>
<feature>adminCenter-1.0</feature>
<feature>restConnector-2.0</feature>
<feature>transportSecurity-1.0</feature>
<feature>logstashCollector-1.1</feature>
<feature>validator-1.0</feature>
<feature>mpConfig-1.3</feature>
<feature>mpFaultTolerance-1.1</feature>
<feature>mpHealth-1.0</feature>
<feature>mpJwt-1.0</feature>
<feature>mpMetrics-1.1</feature>
<feature>mpOpenAPI-1.0</feature>
<feature>mpOpenTracing-1.0</feature>
<feature>mpRestClient-1.0</feature>
<feature>localConnector-1.0</feature>
<feature>jdbc-4.2</feature>
</featureManager>
<httpEndpoint id="defaultHttpEndpoint"
host="*"
httpPort="9080"
httpsPort="9443" />
<applicationManager autoExpand="true"/>
<keyStore id="defaultKeyStore"
password="Liberty" />
<basicRegistry id="userReg">
<user password="admin"
name="admin"
id="admin"></user>
</basicRegistry>
<administrator-role>
<user>admin</user>
</administrator-role>
<remoteFileAccess>
<writeDir>${server.config.dir}</writeDir>
</remoteFileAccess>
<dataSource id="DefaultDataSource" jndiName="jdbc/mySQL">
<jdbcDriver libraryRef="MySQLLib" />
<properties.microsoft.sqlserver databaseName="SAMPLEDB" serverName="localhost" portNumber="3306" user="admin" password="admin"/>
<connectionManager></connectionManager>
</dataSource>
<library id="MySQLLib">
<file name="C:/PUBLIC/tools/mysql-connector-java-8.0.12/mysql-connector-java-8.0.12/mysql-connector-java-8.0.12.jar" />
</library>
</server>
我现在遇到的问题是我似乎无法与服务器通信。
这是控制台错误:
Connecting to database...
JDBC ERROR !
Goodbye!
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:832)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:456)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:207)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at connection.FirstExample.main(FirstExample.java:23)
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
at com.mysql.cj.NativeSession.connect(NativeSession.java:152)
at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:952)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:822)
... 6 more
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:173)
at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
... 9 more
作为一个初学者,我试图盲目地遵循在线上找到的教程,并且对我实际上在做什么并不怎么想。
我不知道Was Liberty不喜欢使用所需的服务来设置VM,而且在Liberty中设置 dataSource并不是在设置数据库 。 缺乏信息,指导和严谨性,我认为在创建数据源时,Liberty服务器实际上正在运行mySQL服务器。 实际上(如果我错了,请纠正我)这部分代码,
<dataSource id="DefaultDataSource" jndiName="jdbc/mySQL">
<jdbcDriver libraryRef="MySQLLib" />
<properties.microsoft.sqlserver databaseName="SAMPLEDB"
serverName="localhost"
portNumber="3306"
user="admin"
password="admin"/>
<connectionManager></connectionManager>
</dataSource>
<library id="MySQLLib">
<file name="C:/PUBLIC/tools/mysql-connector-java-8.0.12/mysql-connector-java-8.0.12/mysql-connector-java-8.0.12.jar" />
</library>
旨在使用足够的驱动程序在服务器中设置服务,以连接到远程数据库。 在这种情况下,我已经使用mySQL驱动程序设置了一个数据源。
当我确实运行本地mySQL服务器时,我能够通过我的Java应用程序进行通信和发送查询。
感谢那些花时间阅读这篇文章并尝试帮助我的人们。