- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我试图从 url 中读取数据并存储在一个字符串中,当我将该字符串值存储在我的 sql 表中时,出现以下异常
Exception in thread "main" com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
如何删除这个异常
这是y代码
public class DownLoadData {
Statement st = null;
Connection connection = null;
String driverName = "com.mysql.jdbc.Driver";
String serverName = "localhost";
String schema = "mandi";
String url1 = "jdbc:mysql://" + serverName + "/" + schema;
String username = "root";
String password = "";
public void DownloadCommodity() throws ClassNotFoundException, SQLException {
int j = 0;
String htmlTableText = null;
System.setProperty("webdriver.chrome.driver", "C:\\Users\\SHAKTI\\Desktop\\JarFiles\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String commodity = "Jo";
String commo[] = {"Paddy", "Rice", "Jwar", "Barley", "Corn"
};
for (String com : commo) {
String sDate = "27/03/2014";
String url = "http://www.upmandiparishad.in/commodityWiseAll.aspx";
driver.get(url);
new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText(com);
driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);
driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
htmlTableText = findElement.getText();
// String html=find.getText();
// do whatever you want now, This is raw table values.
htmlTableText = htmlTableText.replace("S.No.DistrictMarketPrice", "");
htmlTableText = htmlTableText.replaceAll("\\s(\\d+\\s[A-Z])", "\n$1");
htmlTableText = htmlTableText.replaceAll("(?=(.*?[ ]){4,}).*?[\n\r]", "");
htmlTableText = htmlTableText.replace("S.No. District Market Price", "");
System.out.println(htmlTableText);
String s[] = htmlTableText.split("");
StringTokenizer str = new StringTokenizer(htmlTableText);
while (str.hasMoreTokens()) // for(int i=0;i<s.length;i++)
// if(str.hasMoreElements())
{
String no = str.nextElement().toString();
String city = str.nextElement().toString();
String mandi = str.nextElement().toString();
String price = str.nextElement().toString();
Class.forName(driverName);
connection = DriverManager.getConnection(url1, username, password);
PreparedStatement ps = connection.prepareStatement("insert into commoditydemo values(?,?,?,?,?,?)");
ps.setString(1, no);
ps.setString(2, city);
ps.setString(3, mandi);
ps.setString(4, price);
ps.setString(5, com);
ps.setString(6, "0");
j = ps.executeUpdate();
connection.close();
}
}
driver.close();
driver.quit();
if (j == 1) {
System.out.println("data inserted");
} else {
System.out.println("not inserted");
}
}
如何获得正确的输出?
提前致谢
最佳答案
我怀疑问题出在 while 循环的每次迭代 打开和关闭数据库连接。数据库需要大量清理工作才能关闭连接,您可能会创建关闭连接任务的积压,积压的速度比清除速度快。
最好在循环之前方法中打开一个连接一次,完成工作,然后在之后关闭连接环形。您可能需要围绕连接的使用进行 try-catch-finally 以确保它正确关闭。如果您使用的是 java 7,请使用 try-resource block 。
关于使用 JDBC 的 Java - 连接异常过多?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23488619/
我在 JBoss 6 上使用 JSF 2。我制作了一个示例应用程序,它使用 JSF 作为 View ,使用 EJB 作为逻辑,使用 JPA 作为持久性。 JSF 中的 bean 具有 RequestS
我打开气流并检查了连接,然后发现背后有太多连接。 关于如何杀死那些我不使用的想法,或者我很想知道运行它的最低conn_id的任何想法。 建筑 LocalExecutor(与其他经纪人不同) Postg
我收到错误 style.css:1 GET https://www.example.com/cs_CZ净::ERR_TOO_MANY_REDIRECTS 可能与我的 htaccess 有关吗? Rew
我收到错误 style.css:1 GET https://www.example.com/cs_CZ净::ERR_TOO_MANY_REDIRECTS 可能与我的 htaccess 有关吗? Rew
我正在 Android 中开发一个应用程序,我有以下 XML 布局:
我有 5 个类用作运算符: TurnOn , TurnOff , PushBox , Exit , Enter 对于每种类型,我都有一个包含该类型描述的字符串。 例如: class Places {
我们在使用 lettuce redis 库时遇到了这个具体问题。我们收到太多 RedisCommandTimeoutException。我们在 redis-cli 中设置了 2 秒的超时,在 redi
在 Controller 类文件中,大多数方法函数都包含try/catch块,如下所示: try { $stmt = $this->prepare($sql); $st
我读到这个page附加很多元素是不好的做法,我应该在循环的每次迭代期间构建一个字符串,然后将 DOM 元素的 HTML 设置为该字符串。在循环中使用过多的 HTML 是否也是如此? 我有一个解析 JS
在我的应用程序日志上运行 logcat 时,我看到很多 GC_FOR_ALLOC 而没有 GC_CONCURRENT 消息。我知道我有内存泄漏,但在堆栈和其他站点的任何地方,logcat 消息都有很多
当我检查我的 Tomcat 应用程序管理器时,我发现我的 session 太多(超过 4600): 为什么会这样?这可以链接到已部署的应用程序,还是它已经运行了很长时间? 最佳答案 这可能有多种原因,
所以我试图在一个 div 中运行 3 个 float ,但我遇到了这个问题。 http://jsfiddle.net/sPUjQ/101/ 点我 每当我运行两次 float 时,背景颜色会保持不变,但
为什么在使用 http_build_query 函数构建查询字符串时,它会将方括号 [] urlencode 到值之外,如何摆脱它? $query = array("var" => array("fo
在一个过程中,我需要使用许多 if-else 条件来决定列的值。该脚本以 FOR rec IN (SELECT....) LOOP Begin 并决定许多不同的值 rec 子记录可以在每次迭代中获得。
我们基于 Java 的服务器应用程序报告异常长的类加载时间: # jstat -class 10625 1000 Loaded Bytes Unloaded Bytes Time 4
我正在使用 Kraken.js 开发一个应用程序并管理我决定使用的 session :connect-mongo。 我有这样的设置: 'use strict'; var session = requi
我在 iPad 应用程序中有 180 个 UILabels(UITableViewCells 的 subview ),每个 UILabel 的宽度为 155 X 155,并且每个包含大量 Unicod
所以基本上,如果我将 JPanel 放入使用 GridBagLayout 的 JPanel 中,并使用 setPreferredSize 限制大小>,最终它达到了无法容纳所有这些的地步,并且它表现出附
我目前使用的是 Play 2.2.1,在白天运行生产 Web 应用程序后,我的日志中开始出现以下错误。似乎我的连接已用完/我一定没有正确关闭某些东西,但我似乎无法弄清楚它是什么。 2014-02-11
所以我几乎完成了我的元素,我打算创建一个完整的网页 slider 作为我网站的背景主题,并且它运行完美! 问题是它的垂直拉伸(stretch)太长,您可以向右滚动(您不应该那样做)而且我不知道为什么它
我是一名优秀的程序员,十分优秀!