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



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

Have Programming Question? Ask it here!
 
 
 


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


How can I connect my database to my application?


Advertisement
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:
Advertisements
 

 

 

DMCA.com