Home Answers Viewqa Java-Beginners How can I connect my database to my application ?

 
 


Nitin Sapra
How can I connect my database to my application ?
1 Answer(s)      2 years and 7 months ago
Posted in : Java Beginners

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 Pages:

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.