2. Akses ke database:
- Server yang memiliki driver Java dgn MYSQL atau SQLSERVER Setting untuk koneksinya seperti biasa (Terutama Server Hostingan Sendiri)
- Server yang tidak memiliki koneksi/driver ke database bisa dipakai cara berikut :
- untuk membaca data gunakan file XML yg di generate oleh PHP
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import javax.swing.*;
import javax.swing.table.*;
public class XMLReader {
public static String[][] a;
public static void main(String args[]) {
bukafile();
}
public static void bukafile(){
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("http://www.domain.com/File_Generate_XML.php");
doc.getDocumentElement().normalize();
System.out.println("Root element "+doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("barang");
System.out.println("Information seluruh");
a = new String[nodeLst.getLength()][5];
String[] columnNames = {"ID",
"Produk",
"Description",
"Status",
"Gambar"};
for (int s = 0; s < fstnode =" nodeLst.item(s);" fstelmnt =" (Element)" fstnmelmntlst =" fstElmnt.getElementsByTagName(namanya);" fstnmelmnt =" (Element)" fstnm =" fstNmElmnt.getChildNodes();">
- untuk menyimpan data gunakan file php dengan methode GET/POST
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
public class aksesPHP {
public String saveData(String perintah){
System.out.println(perintah);
String requestUrl = "http://www.domain.com/file_php.php?"+perintah;
try {
URL url = new URL(requestUrl.toString());
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
in.close();
return "Success";
} catch (IOException e) {
e.printStackTrace();
return "Failed";
}
}
public static void main(String[] args) {
}
}
Tidak ada komentar:
Posting Komentar