有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java没有可用的缓冲区空间,已达到最大连接数

package Simple;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.text.*;

public class CurrentProg {
    //connecting to the database

  private static final String DB_DRIVER = "com.mysql.jdbc.Driver";
  private static final String DB_CONNECTION ="jdbc:mysql://localhost:3306/db?autoReconnect=true"; 
  private static final String DB_USER = "root";
  private static final String DB_PASSWORD = "root";
  static Connection dbConnection = null;
  static Statement statement = null;

  static int total=1;

  //Searching between startdate and enddate

  public static java.util.LinkedList searchBetweenDates(java.util.Date startDate, java.util.Date endDate) {

        java.util.Date begin = new Date(startDate.getTime());
        java.util.LinkedList list = new java.util.LinkedList();
        list.add(new Date(begin.getTime()));
        java.util.Date end = new Date(endDate.getTime());
        endDate.setTime(endDate.getTime() + 24*3600*1000);
        Calendar cal = Calendar.getInstance();
        cal.setTime(begin);
       dbConnection = getDBConnection();

        while(begin.compareTo(endDate)<0){
        begin = new Date(begin.getTime() + 86400000);
        list.add(new Date(begin.getTime()));        
        Timestamp timestamp = new Timestamp(new Date().getTime());

        //For a single day calculation: 24hours*60mins=1440 /2 (2 mins time difference as per the requirement) = 720 

                for (int j = 0; j < 720; j++) {
                cal.add(Calendar.MINUTE, 2);            
                timestamp = new Timestamp(cal.getTime().getTime());
                String S = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(timestamp);               
                String[] parts = S.split(" ");
                String date=parts[0];
                String time=parts[1];
                cal.getTime().toString();

           // To create data loop into a List
            List<String> records = new ArrayList<String>();
            StringBuffer record = new StringBuffer();
            for (int i = 1; i <= total; i++) {
            records = new ArrayList<String>(total);         

            int a2 = 220 + j % 31; // 230 - 244 by 1
            String wString = Integer.toString(a2);
            String a = String.valueOf(a2);   
            double b2 = 0.00 + j % 3.7  ; // 1.3 - 3.9 by 0.1 
            String aString = Double.toString(b2);
            String b = String.valueOf(b2);
            b = b.substring(0, Math.min(b.length(), 5));


          record.delete(0, record.length());
          record.append(a + "," + b  +  ",'"+  date + "', '"+  time + "'");
          record.append("\t\t");      
          record.append("\n");
          records.add(record.toString()); 

          //Insert Query
          String insertTableSQL = "INSERT INTO cmd1"
                  + "(a, b, date, time) " + "VALUES"
                  + "("+record.toString()+")";
                  System.out.println("insertTableSQL - " + insertTableSQL);             //  Statement.executeUpdate(insertTableSQL);

          try {

                  statement = dbConnection.createStatement();
                  statement.executeUpdate(insertTableSQL);
                  System.out.println("Record is inserted into Db table!");
                  } catch (SQLException e) {
                  System.out.println(e.getMessage());
                  } 
      try {

            //  dbConnection = getDBConnection();
              statement = dbConnection.createStatement();
              statement.executeUpdate(insertTableSQL);
              System.out.println("Record is inserted into Db table!");
              } catch (SQLException e) {
              System.out.println(e.getMessage());
              } 
        finally {
        //  httpPost.releaseConnection()
            try{
                 if(statement!=null)
                    statement.close();
              }
             finally{
             }

              try{
                 if(dbConnection!=null)
                    dbConnection.close();
              }
              finally{

              }

              }                                            
         }
        }
        }
        return list;
        }

  @SuppressWarnings("unused")
public static void main(String[] args) throws Exception {

      //To enter startDate and enddate
    //  EntityManagerFactory.getCache().evictAll;


    SimpleDateFormat  startDate=new java.text.SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat  endDate=new java.text.SimpleDateFormat("yyyy-MM-dd");

                java.util.LinkedList hitList = searchBetweenDates(
                startDate.parse("2016-01-01"),
                endDate.parse("2016-03-01"));
                String[] combo = new String[hitList.size()];
                for(int i=0; i<hitList.size(); i++)
                combo[i] = new java.text.SimpleDateFormat("yyyy-MM-dd").format(((java.util.Date)hitList.get(i)));

  }

private static void insertRecordIntodb() {
    //
  }
  private static Connection getDBConnection() {
    Connection dbConnection = null;
    try {
      Class.forName(DB_DRIVER);
    } catch (ClassNotFoundException e) {
      System.out.println(e.getMessage());
    }
    try {
      dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER, DB_PASSWORD);
      return dbConnection;
    } catch (SQLException e) {
      System.out.println(e.getMessage());
    }
    return dbConnection;
  }  
} 

事务处理期间服务器连接失败。由于基础异常:“java”。网SocketException:java。网SocketException:没有可用的缓冲区空间(已达到最大连接数?):连接'

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: java.net.SocketException: No buffer space available (maximum connections reached?): connect

STACKTRACE:

java.net.SocketException: java.net.SocketException: No buffer space available (maximum connections reached?): connect
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:156)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:276)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:2717)
    at com.mysql.jdbc.Connection.<init>(Connection.java:1509)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:266)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at Simple.CurrentProg.getDBConnection(CurrentProg.java:186)
    at Simple.CurrentProg.searchBetweenDates(CurrentProg.java:126)
    at Simple.CurrentProg.main(CurrentProg.java:164)


** END NESTED EXCEPTION **


Attempted reconnect 3 times. Giving up.
Exception in thread "main" java.lang.NullPointerException
    at Simple.CurrentProg.searchBetweenDates(CurrentProg.java:137)
    at Simple.CurrentProg.main(CurrentProg.java:164)

在这里,我试图将java程序与数据库连接起来,但当我尝试插入大数据时,比如说1个月,所以它只获取16000条记录,不超过我想要的数量。数据应该按照给定的日期范围插入,我应该做些什么来获取这些数据。在stacktrace中,它会显示一个异常,因为没有可用的缓冲区空间,因此未达到最大连接数。提前谢谢


共 (0) 个答案