Java: How do I retrieve a string from a full text search in mysql
Edit 4: the problem came from using the string name of the column instead of the column number as indicated below by nuzz
I've been searching all day and can't figure out how to get a full text search to return a string. It works fine when I type it directly into the mysql command prompt and also works fine if I just use SELECT username FROM user_info; but I get nothing with this.
String result = "";
String request = "SELECT username FROM user_info WHERE MATCH (username) AGAINST ('themanager');";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(request);
while (rs.next()) {
result += rs.getString("username") ;
result += "\n";
}
Edit 1: Here's the code for the table:
Create table user_info ( username varchar(16),
password varchar(16),
email varchar(16),
PRIMARY KEY(username,email));
ALTER table user_info add FULLTEXT(username,email);
Sorry I'm still new to this and have no clue how to copy the code from desc user_info without posting a print screen.
Edit 2: oops wrong code, same problem though.
Edit 3: Just figuered out how to copy the desc. The output gives me all the users in the database for SELECT username FROM user_info;. I haven't a clue about the specifics of my database system just in that its a default install of mysql.
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| username | varchar(16) | NO | PRI | | |
| password | varchar(16) | YES | | NULL | |
| email | varchar(50) | NO | PRI | | |
+-----------------+-------------+------+-----开发者_开发技巧+---------+-------+
Ok, this works for fulltext search. Heres what I ran. First I inserted into the database (via mysql console):
insert into user_info values('themanager', 'password', 'email@test.com');
Once I had that I ran:
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.junit.Test;
public class TestMysql {
private String dbUrl = "jdbc:mysql://localhost:3306/test";
private String user = "root";
private String password = "password";
private String dbClass = "com.mysql.jdbc.Driver";
private String query = "SELECT username, email FROM user_info WHERE MATCH (username, email) AGAINST ('themanager' IN BOOLEAN MODE)";
@Test
public void test() {
try {
Class.forName(dbClass);
Connection con = DriverManager.getConnection (dbUrl, user, password);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String username = rs.getString(1);
System.out.println("FOUND: '" + username + "'");
} //end while
con.close();
} //end try
catch(ClassNotFoundException e) {
e.printStackTrace();
}
catch(SQLException e) {
e.printStackTrace();
}
}
}
That printed: FOUND: 'themanager'
Try that. Let me know if it fails. If it does, it could well be a connection issue
see: http://www.petefreitag.com/item/477.cfm for more info about FULLTEXT querying
精彩评论