Download import java.util.List - Datu bāzes tehnoloģijas

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
1
Grafiskās datu bāzes datu vizualizācija izmantojot Java
valodu
Lietojums
Datu
bāzes
vadības
sistēma
Java2D
(vizualizācija)
Datu bāzes sistēma
Datu bāze
SDO_GEOMETRY
JGEOMETRY
JDBC
interfeiss
2
Grafiskās datu bāzes datu glabāšanas struktūra
1. slāņa definēšana - celtnes
CREATE TABLE CELTNES (
C_NUM
NUMBER CONSTRAINT Ier_PA_Celtnes PRIMARY KEY,
GEOMETRIJA
MDSYS.SDO_GEOMETRY,
C_NOS
VARCHAR2(20) );
2. slāņa definēšana - ielas
CREATE TABLE IELAS (
I_NUM
NUMBER CONSTRAINT Ier_PA_Ielas PRIMARY KEY,
GEOMETRIJA
MDSYS.SDO_GEOMETRY,
I_NOS
VARCHAR2(20) );
3. slāņa definēšana - koki
CREATE TABLE KOKI (
K_NUM
NUMBER CONSTRAINT Ier_PA_Koki PRIMARY KEY,
GEOMETRIJA
MDSYS.SDO_GEOMETRY,
K_NOS
VARCHAR2(20) );
3
delete from USER_SDO_GEOM_METADATA where TABLE_NAME =
'CELTNES';
delete from USER_SDO_GEOM_METADATA where TABLE_NAME =
'IELAS';
delete from USER_SDO_GEOM_METADATA where TABLE_NAME =
'KOKI';
INSERT INTO USER_SDO_GEOM_METADATA(
TABLE_NAME, COLUMN_NAME, DIMINFO, SRID) VALUES('CELTNES',
'GEOMETRIJA',
MDSYS.SDO_DIM_ARRAY(MDSYS.SDO_DIM_ELEMENT('X', 0, 20, 0.05),
MDSYS.SDO_DIM_ELEMENT('Y', 0, 20, 0.05)), NULL);
INSERT INTO USER_SDO_GEOM_METADATA(
TABLE_NAME, COLUMN_NAME, DIMINFO, SRID) VALUES('IELAS',
'GEOMETRIJA',
MDSYS.SDO_DIM_ARRAY(MDSYS.SDO_DIM_ELEMENT('X', 0, 20, 0.05),
MDSYS.SDO_DIM_ELEMENT('Y', 0, 20, 0.05)), NULL);
INSERT INTO USER_SDO_GEOM_METADATA(
TABLE_NAME, COLUMN_NAME, DIMINFO, SRID) VALUES('KOKI',
'GEOMETRIJA',
MDSYS.SDO_DIM_ARRAY(MDSYS.SDO_DIM_ELEMENT('X', 0, 20, 0.05),
MDSYS.SDO_DIM_ELEMENT('Y', 0, 20, 0.05)), NULL);
4
Fiksētā indeksa veidošana:
CREATE INDEX IND_CELTNES ON CELTNES(GEOMETRIJA)
INDEXTYPE IS MDSYS.SPATIAL_INDEX PARAMETERS('SDO_LEVEL
= 4');
Hibrīda tipa indeksa veidošana:
CREATE INDEX IND_KOKI ON KOKI(GEOMETRIJA)
INDEXTYPE IS MDSYS.SPATIAL_INDEX
PARAMETERS('SDO_LEVEL = 4, SDO_NUMTILES =4');
R-tree koka indeksa veidošana:
CREATE
INDEX
IND_IELAS
ON
INDEXTYPE IS MDSYS.SPATIAL_INDEX;
IELAS(GEOMETRIJA)
5
In setting up JDK and Java applications, you will encounter these environment
variables: PATH, CLASSPATH, JAVA_HOME and JRE_HOME. In short:
1) PATH: maintains a list of directories. The OS searches the PATH entries for
executable programs, such as Java Compiler (javac) and Java Runtime (java).
2) CLASSPATH: maintain a list of directories (containing many Java class files) and
JAR file (a single-file archive of Java classes). The Java Compiler and Java Runtime
searches the CLASSPATH entries for Java classes referenced in your program.
3) JAVA_HOME and JRE_HOME: maintain the locations of JDK and JRE
installed directory, respectively.
Set the PATH environment variable if you want to be able to conveniently run the
executables (javac.exe, java.exe, javadoc.exe, and so on) from any directory without
having to type the full path of the command. If you do not set the PATH variable, you
need to specify the full path to the executable every time you run it, such as:
C:\Java\jdk1.7.0\bin\javac MyClass.java
The PATH environment variable is a series of directories separated by semicolons (;).
Microsoft Windows looks for programs in the PATH directories in order, from left to
right. You should have only one bin directory for the JDK in the path at a time (those
following the first are ignored), so if one is already present, you can update that
particular entry.
The following is an example of a PATH environment variable:
C:\Java\jdk1.7.0\bin;C:\Windows\System32\;C:\Windows\;C:\Windows\System
32\Wbem
The class path is the path that the Java Runtime Environment (JRE) searches for
classes and other resource files.
Class path entries can contain the base name wildcard character (*), which is
considered equivalent to specifying a list of all of the files in the directory with the
extension .jar or .JAR. For example, the class path entry mydir/* specifies all JAR
files in the directory named mydir. A class path entry consisting of * expands to a list
of all the jar files in the current directory. Files are considered regardless of whether
they are hidden (have names beginning with '.').
6
Nepieciešamo papildbibliotēku piesaiste
1. JGeometry tipa objektu klase:
oracle.spatial.geometry.JGeometry
F:\app\Janis\product\11.2.0\dbhome_1\md\jlib\*sdoapi.jar;
2. Oracle JDBC draivera klase:
oracle.jdbc.driver.OracleDriver
F:\app\Janis\product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar;
7
fails: dbc.java
// Nepieciešamo bibliotēku (pakešu) pievienošana
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import oracle.core.lmx.CoreException;
import oracle.spatial.geometry.JGeometry;
import oracle.sql.STRUCT;
// Klases dbc izveidošana, kura:
// 1) izveido metodi connect() savienojuma ar datu bāzes sistēmu
// izveidošanai;
// 2) izveido metodi getData() grafiskos datu (SDO_GEOMETRY)
// izgūšanai no datu bāzes un to transformācijai JGeometry tipa
// objektos;
// 3) kolekciju tipa objektu izveidošana.
public class dbc {
public dbc() { }
public void connect() {
String URL = "jdbc:oracle:thin:@localhost:1521:BAZE1";
String USER = "system";
String PASS = "Janis1946";
try { conn = DriverManager.getConnection(URL, USER, PASS); }
catch (SQLException e) { e.printStackTrace(); } }
// Anotācija brīdinājuma ziņojuma izvades aizliegšanai
@SuppressWarnings("deprecation")
// Datu izgūšana un transformēšana
public void getData() {
for (int i = 0; i < tblArray.length; i++) { try {
Statement stmt = conn.createStatement();
String sql = "SELECT a.geometrija geom FROM "+ tblArray[i] + " a";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
oracle.sql.STRUCT st = (STRUCT) rs.getObject("geom");
JGeometry geom = JGeometry.load(st);
result.add(geom); }
} catch (SQLException e) { e.printStackTrace(); } } }
//JGeometry tipa objektu kolekcijas saraksta (return) iegūšanas metodes
// izveidošana
public List<JGeometry> getJList(){ return result; }
// Savienojuma objekta conn izveidošana
private Connection conn;
8
// JGeometry objektu kolelkcijas saraksta (result) definēšana
private List<JGeometry> result = new ArrayList<JGeometry>(0);
// Grafisko datu slāņu tabulu nosaukumu ierakstīšana masīvā
private String[] tblArray = {"CELTNES", "IELAS", "KOKI" } }
9
fails: GraphWindow.java
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.util.List;
import javax.swing.JPanel;
import oracle.spatial.geometry.JGeometry;
import java.awt.geom.AffineTransform;
public class GraphWindow extends JPanel {
// Konstruktora metode
public GraphWindow(dbc dbcon) { this.dbcon = dbcon; }
// JGeometry grafisko objektu attēlošana
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// Koordināšu sistēmas transformācija.
// Attēlojuma lauka sākuma punkta novirze uz punktu X = 0, Y = 500.
AffineTransform tform = AffineTransform.getTranslateInstance(0, 500);
// Attēlojumu mēroga izmaiņa (reizes). Y ass būs vērsta uz augšu.
tform.scale(20, -20);
// Var izmantot arī koordināšu sistēmu pagriešanu par notektu leņķi.
// tform.rotate(Math.PI/2); // Pagriešana par 90 grādiem.
// JGeometry objektu kolekcijas saraksta iegūšana
List<JGeometry> lst = dbcon.getJList();
// JGeometry objektu attēlošana
for(int i=0; i<=lst.size()-1; i++){
if(lst.get(i) != null){ Shape a = lst.get(i).createShape(tform);
g2.draw(a); } } }
private dbc dbcon;
// Serializācijas versijas norāde, lai viena un tā pati versija tiktu
// izmantota gan serializācijā, gan deserializācijā.
private static final long serialVersionUID = 1L; }
10
fails: main.java
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import oracle.spatial.geometry.JGeometry;
public class main {
public final static void main(String[] args) {
// Jauna dbc klases objekta izveidošana
dbc dbcon = new dbc();
// Savienojuma ar datu bāzi izveidošana
dbcon.connect();
// Grafisko datu izgūšana no datu bāzes un transformācija
dbcon.getData();
// Grafisko datu kolekcijas saraksta iegūšana
List<JGeometry> lst = dbcon.getJList();
// Jauna GraphWindow tipa loga satura objekta (paneļa) izveidošana
JPanel panel = new GraphWindow(dbcon);
// Jauna Window loga (freima) izveidošana
JFrame frame = new JFrame("JavaGeometry");
// Loga satura izvade freimā
frame.setContentPane(panel);
// Freima izmēru norāde
frame.setSize(500, 500);
// Freima attēlošanas atļauja
frame.setVisible(true); } }
11
Paskaidrojumi
The java.util.List interface is a subtype of the java.util.Collection interface. It represents an
ordered list of objects, meaning you can access the elements of a List in a specific order, and by an
index too. You can also add the same element more than once to a List.
Being a Collection subtype all methods in the Collection interface are also available in the List
interface.
Since List is an interface you need to instantiate a concrete implementation of the interface in order
to use it. You can choose between the following List implementations in the Java Collections API:
1) java.util.ArrayList
2) java.util.LinkedList
3) java.util.Vector
4) java.util.Stack
Here are a few examples of how to create a List instance:
List listA = new ArrayList();
List listB = new LinkedList();
List listC = new Vector();
List listD = new Stack();
To add elements to a List you call its add() method. This method is inherited from the Collection
interface. Here are a few examples:
List listA = new ArrayList();
listA.add("element 1");
listA.add("element 2");
listA.add("element 3");
listA.add(0, "element 0");
The first three add() calls add a String instance to the end of the list. The last add() call adds a
String at index 0, meaning at the beginning of the list.
The order in which the elements are added to the List is stored, so you can access the elements in
the same order. You can do so using either the get(int index) method, or via the Iterator returned by
the iterator() method. Here is how:
List listA = new ArrayList();
listA.add("element 0");
listA.add("element 1");
listA.add("element 2");
//access via index
String element0 = listA.get(0);
String element1 = listA.get(1);
String element3 = listA.get(2);
//access via Iterator
Iterator iterator = listA.iterator();
while(iterator.hasNext(){ String element = (String) iterator.next();}
//access via new for-loop
for(Object object : listA) { String element = (String) object;}
When iterating the list via its Iterator or via the for-loop (which also uses the Iterator behind the
scene), the elements are iterated in the same sequence they are stored in the list.
12
13
You can remove elements in two ways:
1.
remove(Object element)
2.
remove(int index)
remove(Object element) removes that element in the list, if it is present. All subsequent elements in
the list are then moved up in the list. Their index thus decreases by 1.
remove(int index) removes the element at the given index. All subsequent elements in the list are
then moved up in the list. Their index thus decreases by 1.
Generic Lists
By default you can put any Object into a List, but from Java 5, Java Generics makes it possible to
limit the types of object you can insert into a List. Here is an example:
List<MyObject> list = new ArrayList<MyObject>();
This List can now only have MyObject instances inserted into it. You can then access and iterate its
elements without casting them. Here is how it looks:
MyObject myObject = list.get(0);
for(MyObject anObject : list){
//do someting to anObject...
}
The docs for java.io.Serializable are probably about as good an explanation as you'll get:
The serialization runtime associates with each serializable class a version number, called a
serialVersionUID, which is used during deserialization to verify that the sender and receiver of a
serialized object have loaded classes for that object that are compatible with respect to serialization.
If the receiver has loaded a class for the object that has a different serialVersionUID than that of the
corresponding sender's class, then deserialization will result in an InvalidClassException. A
serializable class can declare its own serialVersionUID explicitly by declaring a field named
"serialVersionUID" that must be static, final, and of type long:
ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime
will calculate a default serialVersionUID value for that class based on various aspects of the class,
as described in the Java(TM) Object Serialization Specification. However, it is strongly
recommended that all serializable classes explicitly declare serialVersionUID values, since the
default serialVersionUID computation is highly sensitive to class details that may vary depending
on compiler implementations, and can thus result in unexpected InvalidClassExceptions during
deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java
compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is
also strongly advised that explicit serialVersionUID declarations use the private modifier where
possible, since such declarations apply only to the immediately declaring class--serialVersionUID
fields are not useful as inherited members.