private static void doSQLProcess(String phr){
int i, j, k;
int T;
ResultSet rs_current = null;
PreparedStatement pstm1 = null;
PreparedStatement pstm2 = null;
PreparedStatement pstm3 = null;
PreparedStatement pstm4 = null;
PreparedStatement pstm5 = null;
Connection con1 = null;
Connection con2 = null;
String WordInSen_given = new String();
String WordInSen_after = new String();
int phraseLen;
int WordInDataLen_given;
int WordInDataLen_after;
//connect database
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DATABASE_URL = "jdbc:mysql://localhost/wordbase";
try{
Class.forName(JDBC_DRIVER);
con1 = DriverManager.getConnection(DATABASE_URL, "root", "root");
pstm1 = con1.prepareStatement("SELECT CharNoString FROM worddatas WHERE CharNoString = ?");
//pstm2 = con1.prepareStatement();
con2 = DriverManager.getConnection(DATABASE_URL, "root", "root");
pstm3 = con2.prepareStatement("SELECT * FROM Probability WHERE G_NoString = ? AND A_NoString = ?");
pstm4 = con2.prepareStatement("UPDATE Probability SET Times = ? WHERE G_NoString = ? AND A_NoString = ?");
pstm5 = con2.prepareStatement("INSERT INTO Probability (G_NoString, A_NoString, Times) VALUES (?, ?, ?)");
System.out.println("connection success");
}
catch(ClassNotFoundException e){
System.out.println("class not found");
System.exit(1);
}
catch(SQLException e){
System.out.println("SQL exception in connection:"+e);
System.exit(1);
}
try{
phraseLen = phr.length();
i = 0;
while(i < phraseLen){
for(j=10; j>0; j--){
if(j >= (phraseLen - i) / 4 - 1){
System.out.println("j too long");
continue;
}
else{
WordInSen_given = phr.substring(i, i+j*4);
pstm1.clearParameters();
pstm1.setString(1, WordInSen_given);
rs_current = pstm1.executeQuery();
rs_current.first();
if(rs_current.getRow() != 0){
System.out.println("a given word found");
for(k=10; k>0; k--){
if(k>phraseLen-(i+j*4)){
System.out.println("k too long");
continue;
}
else{
WordInSen_after = phr.substring(i+j*4, i+j*4+k*4);
pstm1.clearParameters();
pstm1.setString(1, WordInSen_after);
rs_current = pstm1.executeQuery();
rs_current.first();
if(rs_current.getRow() != 0){
System.out.println("an after word found");
pstm3.clearParameters();
pstm3.setString(1, WordInSen_given);
pstm3.setString(2, WordInSen_after);
rs_current = pstm3.executeQuery();
rs_current.first();
if(rs_current.getRow() != 0){
System.out.println("pair exist");
T = rs_current.getInt(3) + 1;
pstm4.clearParameters();
pstm4.setInt(1, T);
pstm4.setString(2, WordInSen_given);
pstm4.setString(3, WordInSen_after);
pstm4.execute();
}
else{
System.out.println("pair not exist");
pstm5.clearParameters();
pstm5.setString(1, WordInSen_given);
pstm5.setString(2, WordInSen_after);
pstm5.setInt(3, 1);
pstm5.execute();
}
i = i + j*4 + k*4;
break;
}
}
}
if(k == 0){
System.out.println(" after word can't find");
i = i + j*4;
}
else{
System.out.println("found a pair and its process completed");
break;
}
}
}
}
if(j == 0)i = i + 4;
System.out.println(i);
}
}
catch(SQLException e){
System.out.println(e);
}
return;
}这个程序要要完成的任务是:在phr字符串中,每4个字符作为一组(下称4字符组),一个或多个4字符组都能构成一个词组,但是词组最多只能包含10个4字符组。同时,在数据表worddatas中每一个记录就是一个词组。找出在phr字符串中有多少对满足下面条件的词组:1)在phr中的位置是相邻的,2)两个词组都是worddatas中的记录,3)如果一个较长的词组包含别的较短的词组,则只考虑长的,忽略短的。
我的方法是:首先,截取一个given词组(从10个4字符组开始,然后在递减),到数据库里找,如果找到,截取它后面的在去找,如果又找到,则找到一队满足条件的词组对。我的程序在if(rs_current.getRow() != 0)这个地方,无论条件满足与否,他都不进去,我找了半天不知道为什么,请各为高手帮帮忙啊!
int i, j, k;
int T;
ResultSet rs_current = null;
PreparedStatement pstm1 = null;
PreparedStatement pstm2 = null;
PreparedStatement pstm3 = null;
PreparedStatement pstm4 = null;
PreparedStatement pstm5 = null;
Connection con1 = null;
Connection con2 = null;
String WordInSen_given = new String();
String WordInSen_after = new String();
int phraseLen;
int WordInDataLen_given;
int WordInDataLen_after;
//connect database
String JDBC_DRIVER = "com.mysql.jdbc.Driver";
String DATABASE_URL = "jdbc:mysql://localhost/wordbase";
try{
Class.forName(JDBC_DRIVER);
con1 = DriverManager.getConnection(DATABASE_URL, "root", "root");
pstm1 = con1.prepareStatement("SELECT CharNoString FROM worddatas WHERE CharNoString = ?");
//pstm2 = con1.prepareStatement();
con2 = DriverManager.getConnection(DATABASE_URL, "root", "root");
pstm3 = con2.prepareStatement("SELECT * FROM Probability WHERE G_NoString = ? AND A_NoString = ?");
pstm4 = con2.prepareStatement("UPDATE Probability SET Times = ? WHERE G_NoString = ? AND A_NoString = ?");
pstm5 = con2.prepareStatement("INSERT INTO Probability (G_NoString, A_NoString, Times) VALUES (?, ?, ?)");
System.out.println("connection success");
}
catch(ClassNotFoundException e){
System.out.println("class not found");
System.exit(1);
}
catch(SQLException e){
System.out.println("SQL exception in connection:"+e);
System.exit(1);
}
try{
phraseLen = phr.length();
i = 0;
while(i < phraseLen){
for(j=10; j>0; j--){
if(j >= (phraseLen - i) / 4 - 1){
System.out.println("j too long");
continue;
}
else{
WordInSen_given = phr.substring(i, i+j*4);
pstm1.clearParameters();
pstm1.setString(1, WordInSen_given);
rs_current = pstm1.executeQuery();
rs_current.first();
if(rs_current.getRow() != 0){
System.out.println("a given word found");
for(k=10; k>0; k--){
if(k>phraseLen-(i+j*4)){
System.out.println("k too long");
continue;
}
else{
WordInSen_after = phr.substring(i+j*4, i+j*4+k*4);
pstm1.clearParameters();
pstm1.setString(1, WordInSen_after);
rs_current = pstm1.executeQuery();
rs_current.first();
if(rs_current.getRow() != 0){
System.out.println("an after word found");
pstm3.clearParameters();
pstm3.setString(1, WordInSen_given);
pstm3.setString(2, WordInSen_after);
rs_current = pstm3.executeQuery();
rs_current.first();
if(rs_current.getRow() != 0){
System.out.println("pair exist");
T = rs_current.getInt(3) + 1;
pstm4.clearParameters();
pstm4.setInt(1, T);
pstm4.setString(2, WordInSen_given);
pstm4.setString(3, WordInSen_after);
pstm4.execute();
}
else{
System.out.println("pair not exist");
pstm5.clearParameters();
pstm5.setString(1, WordInSen_given);
pstm5.setString(2, WordInSen_after);
pstm5.setInt(3, 1);
pstm5.execute();
}
i = i + j*4 + k*4;
break;
}
}
}
if(k == 0){
System.out.println(" after word can't find");
i = i + j*4;
}
else{
System.out.println("found a pair and its process completed");
break;
}
}
}
}
if(j == 0)i = i + 4;
System.out.println(i);
}
}
catch(SQLException e){
System.out.println(e);
}
return;
}这个程序要要完成的任务是:在phr字符串中,每4个字符作为一组(下称4字符组),一个或多个4字符组都能构成一个词组,但是词组最多只能包含10个4字符组。同时,在数据表worddatas中每一个记录就是一个词组。找出在phr字符串中有多少对满足下面条件的词组:1)在phr中的位置是相邻的,2)两个词组都是worddatas中的记录,3)如果一个较长的词组包含别的较短的词组,则只考虑长的,忽略短的。
我的方法是:首先,截取一个given词组(从10个4字符组开始,然后在递减),到数据库里找,如果找到,截取它后面的在去找,如果又找到,则找到一队满足条件的词组对。我的程序在if(rs_current.getRow() != 0)这个地方,无论条件满足与否,他都不进去,我找了半天不知道为什么,请各为高手帮帮忙啊!
你的worddatas中的数据是词组,所以不能用“=”应该写成:
"SELECT CharNoString FROM worddatas WHERE CharNoString like ?"然后,
pstm1.setString(1, "%" + WordInSen_after + "%");
其中"%"是通配符,我不知道MySql的通配符是不是这样,你试试吧