import java.sql.Connection; import java.sql.DriverManager; public class TestConnection { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); System.out.println("Loaded driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mysql?user=root&password=srikanth"); System.out.println("Connected to MySQL"); con.close(); } catch (Exception ex) { ex.printStackTrace(); } } }
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class BooksList { public static void main(String[] args) { try { Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost/mysql?user=root&password=srikanth"); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from books"); while(rs.next()) { System.out.println( rs.getString("title")); } st.close(); rs.close(); con.close(); } catch (Exception ex) { ex.printStackTrace(); } } }