Latest Tutorials| Questions and Answers|Ask Questions?|Site Map

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

View Questions and Answers by Category


Have Programming Question? Ask it here!

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

How can I connect my database to my application?

View Answers

October 21, 2010 at 2:20 PM


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 {
      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 ( {
        tableNames = rs.getString(3);
    } catch (Exception e) {

Read more at JDBC Tutorials Thanks

Related Tutorials/Questions & Answers: