How can I connect my database to my application ?

How can I connect my database to my application ?

How can I connect my database to my application?

View Answers

October 21, 2010 at 2:20 PM

Hi,

You can use JDBC API to connect to database from your Java application.

Here is the sample code to connect to database:

import java.sql.*;

public class ConnetToDatabase {
  public static void main(String[] args) {
    try {
      Class.forName("com.mysql.jdbc.Driver").newInstance();
      Connection conn = DriverManager.getconnnection(
          "jdbc:mysql://localhost:3306/test", "root", "root");
      Statement statement = conn.createStatement();
      DatabaseMetaData meta = conn.getMetaData();
      ResultSet rs = meta.getTables(null, null, "%", null);
      String tableNames = "";
      while (rs.next()) {
        tableNames = rs.getString(3);
        System.out.println(tableNames);
      }
    } catch (Exception e) {
    }
  }
}

Read more at JDBC Tutorials Thanks









Related Tutorials/Questions & Answers:

Ads