JDBC MySQL quick start


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. import java.util.logging.Level;
  2. import java.util.logging.Logger;
  3.  
  4. // import com.mysql.jdbc.*;
  5. import java.sql.*;
  6.  
  7. /*
  8.  * To change this template, choose Tools | Templates
  9.  * and open the template in the editor.
  10.  */
  11. /**
  12.  *
  13.  * @author sukanta
  14.  */
  15. public class Main {
  16.  
  17. public static void main(String[] args) throws SQLException {
  18. try {
  19. Class.forName("com.mysql.jdbc.Driver");
  20. } catch (ClassNotFoundException ex) {
  21. Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  22. }
  23. Connection con = null;
  24. Statement stmt = null;
  25. ResultSet rs = null;
  26.  
  27. con = DriverManager.getConnection("jdbc:mysql://desktop:3306/employees", "sukanta", "sukanta");
  28. stmt = con.createStatement();
  29. ResultSet result = stmt.executeQuery("SELECT * FROM table1");
  30. while (result.next())
  31. System.out.println(result.getString(1) + " " + result.getString(2));
  32. result.close();
  33. con.close();
  34. }
  35. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.