Advertisement
Guest User

Untitled

a guest
Apr 19th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. // Here's a basic Linux JDBC test program. M.Battig 4/6/2017
  2. // to start jGrasp in the lab, go to the terminal and type jgrasp.
  3. // Next, the following line is entered in jGrasp by clicking "Settings" then "PATH/CLASSPATH"
  4. // then "Workspace" then the "CLASSPATHS" tab, then "New"
  5. // /usr/share/java/mysql.jar
  6. // ALSO, you must have created your database on the SAME MACHINE (in my case, I called it "sampdb")
  7.  
  8.  
  9. import java.sql.*;
  10.  
  11. public class LinuxJdbc {
  12. public static void main(String[] args)
  13. throws SQLException, ClassNotFoundException {
  14. // Load the JDBC driver
  15. Class.forName("com.mysql.jdbc.Driver");
  16. System.out.println("Driver loaded");
  17.  
  18. // Establish a connection using "sampdb" as the DB created in MySql
  19. Connection connection = DriverManager.getConnection
  20. ("jdbc:mysql://localhost/sampdb", "root","cs304");
  21. System.out.println("Database connected");
  22.  
  23. // Create a statement
  24. Statement stmt = connection.createStatement();
  25.  
  26. // Select the columns from the Student table
  27. ResultSet rset = stmt.executeQuery
  28. ("select name, address, city from student where advisor "
  29. + " = 'Trono'");
  30.  
  31. // Iterate through the result and print the student names
  32. while (rset.next())
  33. System.out.println(rset.getString(1) + " " + rset.getString(2)
  34. + ". " + rset.getString(3));
  35.  
  36. // close the connection
  37. connection.close();
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement