locate jdbc | grep jarWhat's the full name of the .jar file? What is the complete path to the file?
Next, we need to include the .jar in the CLASSPATH, or places to look for the libraries or archives used by the program. To make sure that the CLASSPATH is set whenever you log on, you can set it in the .bash_profile, which is executed whenever you open a new terminal window. To do this, edit the .bash_profile and add the lines:
CLASSPATH=/usr/lib/pgsql/jdbc6.5-1.2.jar:.The `.' at the end of the CLASSPATH says to also look in the current directory for classes. The export statement means that the environment variable will continue to exist after the file has finished executing.
export CLASSPATH
To immediately put the changes in to effect that you entered in your .bash_profile, type at the command line:
source .bash_profileTo check that it was successful, print the environment variables, searching (or "grepping") for the ones that include the word PATH:
printenv | grep PATHYou should see two different path variables-- one for locating executable programs and one for loading classes into java.
Whenever you use SQL statements in your java program, you will have to include these two commands. Note that the forName method could throw a ClassNotFoundException and must be caught and handled.
Connection db = DriverManager.getConnection("jdbc:postgresql:myDB",Note that this method, as well as the ones in the next section, can throw SQLExceptions that must be caught and handled.
"testPerson", "pass");
Modify the java program from the previous section to connect to the database you created for Lab 2.
Statement st = db.createStatement();These statements could throw an SQLException that must be caught and handled.
ResultSet rs = st.executeQuery("select co_name from companies");
System.out.println("Company Name");
while(rs.next()) {
System.out.println(rs.getString(1));
}
rs.close();
st.close();
Modify your java program to print out the company names from the database.
st.executeUpdate("create table t(id int4, date timestamp);");For example, to insert new values into the table, you could use the following statements:
st.executeUpdate("insert into t values(3, now());");These statements could throw an SQLException that must be caught and handled.
st.executeUpdate("insert into t values(2, now());");
Write a java program that creates 2 tables:
Employees(ID, Name, Address, StartYear, StopYear, Salary)and adds in several employees and keeps track of the schools they attended. Type these by hand. In a future lab, we will load in employees automatically, using a Java progra.
Colleges(SchoolName, StudentName)
Write another program that prints out the number of years each employee was employed (that is, the difference between StartYear and StopYear).
Remember to pick up any files you sent to the printer and to take any personal belongings. It is difficult to get back into the lab later (since it's locked when not in use), so, it's worth making sure you haven't forgotten anything.