0

Is there a standard way to connect a Java program to a MySQL database, and which is the easiest one?

4 Answers 4

5

JDBC is the standard way also the easiest.

Sign up to request clarification or add additional context in comments.

Comments

0

In addition to answer posted above, JPA (Hibernate) is even easier one once you cross the initial barrier called learning curve (and before you are hit by next one called, Performance Optimization). On the serious note, Yes JPA is also a standard way to connect and query pretty much any database the same way.

Comments

0

JDBC is the standard way. Below is the sample java code to connect MySQL database:

  Connection con = null;
  String driver = "com.mysql.jdbc.Driver";
  String url = "jdbc:mysql://localhost:3306/";
  String db = "testdb";
  String dbUser = "root";
  String dbPasswd = "mysql123";
  try{
  Class.forName(driver);
  con = DriverManager.getConnection(url+db, dbUser, dbPasswd);
  try{
  Statement st = con.createStatement();
  String sql = "DELETE FROM user WHERE email = '[email protected]'";
  int delete = st.executeUpdate(sql);
  if(delete >= 1){
  System.out.println("Row is deleted.");
  }
  else{
  System.out.println("Row is not deleted.");
  }
  }
  catch (SQLException s){
  System.out.println("SQL statement is not executed!");
  }
  }
  catch (Exception e){
  e.printStackTrace();
  }

Comments

0

Maybe object-relational mapping with Hibernate is a useful way for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.