Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class App {
- public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
- public static final String JDBC_URL = "jdbc:mysql://localhost/sample";
- public static final String JDBC_USER = "root";
- public static final String JDBC_PASSWORD = "";
- public static void main (String[] args) throws SQLException, ClassNotFoundException {
- Statement statement = null;
- try{
- Class.forName(JDBC_DRIVER);
- Connection connection = DriverManager.getConnection(JDBC_URL, JDBC_USER, JDBC_PASSWORD);
- statement = connection.createStatement();
- ResultSet resultSet = statement.executeQuery("select id, firstname, lastname, email " +
- "from customer");
- System.out.println("First nametLast nametEmail");
- int count = 0;
- while (resultSet.next()){
- String firstname = resultSet.getString("firstname");
- String lastname = resultSet.getString("lastname");
- String email = resultSet.getString("email");
- System.out.printf("%st%st%s%n", firstname, lastname, email);
- count++;
- }
- System.out.println("--");
- System.out.println(MessageFormat.format("Rows: {0}", count));
- } finally {
- if(statement != null){
- statement.close();
- }
- }
- }
- }
- Connection to MySQL β sample@localhost failed: Exception in thread βmainβ java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
- at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
- at java.security.AccessController.doPrivileged(Native Method)
- at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
- at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
- at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
- at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
- at java.lang.Class.forName0(Native Method)
- at java.lang.Class.forName(Class.java:190)
- at com.intellij.persistence.database.console.RemoteJdbcServer.main(RemoteJdbcServer.java:15)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement