Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Here's a basic Linux JDBC test program. M.Battig 4/6/2017
- // to start jGrasp in the lab, go to the terminal and type jgrasp.
- // Next, the following line is entered in jGrasp by clicking "Settings" then "PATH/CLASSPATH"
- // then "Workspace" then the "CLASSPATHS" tab, then "New"
- // /usr/share/java/mysql.jar
- // ALSO, you must have created your database on the SAME MACHINE (in my case, I called it "sampdb")
- import java.sql.*;
- public class LinuxJdbc {
- public static void main(String[] args)
- throws SQLException, ClassNotFoundException {
- // Load the JDBC driver
- Class.forName("com.mysql.jdbc.Driver");
- System.out.println("Driver loaded");
- // Establish a connection using "sampdb" as the DB created in MySql
- Connection connection = DriverManager.getConnection
- ("jdbc:mysql://localhost/sampdb", "root","cs304");
- System.out.println("Database connected");
- // Create a statement
- Statement stmt = connection.createStatement();
- // Select the columns from the Student table
- ResultSet rset = stmt.executeQuery
- ("select name, address, city from student where advisor "
- + " = 'Trono'");
- // Iterate through the result and print the student names
- while (rset.next())
- System.out.println(rset.getString(1) + " " + rset.getString(2)
- + ". " + rset.getString(3));
- // close the connection
- connection.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement