应该连接到easyphp服务器上的phpmyadmin数据库,在用户数据库上插入一条记录,所有这些都通过glassfish服务器通过web服务,但这不会发生
netbeans说glassfish正在运行,因为它只有停止,删除或重启服务器的选项,但'start'选项显示为灰色
package Java;
import javax.jws.WebService;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.DriverManager;
/**
*
* @author Laplet Repair
*/
@WebService(serviceName = "AddUser")
public class AddUser {
private Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
private Connection connect = null;
public void readDataBase() throws Exception {
try {
// This will load the MySQL driver, each DB has its own driver
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager
.getConnection("jdbc:mysql://localhost:3306/conquest_of_the_fates_user?zeroDateTimeBehavior=convertToNull"
+ "user=root&password=");
// Statements allow to issue SQL queries to the database
statement = connect.createStatement();
// PreparedStatements can use variables and are more efficient
preparedStatement = connect
.prepareStatement("insert into feedback.comments values (default, ?, ?, ?)");
// "myuser, webpage, datum, summery, COMMENTS from feedback.comments");
// Parameters start with 1
preparedStatement.setString(1, "username");
preparedStatement.setInt(2, 5);
preparedStatement.setString(3, "password");
preparedStatement.executeUpdate();
resultSet = statement
.executeQuery("select * from feedback.comments");
} catch (Exception e) {
throw e;
} finally {
close();
}
}
// You need to close the resultSet
private void close() {
try {
if (resultSet != null) {
resultSet.close();
}
if (statement != null) {
statement.close();
}
if (connect != null) {
connect.close();
}
} catch (Exception e) {
}
}
}
这是错误日志
java.lang.RuntimeException:java.io.IOException at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:266) 在 org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189) 在 com.sun.enterprise.v3.services.impl.ContainerMapper $ HttpHandlerCallable.call(ContainerMapper.java:459) 在 com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167) 在 org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201) 在 org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175) 在 org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235) 在 org.glassfish.grizzly.filterchain.ExecutorResolver $ 9.execute(ExecutorResolver.java:119) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) 在 org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 在 org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561) 在 org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access $ 100(WorkerThreadIOStrategy.java:56) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy $ WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) 在 org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.doWork(AbstractThreadPool.java:565) 在 org.glassfish.grizzly.threadpool.AbstractThreadPool $ Worker.run(AbstractThreadPool.java:545)
答案 0 :(得分:0)
您应该在glassfish管理控制台上为MySQL数据库配置“JDBC资源”+“JDBC连接池” How to setup a JDBC connection in Glassfish
您可能需要在以下位置部署MySQL jdbc实现库: glassfish-install-path \ domains \ domain-name \ lib
创建EJB Singleton以检索连接:
@Singleton
@LocalBean
public class DBConnections {
@Resource(lookup = "resource JNDI name")
private DataSource dataSource;
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}