简体   繁体   中英

How do I translate a MySQL query's ResultSet into an integer?

I am not fluent in Java, and JavaScript.

What I am doing is working with a java plugin that allows me to add javaScript into a text file to be called upon on certain events. The JS in the text file can also call upon things inside of the Java Plugin itself giving it access to an API that I don't have documentation for.

What I need to do with my JS is query a MySQL database and get the User Id from a table. Here is my code:

var host = "localhost"
var port = "3306"
var database = "dev"
var user = "root"
var pass = ""
// var username = name of the user used to vote. Part of internal API

function connect(){
  var url = "jdbc:mysql://" + host + ":" + port + "/" + database
  return java.sql.DriverManager.getConnection(url, user, pass)
}
function getUserId(){
  var connection = connect()
  var statement = connection.createStatement()
  var userId = statement.executeQuery("SELECT user_id FROM xf_user_field_value WHERE field_value = '" + username + "'")
  return userId
}

What this returns is a resultSet. I need it to return is the integer value of the field.. not the result set.

How do I get an integer value from a ResultSet in JavaScript?

I am not familiar with this Java/Javascript plugin but it seems to follow the standard Java approach. It should be:

var statement = connection.createStatement()
statement.executeQuery("SELECT user_id FROM xf_user_field_value WHERE field_value = '" + username + "'")
var rs = s.getResultSet ();

if (rs.next()) {
    var userId = rs.getInt("user_id");
}

EDIT

In your case it seems to return a ResultSet already, so maybe:

var rs = statement.executeQuery("SELECT user_id FROM xf_user_field_value WHERE field_value = '" + username + "'")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM