有时MySql在从java连接时会抛出错误,但并非总是如此。大多数时候程序运行没有任何错误

时间:2014-06-28 23:14:22

标签: java mysql

问题似乎是随机的。正如您在下面的输出中所看到的,循环执行了5次(预期70次),然后出现问题。 Wait_timeout设置为28800.可能的原因是什么? 这是代码(数据库部分)和输出。

 public class DataManager {
    private Connection conn=null;
    private Properties props=null;

    private Statement batchStat;
    private ResultSet result=null;
    private String userName="root";
    private String password="root";
    private String url="jdbc:mysql://localhost/";//jdbc:oracle:thin:@localhost";
    private String driver="com.mysql.jdbc.Driver";//oracle.jdbc.OracleDriver";


    public  DataManager()   {
            try { 
                Class.forName(driver) ;

              conn = DriverManager.getConnection(url, userName,password);
              batchStat=conn.createStatement();

            } catch (ClassNotFoundException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            } catch (SQLException ex) {
                Logger.getLogger(DataManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
public ArrayList<PriceData> getPriceList(String symbol,String table,String t_date) throws SQLException{

        DataManager dm=new DataManager();
        ResultSet rs=dm.execute("select * from trader."+table+" where symbol = '"+symbol+"' and t_date='"+t_date+"'");
        ArrayList<PriceData> priceList=new ArrayList<PriceData>();
        PriceData pd=null;
        while(rs.next()){

            pd=new PriceData();
            pd.setDate((rs.getString("t_date")));
            pd.setTime((rs.getString("t_time")));
            pd.setOpen(Double.parseDouble(rs.getString("open")));
            pd.setHigh(Double.parseDouble(rs.getString("high")));
            pd.setLow(Double.parseDouble(rs.getString("low")));
            pd.setClose(Double.parseDouble(rs.getString("close")));
            pd.setVolume(Long.parseLong(rs.getString("volume"))); 
            priceList.add(pd);

        }
         dm.disconnect();
         return priceList;
        }



Output: 

Total PnL for M&M : 100219.9955996275

Total PnL for MARUTI : 93443.85619986057

Total PnL for NMDC : 74072.72315007448

Total PnL for NTPC : 104440.35690015554

Total PnL for ONGC : 122273.74220007658


Jun 29, 2014 4:16:26 AM strategies.DataManager <init>
SEVERE: null
com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: 

** BEGIN NESTED EXCEPTION ** 

java.net.SocketException
MESSAGE: java.net.SocketException: Permission denied: connect


java.net.SocketException: java.net.SocketException: Permission denied: connect
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:151)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:280)
    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1765)
    at com.mysql.jdbc.Connection.<init>(Connection.java:430)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:268)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at strategies.DataManager.<init>(DataManager.java:41)
    at strategies.Utilities.getPriceList(Utilities.java:21)
    at strategies.StratORBSpot.start(StratORBSpot.java:81)
    at strategies.Starter.main(Starter.java:23)


** END NESTED EXCEPTION **


    at com.mysql.jdbc.Connection.createNewIO(Connection.java:1830)
    at com.mysql.jdbc.Connection.<init>(Connection.java:430)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:268)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)

0 个答案:

没有答案