What's new

Java Java Unexpected Type assigning string error

SUNRAI

Eternal Poster
Joined
Feb 1, 2018
Posts
515
Reaction
59
Points
349
Age
23
Error in String p = id_col + " " = first_name + last_name + " " + job; line

package database_console; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; import java.sql.ResultSet; public class DBConnect { public static void main(String[] args) { try { String host = "jdbc:derby://localhost:1527/Employees"; String uName = "songalia"; String uPass = "relente1"; Connection con = DriverManager.getConnection(host, uName, uPass); Statement stmt = con.createStatement(); String sql = "SELECT * FROM Workers"; ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { int id_col = rs.getInt("ID"); String first_name = rs.getString("First_Name"); String last_name = rs.getString("Last_Name"); String job = rs.getString("Job_Title"); String p = id_col + " " = first_name + last_name + " " + job; System.out.println(p); } } catch( SQLException err) { System.out.println( err.getMessage()); } } }
 
The error in the code is on this line:

Code:
String p = id_col + " " = first_name + last_name + " " + job;

The equal sign (=) should be a plus sign (+). The corrected line should look like this:

Code:
String p = id_col + " " + first_name + last_name + " " + job;

This will concatenate the id_col, first_name, last_name, and job variables with spaces in between and store the result in the p variable.
 
Back
Top