Я использую базу данных Access. Я создаю приложение календаря.
я получаю сообщение об ошибке, когда я пытаюсь получить напоминание для задач и событий.
Проблема в том, что я не закрыл соединение, но если я закрою соединение, то как оно будет проверять наличие напоминаний. Как мне продолжить закрытие соединения
Вот ошибка:
Ошибка при получении данных текущего события.
java.sql.SQLException: [Microsoft] [ODBC Microsoft Access Driver] Слишком много клиентских задач.
at sun.jdbc.odbc.JdbcOdbc.createSQLException (JdbcOdbc.java:6964)
at sun.jdbc.odbc.JdbcOdbc.standardError (JdbcOdbc.java:7121)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect (JdbcOdbc.java:3080)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize (JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect (JdbcOdbcDriver.java:174)
в java.sql.DriverManager.getConnection (DriverManager.java:571)
в java.sql.DriverManager.getConnection (DriverManager.java:233)
at database.DatabaseConnection.createConnection (DatabaseConnection.java:22)
at database.DatabaseConnection.getCurrentTaskData (DatabaseConnection.java:71)
at Home.TaskCheckThread.run (TaskCheckThread.java:24)
Ошибка при получении данных текущего события.
java.sql.SQLException: [Microsoft] [ODBC Microsoft Access Driver] Слишком много клиентских задач.
at sun.jdbc.odbc.JdbcOdbc.createSQLException (JdbcOdbc.java:6964)
at sun.jdbc.odbc.JdbcOdbc.standardError (JdbcOdbc.java:7121)
at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect (JdbcOdbc.java:3080)
at sun.jdbc.odbc.JdbcOdbcConnection.initialize (JdbcOdbcConnection.java:323)
at sun.jdbc.odbc.JdbcOdbcDriver.connect (JdbcOdbcDriver.java:174)
в java.sql.DriverManager.getConnection (DriverManager.java:571)
Ошибка при получении данных текущего события.
в java.sql.DriverManager.getConnection (DriverManager.java:233)
at database.DatabaseConnection.createConnection (DatabaseConnection.java:22)
at database.DatabaseConnection.getCurrentEventData (DatabaseConnection.java:39)
в Home.ReminderCheckThread.run (ReminderCheckThread.java:28)
Мой код Соединение с базой данных :
package database;
import javax.swing.JOptionPane;
public class DatabaseConnection
{
public Connection con;
public PreparedStatement ps,ps1;
public ResultSet rs,rs1;
private SimpleDateFormat simpleDateFormat;
public static Connection createConnection() throws Exception
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:Calendar");
return con;
}
public void closeConnection(Connection con)throws SQLException
{
con.close();
}
public ResultSet selectStatement(String sql)throws Exception
{
this.createConnection();
ps=con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=ps.executeQuery();
return rs;
}
public ResultSet getCurrentEventData() {
try {
con = this.createConnection();
} catch(Exception e) {
System.out.println("Error while getting current event data.");
e.printStackTrace();
}
String query = "SELECT * FROM Event where e_from_date like ? AND e_status like 'Not Viewed'";
simpleDateFormat = new SimpleDateFormat("dd-MMMMM-yyyy");
Calendar cal = Calendar.getInstance();
try {
ps = con.prepareStatement(query,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ps.setString(1, simpleDateFormat.format(cal.getTime()));
rs = ps.executeQuery();
return rs;
}
catch (SQLException e) {
System.out.println("Failed while executing getAllReminders.");
e.printStackTrace();
}
return null;
}
public ResultSet getCurrentTaskData() {
try {
con = this.createConnection();
} catch(Exception e) {
System.out.println("Error while getting current event data.");
e.printStackTrace();
}
String query = "SELECT * FROM Task where t_date like ? AND t_rem_status like 'Not Viewed'";
simpleDateFormat = new SimpleDateFormat("dd-MMMMM-yyyy");
Calendar cal = Calendar.getInstance();
try {
ps = con.prepareStatement(query,ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ps.setString(1, simpleDateFormat.format(cal.getTime()));
rs = ps.executeQuery();
return rs;
} catch (SQLException e) {
System.out.println("Failed while executing TaskgetAllReminders.");
e.printStackTrace();
}
return null;
}
public void insertRecord(String table, String[] cols) {
String sql = "insert into " + table + " values(";
try {
this.createConnection();
} catch(Exception e) {
e.printStackTrace();
}
for(int i=0;i<cols.length;i++) {
if(i == cols.length-1) {
sql = sql + "'" + cols[i] + "')";
} else {
sql = sql + "'" + cols[i] + "',";
}
}
try {
ps = con.prepareStatement(sql);
if(ps.execute() == false) {
JOptionPane.showMessageDialog(null, "Record inserted successfully!");
} else {
JOptionPane.showMessageDialog(null, "Record not inserted.");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateRecord(String table,String field,String sat,Object custid,String date)
{
String sql="update "+ table +" set "+ field +" = "+ "'" + sat + "'" + ",Date_c='"+date+"' where Cust_Id = " +custid;
try {
this.createConnection();
ps=con.prepareStatement(sql);
ps.executeUpdate();
if(ps.execute()==false)
{
JOptionPane.showMessageDialog(null, "Record Updated successfully!");
} else {
JOptionPane.showMessageDialog(null, "Record not Update");
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
здесь TaskCheckThread.java
package Home;
import java.sql.ResultSet;
import java.util.Calendar;
import javax.swing.JOptionPane;
import database.DatabaseConnection;
public class TaskCheckThread extends Thread
{
private DatabaseConnection dataWriter;
private ResultSet rs;
public TaskCheckThread()
{
dataWriter = new DatabaseConnection();
}
public void run()
{
while(true)
{
rs = dataWriter.getCurrentTaskData();
Calendar cal = Calendar.getInstance();
String tokens[] = null, str="";
try
{
if(rs != null)
{
while(rs.next())
{
tokens = rs.getString(7).split("-");
cal = Calendar.getInstance();
if(tokens[0].equals(""+cal.get(Calendar.HOUR)))
{
if((Integer.parseInt(tokens[1]) - cal.get(Calendar.MINUTE)) <= rs.getInt(11))
{
str += rs.getString(2) + rs.getString(3) + rs.getString(4);
JOptionPane.showMessageDialog(null, str);
rs.updateString(12, "Viewed");
rs.updateRow();
}
}
}
}
Thread.sleep(5000);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
}
здесь ReminderCheckThread.java
package Home;
import java.sql.ResultSet;
import java.util.Calendar;
import javax.swing.JOptionPane;
import database.DatabaseConnection;
public class ReminderCheckThread extends Thread
{
private DatabaseConnection dataWriter;
private ResultSet rs;
public ReminderCheckThread()
{
dataWriter = new DatabaseConnection();
}
public void run()
{
while(true)
{
rs = dataWriter.getCurrentEventData();
Calendar cal = Calendar.getInstance();
String tokens[] = null, str="";
try {
if(rs != null)
{
while(rs.next())
{
tokens = rs.getString(13).split("-");
cal = Calendar.getInstance();
if(tokens[0].equals(""+cal.get(Calendar.HOUR)))
{
if((Integer.parseInt(tokens[1]) - cal.get(Calendar.MINUTE)) <= rs.getInt(15))
{
str += ""+"Title :-"+rs.getString(2)+"Location :-" + rs.getString(3)+"Description :-" + rs.getString(4);
JOptionPane.showMessageDialog(null, str);
rs.updateString(17, "Viewed");
rs.updateRow();
}
}
}
}
}
catch(Exception ex)
{
}
try
{
Thread.sleep(5000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
public static void main(String args[])
{
new ReminderCheckThread();
}
}