Accessing database with JDBC via Java

Accessing database with JDBC via Java

How to access a database with JDBC via Java application?

View Answers

May 10, 2012 at 11:52 AM

Accessing database with JDBC through Java

JDBC Example to access Database

import java.sql.*;

public class JdbcConnect{
  public static void main(String[] args) {
  System.out.println("JDBC Example.");
  Connection connnection = null;
  String url = "jdbc:mysql://localhost:3306/";
  String dbName = "student";
  String driver = "com.mysql.jdbc.Driver";
  String userName = "root"; 
  String password = "root";
  try {
  Class.forName(driver).newInstance();
  connnection  = DriverManager.getConnection(url+dbName,userName,password);
  System.out.println("Connected to the database");
  connnection .close();
  System.out.println("Disconnected from database");
  } catch (Exception e) {
  e.printStackTrace();
  }
  }
}

Connection: This is an interface in java.sql package that specifies connection with specific database like: MySQL, Ms-Access, Oracle etc and java files. The SQL statements are executed within the context of the Connection interface.

Class.forName(String driver): This method is static. It attempts to load the class and returns class instance and takes string type value (driver) after that matches class with given string.

DriverManager: It is a class of java.sql package that controls a set of JDBC drivers. Each driver has to be register with this class.

getConnection(String url, String userName, String password): This method establishes a connection to specified database url. It takes three string types of arguments like:

url: - Database url where stored or created your database

userName: - User name of MySQL

password: -Password of MySQL

con.close(): This method is used for disconnecting the connection. It frees all the resources occupied by the database.

printStackTrace(): The method is used to show error messages. If the connection is not established then exception is thrown and print the message.

Ads









