Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- *
- */
- package dbTest;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- /**
- * @author mn
- *
- */
- public class DbFtg1 {
- static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
- static final String DB_URL = "jdbc:mysql://java.bx.nimell.se:33306/javadb";
- // Database credentials
- static final String USER = "coffee";
- static final String PASS = "hunter12";
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Connection conn = null;
- Statement stmt = null;
- try {
- // Register JDBC driver
- Class.forName(JDBC_DRIVER);
- // Open a connection
- System.out.println("Connecting to database...");
- conn = DriverManager.getConnection(DB_URL, USER, PASS);
- stmt = conn.createStatement();
- String sql = "SELECT id, first, last, salary FROM Employees";
- ResultSet result = stmt.executeQuery(sql);
- // Show result
- while (result.next()) {
- // Retrieve by column name
- int id = result.getInt("id");
- int age = result.getInt("salary");
- String first = result.getString("first");
- String last = result.getString("last");
- // Display values
- System.out.print("ID: " + id);
- System.out.print("\t Pay: " + age);
- System.out.print("\tFirst: " + first);
- System.out.println("\tLast: " + last);
- }
- // Clean-up
- result.close();
- stmt.close();
- conn.close();
- } catch (Exception e) {
- System.out.println("Something went wrong");
- }
- }
- }
Add Comment
Please, Sign In to add comment