Showing posts with label jdbc. Show all posts
Showing posts with label jdbc. Show all posts

Wednesday 3 July 2013

Connect To Oracle DB Via JDBC Driver

// siddhu vydyabhushana // 2 comments
Here is an example to show you how to connect to Oracle database via JDBC driver.
1. Download Oracle JDBC Driver
Get Oracle JDBC driver here – ojdbcxxx.jar

2. Java JDBC connection example

Code snippets to use JDBC to connect a Oracle database.
Class.forName("org.postgresql.Driver");
Connection connection = null;
connection = DriverManager.getConnection(
 "jdbc:oracle:thin:@localhost:1521:mkyong","username","password");
connection.close();
See a complete example below :
File : OracleJDBC.java
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
 
public class OracleJDBC {
 
 public static void main(String[] argv) {
 
  System.out.println("-------- Oracle JDBC Connection Testing ------");
 
  try {
 
   Class.forName("oracle.jdbc.driver.OracleDriver");
 
  } catch (ClassNotFoundException e) {
 
   System.out.println("Where is your Oracle JDBC Driver?");
   e.printStackTrace();
   return;
 
  }
 
  System.out.println("Oracle JDBC Driver Registered!");
 
  Connection connection = null;
 
  try {
 
   connection = DriverManager.getConnection(
     "jdbc:oracle:thin:@localhost:1521:mkyong", "username",
     "password");
 
  } catch (SQLException e) {
 
   System.out.println("Connection Failed! Check output console");
   e.printStackTrace();
   return;
 
  }
 
  if (connection != null) {
   System.out.println("You made it, take control your database now!");
  } else {
   System.out.println("Failed to make connection!");
  }
 }
 
}

2. Run it

Assume OracleJDBC.java is store in “C:\jdbc-test” folder, together with Oracle JDBC driver (ojdbc6.jar), then run following commands :
C:\jdbc-test>javac OracleJDBC.java
 
C:\jdbc-test>java -cp c:\jdbc-test\ojdbc6.jar;c:\jdbc-test OracleJDBC
-------- Oracle JDBC Connection Testing ------------
Oracle JDBC Driver Registered!
You made it, take control your database now!
 
C:\jdbc-test>
Done.
Read More

Connect To PostgreSQL With JDBC Driver

// siddhu vydyabhushana // 2 comments
Here is an example to show you how to connect to PostgreSQL database with JDBC driver.

1. Download PostgreSQL JDBC Driver

Get a PostgreSQL JDBC driver at this URL : http://jdbc.postgresql.org/download.html

2. Java JDBC connection example

Code snippets to use JDBC to connect a PostgreSQL database
Class.forName("org.postgresql.Driver");
Connection connection = null;
connection = DriverManager.getConnection(
   "jdbc:postgresql://hostname:port/dbname","username", "password");
connection.close();
See a complete example below :
File : JDBCExample.java
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
 
public class JDBCExample {
 
 public static void main(String[] argv) {
 
  System.out.println("-------- PostgreSQL "
    + "JDBC Connection Testing ------------");
 
  try {
 
   Class.forName("org.postgresql.Driver");
 
  } catch (ClassNotFoundException e) {
 
   System.out.println("Where is your PostgreSQL JDBC Driver? "
     + "Include in your library path!");
   e.printStackTrace();
   return;
 
  }
 
  System.out.println("PostgreSQL JDBC Driver Registered!");
 
  Connection connection = null;
 
  try {
 
   connection = DriverManager.getConnection(
     "jdbc:postgresql://127.0.0.1:5432/testdb", "mkyong",
     "123456");
 
  } catch (SQLException e) {
 
   System.out.println("Connection Failed! Check output console");
   e.printStackTrace();
   return;
 
  }
 
  if (connection != null) {
   System.out.println("You made it, take control your database now!");
  } else {
   System.out.println("Failed to make connection!");
  }
 }
 
}

3. Run it

Assume JDBCExample is store in c:\test folder, together with PostgreSQL JDBC driver, then run it :
C:\test>java -cp c:\test\postgresql-8.3-603.jdbc4.jar;c:\test JDBCExample
-------- MySQL JDBC Connection Testing ------------
PostgreSQL JDBC Driver Registered!
You made it, take control your database now!
Done
Read More

Connect To MySQL With JDBC Driver

// siddhu vydyabhushana // 4 comments
Here’s an example to show you how to connect to MySQL database via a JDBC driver. First, get a MySQL JDBC driver from here -MySQL JDBC Driver Download Here.

