我正在做一些功课,我需要将 mysql 与 netbeans 相关联,以便保存一些certian数据。 我失去了联系,我不知道为什么和在哪里。
这是我的代码:
public void runThread(int taban , int tavan) {
int primNum=0;
try
{
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.out.println("MySQL JDBC Driver not found !!");
return;
}
try{
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/primenumbers" , "root" , "password123");
System.out.println("SQL Connection to database established!");
Statement stmt = (Statement) con.createStatement();
int i=taban;
while(i<tavan){
boolean tf = TredAsal.isPrime(i);
if(tf==true) {
primNum++;
if(i>15){
String insert = "INSERT INTO primenumbers VALUES (" + i + ")";
stmt.execute(insert);
stmt.cancel();
}
Thread.sleep(500);
}
i+=2;
}
con.close();
}
catch(Exception e){
System.out.println("Connection Failed! Check output console1");
}
}
我正在调用runThread的代码:
StringBuilder sb = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
sb.append("");
sb.append(clicks);
String strI = sb.toString();
jTextField1.setText(strI);
int tavan;
tavan=(int) sqrt(clicks);
int tredsayisi=0;
int Brojgrupe;
TredAsal ta= new TredAsal();
int x = ta.run(tavan);
sb2.append("");
sb2.append(x);
String strP=sb2.toString();
if(x<100){
tredsayisi = 2 ;
}
else {
tredsayisi=(int) (x/100)+1;
}
Brojgrupe = tavan / tredsayisi;
if(tredsayisi==2 ){
StringBuilder sb3 = new StringBuilder();
StringBuilder sb4 = new StringBuilder();
sb3.append("");
sb3.append(Brojgrupe);
sb4.append("");
sb4.append(tavan);
String strBit2 = sb3.toString();
String strBit3 = sb4.toString(); // pravim text za bas i bitis noktasi
TredAsal t1 = new TredAsal();
TredAsal t2 = new TredAsal();
t1.runThread(3, Brojgrupe); //Thread.sleep(1000);
t2.runLastThread(tavan, Brojgrupe);// Thread.sleep(1000);
jTextField2.setText("2");
jTextField9.setText(strBit2);
jTextField3.setText(strBit2);
jTextField10.setText(strBit3);
}
每当我调用此函数时,SQL连接都会建立,但在某些时候会被某些我看不到的东西打断。