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.









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
What’s the difference between accessing a class method via -> and via ::?
What?s the difference between accessing a class method via -> and via ::?  What?s the difference between accessing a class method via -> and via
Accessing Database from servlets through JDBC!
Java Servlets - Downloading and Installation         Java Servlets are server... and reusability issues make it less than optimal solutions. Java Servlets changes
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
error in accessing database - JSP-Servlet
error in accessing database  hiiii im tanushri im tryng to connect my database to the servlet i hv succeeded in connectivity but im stuck to nother... database cud ne1 help me out plss  Hi friend, Code to help in solving
Accessing Databases Using Java and JDBC
Accessing Database using Java and JDBC       Accessing Database using Java and JDBC Database plays an important... and example to access the database using Java and JDBC. For this, firstly we need
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
java database connection - JDBC
java database connection  sir i want to join my project with MS access database. i am making my project in netbeans.please tell me the coding to do... the data into the MS Access database and retrieve the data from the database table
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... to package In new java package folder add a new name with extension .jdbc
java database error - JDBC
java database error  hi all i am writing one swing application where i want to insert data into database(MS-Access) but dont know how to do... the following code to insert the data into MS-Access database: import java.sql.
database connectivity in java - JDBC
database connectivity in java  import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.sql.*; class... con1; con1=DriverManager.getConnection("jdbc:odbc:Mycon
xml to html via java
xml to html via java  how to read xml into html using java code
JDBC, Java Database Connectivity
tutorials. Java Database Connectivity or JDBC for short is Java bases API... or download the free driver as per your application needs. About JDBC Java Database Connectivity or JDBC for short is a standard API in Java for accessing
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
JDBC - Java Database Connectivity Tutorial
;      Accessing Database using Java and JDBC Learn...JDBC - Java Database Connectivity Tutorials   ... 4.0 Java database connectivity (JDBC) is the Java Soft specification
accessing xml using java
accessing xml using java  I need to retrieve some elements in xml file using java   Hi Friend, Please visit the following links:ADS_TO_REPLACE_1 http://www.roseindia.net/xml/Listingnode.shtml http://www.roseindia.net
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
database - JDBC
database  hai friend, yes i want to use java database connection in eclipse IDE. Thanks in advance
Database Connection - JDBC
Database Connection  In java How will be connect Database through JDBC?  Hi Friend, Please visit the following link: http://www.roseindia.net/jdbc/jdbc-mysql/MysqlConnect.shtml Thanks
Database drivers - JDBC
. * Native-API, partly Java driver, also called Type 2. * JDBC-Net... Bridge Driver Native-API partly Java driver JDBC-Net pure Java driver Native... drivers: 1. Jdbc Odbc Driver: using this driver, the jdbc can be accessed via
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
database entry using jdbc
database entry using jdbc  Give me a program to enter records in table using jdbc in java.   Hi Friend, Please visit the following link:ADS_TO_REPLACE_1 Insert data into database Thanks
DataBase connectivity in java with Microsoft sql server2000 - JDBC
DataBase connectivity in java with Microsoft sql server2000  How i can achieve database connectivity with Microsoft sql server2000 in my java source code
about value taken in java database - JDBC
about value taken in java database  how to take value from Serial port communication Hyperterminal to the java database
Connecting to the Database Using JDBC and Pure Java driver
Connecting to the Database JDBC Driver In our search engine we are using MySQL database server and MM.MySQL Driver for connecting our application to the database. MM.MySQL Driver
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
database connectivity - JDBC
database connectivity  example java code for connecting Mysql database using java  Hi friend, Code for connecting Mysql database using...(); } } } For more information on JDBC in java visit to : http://www.roseindia.net/jdbc
Accessing database from JSP
Accessing database from JSP   ... or Accessing data from database: This is done using following code :- stmt... In This article I am going to discuss the connectivity from MYSQL database with JSP.we
JDBC (Java Database Connectivity) -Tutorials
JDBC (Java Database Connectivity) -Tutorials    Introduction to JDBC Java started... to a database server via a database driver. JDBC-ODBC Bridge - Open Database
database backup and restore using java - JDBC
database backup and restore using java  Hi... I have MS Access 2000 database.I want to backup and restore the db using jdbc.What's the procedure for that. Thanks in advance
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 collection of data. It is the easy way of accessing ,managing and updating
database connectivity - JDBC
database connectivity  can u ppl pls provide me with database... a user DSN 4. Select Microsoft Access Driver(*.mdb) 5. Select database name.... For programs,visit the following links: http://www.roseindia.net/jdbc/Jdbc-odbc
First Step towards JDBC!
JDBC - Java Database Connectivity Tutorials    Java Database Connectivity or in short JDBC  is a technology... rows.ADS_TO_REPLACE_5   JDBC (Java Database Connectivity
image store in database - JDBC
; Inserting Image in Database Table http://www.roseindia.net/jdbc/jdbc-mysql/insert... into the database what is the process to do that? Please explain... to store image into database. Check at http://www.roseindia.net/servlets/insert
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
Accessing the object information - Java Beginners
Accessing the object information  Authors.java import java.util.*; import jsns.model.AbstractAgent; import jsns.model.IAgentActor; import jsns.model.IAgentSensor; import jsns.model.IEnvironment; import
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
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
add record to database - JDBC
add record to database  How to create program in java that can save record in database ?  Hi friend, import java.io.*; import java.sql....(); String url = "jdbc:mysql://localhost:3306/"; String db = "register
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
database retrieving - JDBC
database retrieving  how to retrieve resultset from table which we have created in 'SQL SERVER MANAGEMENT STUDIO' using java program
Oracle Database error - JDBC
Oracle Database error   String query11 = "SELECT product_code, product_quantity, price FROM o"+orderid; ResultSet rs11... = DriverManager.getConnection("jdbc:mysql://localhost:3306/register", "root", "root"); Statement
Connecting JTable to database - JDBC
to store this JTable content in my database table.. This is a very important...("jdbc:odbc:access"); for(int i=0;i  Hi Friend, Make one change
insert arabic chacters in mysql via java - Java Interview Questions
insert arabic chacters in mysql via java  Dear, I have I java..."; static public final String connection = "jdbc:mysql...); System.out.println("Jdbc Mysql Connection Url
Help on JDBC and my SQL 5 database - JDBC
Help on JDBC and my SQL 5 database  Dear Sir, I had create a statement for the JDBC as shown below: try... the connection c = DriverManager.getConnection ("jdbc:mysql
JDBC Database URLs
JDBC Database URLs In this Section, We will discuss about following topics : JDBC Database URL specify a JDBC driver name Connection to a database table JDBC Database URL JDBC Uniform resource locater or 'JDBC URL'
cannot connect to database - JDBC
cannot connect to database  Iam using eclipse in my system ,when connecting the database mysql version 5.0 to the eclipse iam getting an error as ""Creating connection to mysql has encountered a problem.Could not connect to mysql
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... the following format: jdbc:mysql://[host][:port]/[database][?property1][=value1]...ADS

Ads