Now Connecting database with sourceCode Java. You can connecting database use driver from database as pasckage in library on this Java. The Driver connection get from vendor database such as Oracle, mySql and SqlSever etc.
you can code java such as :This file save with name ConnectionDatabase.java
package myPackage;
import java.io.*;
import java.sql.*;
interface InterfaceDatabase {
public void connectDatabase(String username, String password,
String oracleSID);
}
public class ConnectionDatabase implements InterfaceDatabase {
private String url;
public Connection conn;
public PreparedStatement pstm;
//Override from InterfaceDatabase
public void connectDatabase(String username, String password,
String DatabaseSID) {
try {
DriverManager.registerDriver(new driverDatabase..);
url = "String connection driver";
conn = DriverManager.getConnection(url, username, password);
} catch (SQLException ex) {
ex.printStackTrace();
if (conn != null) {
try {
conn.rollback();
} catch (SQLException el) {
el.printStackTrace();
}
}
}
}
public void disconnect() {
try {
pstm.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public void setQuery(String vsql) throws SQLException {
pstm = conn.prepareStatement(vsql);
}
}
call on file with file ConnUser.java
package myPackage;
import java.io.*;
import java.sql.*;
public class ConnUser {
public static void connUser(ConnectionDatabase cd,String vsql)throws Exception{
cd.connectDatabase("username", "password", "database");
cd.setQuery(vsql);
ResultSet rs=cd.pstm.executeQuery();
while(rs.next()){
System.out.println(rs.getInt(index)+" - "+rs.getString(index));
}
cd.disconnect();
}
public static void main (String[]args) throws Exception{
ConnectionDatabase db=new ConnectionDatabase();
connUser(db,"Query Database");
}
}
good luck to try it
Its nice articles..thanks and visit back me more
ReplyDelete