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



Home Answers Viewqa JDBC Accessing database with JDBC via Java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Java Coder
Accessing database with JDBC via Java
1 Answer(s)      2 years and 7 months ago
Posted in : JDBC


How to access a database with JDBC via Java application?


Advertisement
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
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
Advertisements
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
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
Accessing Databases Using Java and JDBC
Accessing Database using Java and JDBC       Accessing Database using Java and JDBC Database plays... develop and example to access the database using Java and JDBC. For this, firstly
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
xml to html via java
xml to html via java  how to read xml into html using java code
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
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.
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: http://www.roseindia.net/xml/Listingnode.shtml http://www.roseindia.net/xml/getting
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
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
JDBC - Java Database Connectivity Tutorials  ...; New Features in JDBC 4.0 Java database connectivity (JDBC) is the Java... Java and JDBC Learn how to access database using JDBC.   Enhanced
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, Java Database Connectivity
tutorials. Java Database Connectivity or JDBC for short is Java bases API... driver as per your application needs. About JDBC Java Database Connectivity or JDBC for short is a standard API in Java for accessing and working
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
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
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
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
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
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
database - JDBC
database  hai friend, yes i want to use java database connection in eclipse IDE. Thanks in advance
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 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
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
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
Accessing database from JSP
Accessing database from JSP   ... Query or Accessing data from database: This is done using following code... In This article I am going to discuss the connectivity from MYSQL database with JSP.we
Connecting to the Database Using JDBC and Pure Java driver
Connecting to the Database JDBC Driver In our search... to the Database All the classes and interfaces of JDBC API are defined... engine our application communicate with database using JDBC
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
Advertisements
 

 

 

DMCA.com