Java使用JDBC-连接太多异常?
我试图从url读取数据并存储在字符串中,当我将该字符串值存储在我的sql表中时,出现以下异常Java使用JDBC-连接太多异常?,java,mysql,jdbc,Java,Mysql,Jdbc,我试图从url读取数据并存储在字符串中,当我将该字符串值存储在我的sql表中时,出现以下异常 Exception in thread "main" com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections" 如何删除此异常 这是y代码 pu
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");
}
}
公共类下载数据{
语句st=null;
连接=空;
String driverName=“com.mysql.jdbc.Driver”;
字符串serverName=“localhost”;
字符串schema=“mandi”;
String url1=“jdbc:mysql://”+serverName+“/”+schema;
字符串username=“root”;
字符串密码=”;
public void DownloadCommodity()抛出ClassNotFoundException、SQLException{
int j=0;
字符串htmlTableText=null;
System.setProperty(“webdriver.chrome.driver”,“C:\\Users\\SHAKTI\\Desktop\\JarFiles\\chromedriver\u win32\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
字符串商品=“Jo”;
字符串commo[]={“稻谷”、“大米”、“Jwar”、“大麦”、“玉米”
};
用于(字符串com:commo){
字符串sDate=“27/03/2014”;
字符串url=”http://www.upmandiparishad.in/commodityWiseAll.aspx";
获取(url);
新选择(driver.findElement(By.id(“ctl00\u ContentPlaceHolder1\u ddl\u商品”)))。选择ByVisibleText(com);
driver.findElement(By.id(“ctl00\u ContentPlaceHolder1\u txt\u rate”)).sendKeys(sDate);
driver.findElement(By.id(“ctl00\u ContentPlaceHolder1\u btn\u show”)。单击();
WebElement findElement=driver.findElement(By.id(“ctl00\u ContentPlaceHolder1\u GridView1”);
htmlTableText=findElement.getText();
//字符串html=find.getText();
//现在做你想做的,这是原始表值。
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);
字符串s[]=htmlTableText.split(“”);
StringTokenizer str=新的StringTokenizer(htmlTableText);
while(str.hasMoreTokens())//for(int i=0;i您正在while循环中重复打开连接:
connection = DriverManager.getConnection(url1, username, password);
尝试将连接的建立置于循环外部(之前)然后在循环完成后将其关闭。您正在while循环内部重复打开连接:
connection = DriverManager.getConnection(url1, username, password);
尝试将连接的建立置于循环外部(之前)然后在循环完成后将其关闭。您正在while循环内部重复打开连接:
connection = DriverManager.getConnection(url1, username, password);
尝试将连接的建立置于循环外部(之前)然后在循环完成后将其关闭。您正在while循环内部重复打开连接:
connection = DriverManager.getConnection(url1, username, password);
尝试将建立连接放在循环外部(之前)然后在循环完成后将其关闭。尝试以下代码:
我刚刚初始化了外的连接,而循环
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);
connection = DriverManager.getConnection(url1, username, password);
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);
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");
}
}
公共类下载数据{
语句st=null;
连接=空;
String driverName=“com.mysql.jdbc.Driver”;
字符串serverName=“localhost”;
字符串schema=“mandi”;
String url1=“jdbc:mysql://”+serverName+“/”+schema;
字符串username=“root”;
字符串密码=”;
public void DownloadCommodity()抛出ClassNotFoundException、SQLException{
int j=0;
字符串htmlTableText=null;
System.setProperty(“webdriver.chrome.driver”,“C:\\Users\\SHAKTI\\Desktop\\JarFiles\\chromedriver\u win32\\chromedriver.exe”);
WebDriver驱动程序=新的ChromeDriver();
字符串商品=“Jo”;
字符串commo[]={“稻谷”、“大米”、“Jwar”、“大麦”、“玉米”
};
用于(字符串com:commo){
字符串sDate=“27/03/2014”;
字符串url=”http://www.upmandiparishad.in/commodityWiseAll.aspx";
获取(url);
新选择(driver.findElement(By.id(“ctl00\u ContentPlaceHolder1\u ddl\u商品”)))。选择ByVisibleText(com);
driver.findElement(By.id(“ctl00\u ContentPlaceHolder1\u txt\u rate”)).sendKeys(sDate);
driver.findElement(By.id(“ctl00\u ContentPlaceHolder1\u btn\u show”)。单击();
WebElement findElement=driver.findElement(By.id(“ctl00\u ContentPlaceHolder1\u GridView1”);
htmlTableText=findElement.getText();
//字符串html=find.getText();
//现在做你想做的,这是原始表值。
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);
字符串s[]=htmlTableText.split(“”);
StringTokenizer str=新的StringTokenizer(htmlTableText);
connection=DriverManager.getConnection(url1、用户名、密码);
while(str.hasMoreTokens())//对于(int i=0;i请尝试以下代码:
我刚刚初始化了外的连接,而循环
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);
connection = DriverManager.getConnection(url1, username, password);
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);
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");
}
}
公共类下载数据{
语句st=null;
连接=空;
String driverName=“com.mysql.jdbc.Driver”;
字符串serverName=“localhost”;
字符串schema=“mandi”;
String url1=“jdbc:mysql://”+serverName+“/”+schema;
字符串username=“root”;
字符串密码=”;
public void DownloadCommodity()抛出ClassNotFoundException、SQLException{
int j=0;
字符串htmlTableText=null;
System.setProperty(“webdriver.c