1. Java JDBC connection example

Code snippets to use a JDBC driver to connect a MySQL database.
Class.forName("com.mysql.jdbc.Driver");
Connection conn = null;
conn = DriverManager.getConnection("jdbc:mysql://hostname:port/dbname","username", "password");
conn.close();
See a complete example below :
JDBCExample.java
package com.mkyong.common;
 
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
 
public class JDBCExample {
 
  public static void main(String[] argv) {
 
 System.out.println("-------- MySQL JDBC Connection Testing ------------");
 
 try {
  Class.forName("com.mysql.jdbc.Driver");
 } catch (ClassNotFoundException e) {
  System.out.println("Where is your MySQL JDBC Driver?");
  e.printStackTrace();
  return;
 }
 
 System.out.println("MySQL JDBC Driver Registered!");
 Connection connection = null;
 
 try {
  connection = DriverManager
  .getConnection("jdbc:mysql://localhost:3306/mkyongcom","root", "password");
 
 } catch (SQLException e) {
  System.out.println("Connection Failed! Check output console");
  e.printStackTrace();
  return;
 }
 
 if (connection != null) {
  System.out.println("You made it, take control your database now!");
 } else {
  System.out.println("Failed to make connection!");
 }
  }
}

2. Run it

Assume JDBCExample.java is store in c:\test folder, along with the MySQL JDBC driver
C:\test>java -cp c:\test\mysql-connector-java-5.1.8-bin.jar;c:\test JDBCExample
-------- MySQL JDBC Connection Testing ------------
MySQL JDBC Driver Registered!
You made it, take control your database now!
 
C:\test>
P.S To run this example, your need mysql-connector-java-{version}-bin.jar in your classpath.
Done.
Read More

Saturday 1 December 2012

Insert Data into Database using Swings,Applets part-3

// siddhu vydyabhushana // 3 comments
this part-3 is followed by before 2 parts links are

PART-1

http://javatyro.blogspot.in/2012/12/insert-data-into-database-using.html

PART-2

http://javatyro.blogspot.in/2012/12/insert-data-into-database-using_1.html




DOWNLOAD FULL CODING
                                   download

DATABASE

DB




DATABASE CONNECTION WITHOUT DSN

try
{
String database="sample.mdb";//Here database exists in the current directory

//this connection is without DSN
String url="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + database + ";DriverID=22;READONLY=true";

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection(url);
PreparedStatement st=con.prepareStatement("insert into reg(username,password,gender,email) values(?,?,?,?)");
st.setString(1,name);
st.setString(2,pass);
st.setString(3,g);
st.setString(4,email);
st.executeUpdate();
JOptionPane.showMessageDialog(null,"Data is successfully inserted into database.");
}
catch( Exception ex){}
}
Read More

Insert Data into Database using Swings,Applets part-1

// siddhu vydyabhushana // 14 comments
Hi guys this is siddhu vydybhushana. Already i have blog (MY BLOG )but this time i concentrated on java so i create another blog and named JAVATYRO ..!!!!

I used  below topics to complete project

1.Swings
2.AWT
3.JDBC
4.Event Handling

Swings
swing is a part of JFC(java foundation classes).It gives smooth interface than AWT but we need to entire program in java.

JFC
java foundation classes are set of GUI(graphical user interface) components which simplifies the development of desktop applications..

AWT
adstract window toolkit used to develop GUI apps in java we can convert this to web applications also.

WHY EVENT HANDLING?
if we observe any change in state of an object we call it as EVENT . for example button pressing,mouse dragging,dialog showing .........etc
These events are provided by java.awt.event package
it will provide many event classes and listeners




now it is time to discuss about JDBC connection
already in my last post i blogged it please refer the below link to know that

                types-of-jdbc-drivers...

in next part i will go through on coding dont miss it ..

PART-2































Read More

Monday 26 November 2012

Types of JDBC Drivers

// siddhu vydyabhushana // Leave a Comment
my first post ON JDBC is 

JDBC TUTORIAL PART-1

there are four types of drivers in jdbc ,what is the use of 4 drivers, what type of driver we are going to use i will explain .


1)JDBC-ODBC bridge driver

    is used to connect to the database.the JDBC-ODBC bridge driver converts JDBC method calls into ODBC function calls.

2)NATIVE-API DRIVER

3)NETWORK PROTOCOL DRIVER


4)THIN DRIVER





Read More