Java/XML/XML Database
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
XML Databases and Tools 1: read from clob
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class MainClass {
public static void main(String[] args) {
try {
Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM tableName");
while (rs.next()) {
int id = rs.getInt("ID");
InputStream bis = rs.getAsciiStream("MANUAL");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch = 0;
while ((ch = bis.read()) != -1)
bos.write(ch);
System.out.println("GAMEID: " + id + "\n" + "MANUAL: " + new String(bos.toByteArray()));
}
} catch (Throwable e) {
System.out.println("exception thrown");
System.out.println(e);
}
}
}
XML Databases and Tools 2: read from blob
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
public class MainClass {
public static void main(String[] args) throws Exception{
Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER");
conn.setAutoCommit(false);
Statement s = conn.createStatement();
s.executeUpdate("CREATE TABLE MANUALS(GAMEID INT, MANUAL LONG VARCHAR)");
conn.rumit();
File file = new File("manuals.xml");
InputStream is = new FileInputStream(file);
PreparedStatement ps = conn.prepareStatement("INSERT INTO MANUALS VALUES(?,?)");
ps.setInt(1, 1285757);
ps.setAsciiStream(2, is, (int) file.length());
ps.execute();
conn.rumit();
}
}
XML Databases and Tools 3
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import org.w3c.dom.Document;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
public class MainClass {
public static void main(String[] args) {
try {
Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER");
Statement s = conn.createStatement();
ResultSet rs = s.executeQuery("SELECT * FROM tableName");
while (rs.next()) {
int id = rs.getInt("GAMEID");
Document manual = (Document) rs.getObject("MANUAL");
XMLSerializer serialize = new XMLSerializer(System.out, null);
serialize.serialize(manual);
}
} catch (Throwable e) {
System.out.println("exception thrown");
System.out.println(e);
}
}
}
XML Databases DOM
import java.sql.*;
import java.io.*;
import org.apache.xerces.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class XMLDBDOM {
public static void main(String[] args) throws Exception{
Class.forName("COM.cloudscape.core.JDBCDriver").newInstance();
Connection conn = DriverManager.getConnection("jdbc:cloudscape:GAMETRADER");
conn.setAutoCommit(false);
Statement s = conn.createStatement();
s.executeUpdate("CREATE TABLE XMLData(GAMEID INT, MANUAL SERIALIZE(org.w3c.dom.Document))");
conn.rumit();
File file = new File("XMLData.xml");
InputStream is = new FileInputStream(file);
PreparedStatement ps = conn.prepareStatement("INSERT INTO XMLData VALUES(?,?)");
ps.setInt(1, 1285757);
DOMParser parser = new DOMParser();
parser.parse("XMLData.xml");
Document manual = parser.getDocument();
ps.setObject(2, manual);
ps.execute();
conn.rumit();
}
}