Wednesday, 25 January 2017

JDBC Sample Program

package org.smartyy.smartiesgeeks;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DbConnection {


private static final Connection con = null;

public static Connection getConnect(){


String url="jdbc:oracle:thin:@localhost:1521:xe";
String Driver="oracle.jdbc.driver.OracleDriver";
String user="scott";
String pass="tiger";
try {
Class.forName(Driver);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

try {
Connection con = DriverManager.getConnection(url,user,pass);
System.out.println("connected");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
return con;



}
public static void main(String[] args) {
getConnect();
}

}

If your Oracle credentials are same that i used in my program then no need to change anything. But before execution of this program make sure that you have imported ojdbc14.jar file in your library folder.  Add JDBC Driver in Classpath or Build path in Eclipse Editor.
Enjoy JDBC 1st Application. Do comment if you have any doubt
Thanks 
SmartiesGeeks
Sunny

No comments:

Post a Comment

Make Top Rank Blog