Related Tutorials/Questions & Answers:
Accessing database with JDBC via Java
Accessing database with JDBC via Java  How to access a database with JDBC via Java application?   Accessing database with JDBC through Java JDBC Example to access Database import java.sql.*; public class JdbcConnect
Accessing Database from servlets through JDBC!
Access Database as backend and Sun's JDBC-ODBC bridge to connect to access... Accessing Access Database From Servlet    This article shows you how to access database from servlets
Advertisements
Accessing Database using EJB
Accessing Database using EJB       This is a simple EJB Application that access the database. Just go through the EJB example given below to find out the steps involved in accessing
Problem in accessing data from Database
Problem in accessing data from Database  hi..... i'm making a project on servlet and jsp with ms access 2007 at the backend. One field in my database is text and all others are of currency data type. If i enter 0 or null value
Video Tutorial: How to access MySQL through JDBC?
the Relational databases is called JDBC or Java Database connectivity. It can be used... JDBC Select a Database Go to Java EE-Eclipse page and select new... In new java package folder add a new name with extension .jdbc Select he new
Accessing Database using EJB
.style1 { color: #000000; } Accessing Database...; This is a simple EJB Application that access the database. Just go through the EJB example given below to find out the steps involved in accessing Database. Creating
xml to html via java
xml to html via java  how to read xml into html using java code
Export data in excel sheet via Browse and upload button into mysql database
Export data in excel sheet via Browse and upload button into mysql... the information from it in mysql database's Table Am using struts2,hibernate for making the Java web-application
JDBC tutorial with MySQL
of accessing MySQL database from Java program. Examples discussed here will help... Java program. JDBC Basics JDBC Classes and Interfaces JDBC Drivers...; Create JDBC Create Database JDBC Create Table JDBC Create Tables
Accessing database from JSP
Accessing database from JSP   ... presents JDBC and shows how you can manipulate data in a relational database from... In This article I am going to discuss the connectivity from MYSQL database with JSP.we
How to display data from database in a TableView via Hibernate
How to display data from database in a TableView via Hibernate  I need to populate a TableView (JavaFx) with database items. I designed the TableView... at this. The classes: This is the Class that creates the database data object
JDBC : Create Database Example
JDBC : Create Database Example In this section you will learn how to create database using JDBC with example. Create Database : Database is an organized... the following format: jdbc:mysql://[host][:port]/[database][?property1][=value1
JDBC access database
JDBC access database       JDBC is a Java Database Connectivity. The JDBC Connectivity provides API classes and interfaces for connecting the front end in Java application with database connections
image retrive into DataBase - JDBC
the image into DataBase. Thanks & Regards, VijayaBabu.M  Hi, Retrieving image from database is easy task. JDBC provides all the necessary API and function to retrieve the image from database. You retrieve image from database
JDBC Components
and retrieve results and updation to the database. The JDBC API is part of the Java... JDBC Components    JDBC stands for Java Database Connectivity. For connectivity with the database we uses JDBC
Accessing Ms access data in JAVA GUI Envi.
Accessing Ms access data in JAVA GUI Envi.  Q.How to insert data into ms access data in java using swing ? Q.How to slove (access denided ("java.lang.RuntimePermission" "accessClassInPackage.sun.jdbc.odbc")" these exception
A Java Program That Prints Out Result Via Printer
A Java Program That Prints Out Result Via Printer  A Java Program That Prints Out Result Through Printer Depending upon paper size , and print the calculated result on printer and save result as text file
JDBC: Select Database Example
JDBC: Select Database Example In this section, we will discuss how to select database using JDBC with example. Select Database : JDBC API provides... database url. The MySQL connection URL has the following format: jdbc:mysql://[host
JDBC: Drop Database Example
JDBC: Drop Database Example In this section, we are using JDBC API to drop... connection URL has the following format: jdbc:mysql://[host][:port]/[database... to the database, using jdbc API. Write query of drop database and call executeUpdate(sql
REQ for connection b/w jdbc and oracle database
REQ for connection b/w jdbc and oracle database    REQ for connection b/w jdbc and oracle database    The Java classes to connect to Oracle are contained in the Oracle JDBC driver jar file. For recent releases
code to establish jdbc database connectivity in jsp
code to establish jdbc database connectivity in jsp  Dear sir, i'm in need of code and procedure to establish jdbc connectivity in jsp
code to establish jdbc database connectivity in jsp
code to establish jdbc database connectivity in jsp  Dear sir, i'm in need of code and procedure to establish jdbc connectivity in jsp
Accessing Database from servlets through JDBC!
what is ment by jdbc and how to connect with database?
what is ment by jdbc and how to connect with database?  i want answer for this question
Understanding the JDBC Architecture
; databases. Type 3 JDBC Architecture Type 4 Java to Database... databases. JDBC is a core part of the Java platform and is included in the standard JDK... in a consistent, database-independent manner. JDBC provides rich, object-oriented
JDBC Drop Database Example
JDBC Drop Database Example: In this tutorial, you can learn how  drop...{ // JDBC driver name and database URL static String driverName... DropDatabase.java F:\jdbc>java DropDatabase DATABASE deleted
JDBC Video Tutorial- How to insert data into MySQL database table?
JDBC Video tutorial example for inserting data into MySQL database JDBC.... You have to change the JDBC driver for that particular database and the program... to connect to the MySQL database The Statement class of the JDBC api
JDBC
for Java Data Base Connectivity that defines how a client may access a database... and provides methods for querying and updating data in a database. The Java Database Connectivity (JDBC) API is the industry standard for database-independent
Application reading data from JDBC database
" should read data from from a JDBC database named PrimeGame with TABLE PRIMEGAMETABLE...Application reading data from JDBC database  I would like to develop... of the game. Upon clicking "I AGREE" button the user's details are written to a Java
Accessing non-static members through the main method in Java.
Accessing non-static members through the main method in Java.  As an oop rule, a static method can have access only to static variables and static... in Java has access to non-static members (variables or methods) even though
JDBC Video tutorial - How to update Data in a MySQL Database?
JDBC Beginners tutorial for updating the data into MySQL database The Java Database Connectivity API (JDBC) provides rich set of classes, interfaces... connects to the MySQL database and returns the JDBC Connection object to the Java

Ads