JDBC Java代码中的太多客户端任务错误

时间:2019-03-06 19:21:11

标签: java jdbc odbc

我正在使用Access数据库,正在创建日历应用程序。 我在尝试获取任务和事件的提醒时遇到错误。 我的问题是我尚未关闭连接,但是如果我关闭了连接,它将如何检查提醒。我应该如何继续关闭连接

这是错误:

获取当前事件数据时出错。     java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驱动程序]太多的客户端任务。         在sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)         在sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)         在sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3080)         在sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)         在sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)         在java.sql.DriverManager.getConnection(DriverManager.java:571)         在java.sql.DriverManager.getConnection(DriverManager.java:233)         在database.DatabaseConnection.createConnection(DatabaseConnection.java:22)         在database.DatabaseConnection.getCurrentTaskData(DatabaseConnection.java:71)         在Home.TaskCheckThread.run(TaskCheckThread.java:24)     获取当前事件数据时出错。     java.sql.SQLException:[Microsoft] [ODBC Microsoft Access驱动程序]太多的客户端任务。         在sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6964)         在sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7121)         在sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3080)         在sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)         在sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)         在java.sql.DriverManager.getConnection(DriverManager.java:571)     获取当前事件数据时出错。         在java.sql.DriverManager.getConnection(DriverManager.java:233)         在database.DatabaseConnection.createConnection(DatabaseConnection.java:22)         在database.DatabaseConnection.getCurrentEventData(DatabaseConnection.java:39)         在Home.ReminderCheckThread.run(ReminderCheckThread.java:28)

我的代码 DatabaseConnection

 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();
    }
}

0 个答案:

没有答案