Searching...
Wednesday, October 7, 2015

JDBC Connecton

October 07, 2015
You need to add a Class Path reference in the manifest. Follow these simple steps:

Add a folder "lib" to your application

For MYSQL
Place "mysql-connector-java-5.1.18.jar " in lib


For MSSQL
Place " sqljdbc4-3.0.jar" in lib

For ORACLE
Place " ojdbc6.jar" in lib

In this way, whenever a runtime eclipse application / osgi application is started this jar file is exported along too. So the connectivity would then be available there too.


DBconnection.java

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

public class DBconnection {
public static Connection getConnection(){
Connection con = null;
try{
//Class.forName("oracle.jdbc.driver.OracleDriver");
//con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password");

//Class.forName("com.mysql.jdbc.Driver");
// con=DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","username","password");


Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con=DriverManager.getConnection("jdbc:sqlserver://localhost:53245;"+"databaseName=databasename;user=username;password=password");
       



}catch(SQLException e){
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}

return con;
}
}


Access Connection Object In Another Class In Java

TestConnection.java
import java.sql.Connection;

import connection.com.DBconnection;
public class TestConnection {
public static void main(String[] args) {
Connection con=DBconnection.getConnection();
System.out.print("Database Connected");

}

}

Output








0 comments:

Post a Comment

ads2