I made a class (OracleConnector2Test) that receives as parameter 2 values, makes a query in the base and returns the result of that query in the Eclipse console. In the class (OracleConnector2TestBuscaEstrategias) I instantiate the class (OracleConnector2Test), I pass the parameters ("179", "319") and have Eclipse display the result on the console screen. The values returned are correct, but in the Eclipse console the message:
Query Result: null.
I can not understand why the message (Query Result: null) if the query in the database returns 2 values (Facilities 179 and Consumer.GOV Clear TV 319).
Follow below the code in Java and Print Screen of Console Eclipse.
Class OracleConnector2Test
public class OracleConnector2Test {
String resultado;
public String returnDb(String Id_Estrategia1, String Id_Estrategia2) {
// Connection URL Syntax: "jdbc:mysql://ipaddress:portnumber/db_name"
String dbUrl = "jdbc:oracle:thin:@10.5.12.116:1521:desenv01";
// Database Username
String username = "bkofficeadm";
// Database Password
String password = "bkofficeadmdesenv01";
// Query to Execute
String query = "SELECT DS_ESTRATEGIA, ID_ESTRATEGIA" + " FROM TB_BKOFFICE_ESTRATEGIA"
+ " WHERE ID_ESTRATEGIA IN (" + Id_Estrategia1 + ", " + Id_Estrategia2 + ")";
try {
// Load mysql jdbc driver
Class.forName("oracle.jdbc.driver.OracleDriver");
// Create Connection to DB
Connection con = DriverManager.getConnection(dbUrl, username, password);
// Create Statement Object
Statement stmt = con.createStatement();
// Execute the SQL Query. Store results in ResultSet
ResultSet rs = stmt.executeQuery(query);
// While Loop to iterate through all data and print results
while (rs.next()) {
String DS_ESTRATEGIA = rs.getString(1);
String ID_ESTRATEGIA = rs.getString(2);
System.out.println(DS_ESTRATEGIA + " " + ID_ESTRATEGIA);
}
// closing DB Connection
con.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return resultado;
}
}
Class OracleConnector2TestBuscaEstrategias
public class OracleConnector2TestBuscaEstrategias {
public static void main(String[] args) {
String query;
OracleConnector2Test t = new OracleConnector2Test();
query = t.returnDb("179", "319");
System.out.println("Query Result: " + query);
}
}
you did String resultado;
then return resultado;
never assigning anything to it.
Assign the result to resultdao
.
something like this:
StringBuilder sb = new StringBuilder();
while (rs.next()) {
String DS_ESTRATEGIA = rs.getString(1);
String ID_ESTRATEGIA = rs.getString(2);
System.out.println(DS_ESTRATEGIA + " " + ID_ESTRATEGIA);
sb.append(DS_ESTRATEGIA + " " + ID_ESTRATEGIA + "\n");
}
resultdao = sb.toString();
Actually you'd be better off returning a List<String>
:
List<String> resultdao = new ArrayList<>();
public List<String> returnDb(String Id_Estrategia1, String Id_Estrategia2) {
...
while (rs.next()) {
String DS_ESTRATEGIA = rs.getString(1);
String ID_ESTRATEGIA = rs.getString(2);
System.out.println(DS_ESTRATEGIA + " " + ID_ESTRATEGIA);
resultdao.add(DS_ESTRATEGIA + " " + ID_ESTRATEGIA);
}
...
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.