Advertisement
Guest User

Untitled

a guest
Oct 26th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class MysqlTest {
  4.  
  5.     public static void main(String[] args) {
  6.         String dbName = "customer";
  7.         String dbUser = "root";
  8.         String dbPasswd = "cccc!";
  9.         String dbRow = "customer";
  10.  
  11.         getRows(dbName, dbRow, dbUser, dbPasswd);
  12.  
  13.     }
  14.  
  15.  
  16.  
  17.     public static void getRows(String dbName, String dbRow, String dbUser, String dbPasswd) {
  18.         Connection conn = null;
  19.  
  20.         try {
  21.             //Class.forName("com.mysql.jdbc.Driver");
  22.             Class.forName("com.mysql.cj.jdbc.Driver");
  23.  
  24.  
  25.             conn = DriverManager.getConnection("jdbc:mysql://localhost/"+ dbName + "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC\n" ,dbUser, dbPasswd);
  26.             Statement sqlState = conn.createStatement();
  27.  
  28.             String selectStuff = "SELECT first_name FROM " + "customers";
  29.  
  30.             ResultSet rows = sqlState.executeQuery(selectStuff);
  31.  
  32.             while (rows.next()) {
  33.                 System.out.println(rows.getString(dbRow));
  34.             }
  35.  
  36.         } catch (SQLException ex) {
  37.             System.out.println("SQLException: " + ex.getMessage());
  38.             System.out.println("VendorError: " + ex.getErrorCode());
  39.  
  40.         } catch (ClassNotFoundException e) {
  41.             e.printStackTrace();
  42.         }
  43.  
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement