课程: Hands-On Introduction: Java

Connect to data source

- We have a SQLite database that's holding all of the data for our application. In order to read and write to this database, we need to first connect to it from our Java code. Fortunately, we can use the SQLite JDBC plugin to accomplish this. Let's create a new class under our bank package called Data Source. This class will handle connecting to the database as well as reading and writing from it. So let's make a static method called Connect, which will return the database connection. We can say public static. It'll return a connection object and we'll call this method "connect." We'll need to import the connection class from the Java dot SQL package. So let's just click on this connection, you'll see a little light bulb here, click this and they'll show you some ways that you can fix this error. The one we want is to import connection from Java dot SQL. Inside of the connect method, let's declare the path to our database file. If we look over here, as you can see, we have a bank dot db under the resources folder. This is our database file. So within our method, let's say string DB file and the path must be prefixed with jdbc colon, SQLite, colon, and then the path to the file. So we'll say resources, slash, bank dot db. Next, let's declare the connection object. So we'll see connection and we'll also call the object name connection. And we'll set this equal to driver manager, dot get connection and we'll pass in the DB file. Notice that the driver manager was automatically imported for me. If this didn't happen for you, be sure to add this import statement. Now notice, if we hover over the GET connection method, we have a compilation error saying that we have an unhandled exception. That's because the GET connection method throws an exception in the case of an error. If a method calls code that throws a runtime exception, that method is obligated to handle the exception or read through it. We can handle the exception by placing the call to get connection within what's called a try block. So let's go ahead and set this equal to null initially and then we'll create a try block. We can do so by writing the word try, followed by a set of curly braces. Inside of this try block, we're going to set the connection equal to this driver manager dot get connection. So, I'm just going to cut that and paste it there. In the event that this call happens to throw an exception, we can catch it with what's called a catch block. The catch block appears right after the try block. So we'll say catch and then inside of parentheses, we can specify the exception that we want to catch. In our case, it's a SQL exception and we'll name this exception E. Within the catch block, we can do anything we like to make the error a bit more graceful. In our case, let's just print the exception stack trace which will give us a log of the code's execution path thus far. So we can say E dot print stack trace. If no exception occurs, let's print a message saying that we're connected and then return that connection. So inside of the try block, after we've connected, let's just go ahead and do system dot out, dot print LN, which will print an output statement and we'll give us a little message, we're connected. Great. And then outside of the try catch block, after all is said and done, we can return our connection object. Awesome. Now let's test our connection by calling this method from the main method. The main method is the starting point of Java applications. It's the first method that is run when we execute our code. So the main method signature is public, static, void, main and inside of a set of parentheses, we can add an array of string, which we'll call args. Within here, let's make a call to connect because methods only execute when they are called. So we'll see. Connect a set of parentheses and then a semicolon. Notice we now have labels run in debug. So we can click run above the name and this will execute this class. And see our message is printed out to the console which means we're successfully connected to the database.

内容