使用mysql驱动程序和glassfish服务器连接到连接池和jdbc资源时出现上述错误。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package database;
/**
*
* @author LenasalonM01
*/
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import javax.swing.JOptionPane;
public class database {
private Connection connect;
private Statement statement;
private ResultSet resultset;
private InitialContext context;
private DataSource datasource;
public void connection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/hrms", "root","");
context = new InitialContext();
datasource = (DataSource)context.lookup("jdbc/hrms");
connect = datasource.getConnection();
statement = connect.createStatement();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
public void disconnect() {
try {
if (resultset != null) {
resultset.close();
}
if (connect != null) {
connect.close();
}
if (context != null) {
context.close();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
public boolean checkValid(String query) {
try {
connection();
resultset = statement.executeQuery(query);
if (resultset.next()) {
return true;
} else {
return false;
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
return false;
}
}
public ResultSet fetchdata(String query) {
try {
connection();
resultset = statement.executeQuery(query);
disconnect();
return resultset;
} catch (Exception e) {
return resultset;
}
// return resultset;
}
// public ResultSet insertdata(){
//
// }
}
服务器资源代码如下
<resources>
<jdbc-resource enabled="true" jndi-name="jdbc/hrms" object-type="user" pool-name="AffablebeanPool">
<description/>
</jdbc-resource>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="AffablebeanPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.ConnectionPoolDataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="URL" value="jdbc:mysql://localhost:3306/hrms"/>
<property name="User" value="root"/>
<property name="Password" value=""/>
</jdbc-connection-pool>
</resources>