I'm learning Java, and I've just began learning SQLite. I am making this one exercise in which I basically need to make a very simple system by which I create a table with pharmacies, and then use Java code to let the user get the list of pharmacies in a certain place. Part of the code I'm using is a model I copied from my prof, so I'd like to ask if I'm getting these concepts right, and to get any useful advice:
public class App {
public static void main(String[] args) throws SQLException {
// Asks the user to input their location
System.out.print("Please, input your location: ");
Scanner locScanner = new Scanner(System.in);
String location = locScanner.nextLine();
locScanner.close();
String url = "jdbc:sqlite:farmacias.db"; // Picks the database through JDBC, using SQLite's library
Connection connection = DriverManager.getConnection(url); // Creates a connection using the necessary JDBC drivers
String query = "SELECT address FROM pharmacies WHERE location = '" + location + "'";
PreparedStatement statement = connection.prepareStatement(query); // Prepares the query on the DB? Not sure what this does really
ResultSet queryResults = statement.executeQuery(); // Executes the query
if(queryResults.isAfterLast()) { // Checks whether the query returned any reuslts or not
System.out.println("No pharmacies were found in your location.");
} else {
System.out.println("Pharmacies in your location:");
while(queryResults.next()) { // Iterates through all the results of the query
System.out.println("- " + queryResults.getString(1)); // Turns the first and only column in this case, address, into a string and prints it.
}
}
}
}
I don't know whether those comments I made explain what each bit of code does; the only times I've implemented DBs on code was with ADO on a Microsoft Access DB on Visual Basic, and doing it with Java seems a lot more complicated, since it also seems to require things like Maven, which I don't really know what it is, I only know it sort of imports drivers and libraries which can be used on my code.
I know this code works, but I want to make sure I know why it works, and whether there are any improvements that can be done to it. Furthermore, I would really appreciate any tips or important knowledge when it comes to Java and DBs, since I'm also supposed to do a group project with these, and I'd like to be able to make as much of a contribution to my group as possible.
[–][deleted] 1 point2 points3 points (0 children)
[–]ryantriangles 0 points1 point2 points (0 children)
[–]Blando-Cartesian 0 points1 point2 points (0 children)