Flex的数据基本操作(一)

想了解Flex有一段时间了,但因为对Java的不熟悉,迟迟找不到入门的感觉。近来比较忙,又常出差,再加上没有这一类的项目可做,比之前的未入门水平更有了退步。所以要写一个练手的东西,使用Flex、blazeds,用jtds连接SqlServer2005数据库,在使用过程中,有什么问题再说,开始干总比老是想要强一些。

数据库中只有两个表,一个表代表班级,另一表则保存相应的学生,在界面打开时,显示班级及学生所有数据,选择某一班级时,显示对应的学生。两个表使用外键关联。界面所提供的功能还有对学生的增删改的功能,班级的就不提供了,操作和学生相同。大概功能界面如下:

2010-6-12 14-56-43

数据库结构:

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[CLASSES]') AND type in (N'U'))

BEGIN

CREATE TABLE [dbo].[CLASSES](

	[ID] [int] IDENTITY(1,1) NOT NULL,

	[CLASSNAME] [nvarchar](20) NOT NULL,

	[REMARK] [nvarchar](100) NULL,

 CONSTRAINT [PK_CLASS] PRIMARY KEY CLUSTERED 

(

	[ID] ASC

)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

END

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[STUDENTS]') AND type in (N'U'))

BEGIN

CREATE TABLE [dbo].[STUDENTS](

	[ID] [int] IDENTITY(1,1) NOT NULL,

	[SNAME] [nvarchar](20) NOT NULL,

	[AGE] [int] NULL,

	[ADDRESS] [nvarchar](50) NULL,

	[CLASSID] [int] NOT NULL,

 CONSTRAINT [PK_STUDENTS] PRIMARY KEY CLUSTERED 

(

	[ID] ASC

)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]

) ON [PRIMARY]

END

GO

IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_STUDENTS_CLASS]') AND parent_object_id = OBJECT_ID(N'[dbo].[STUDENTS]'))

ALTER TABLE [dbo].[STUDENTS]  WITH CHECK ADD  CONSTRAINT [FK_STUDENTS_CLASS] FOREIGN KEY([CLASSID])

REFERENCES [dbo].[CLASSES] ([ID])

ON UPDATE CASCADE

ON DELETE CASCADE
 
Java与数据库的连接使用Hibernate操作,说实在话,也不太会用,只是为了图个少写代码,Hibernate.cfg.xml:
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-configuration PUBLIC

		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

    <session-factory name="MySession">

        <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>

        <property name="hibernate.connection.password">pwd</property>

        <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localhost/test</property>

        <property name="hibernate.connection.username">sa</property>

        <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>

		<property name="current_session_context_class">thread</property>

        

        <mapping resource="hibernate.cfg.xml"/>

        <mapping resource="com/stud/Classes.hbm.xml"/>

        <mapping resource="com/stud/Students.hbm.xml"/>

    </session-factory>

</hibernate-configuration>

文件结构:

2010-6-12 15-23-16 

生成的Hibernate映射文件分别对应了两个表:

 

Classes.hbm.xml内容:

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated 2010-6-11 13:58:04 by Hibernate Tools 3.2.4.GA -->

<hibernate-mapping>

    <class name="com.stud.Classes" table="CLASSES" schema="dbo" catalog="test">

        <id name="id" type="int">

            <column name="ID" />

            <generator class="identity" />

        </id>

        <property name="classname" type="string">

            <column name="CLASSNAME" length="20" not-null="true" />

        </property>

        <property name="remark" type="string">

            <column name="REMARK" length="100" />

        </property>

        <set name="studentses" inverse="true" lazy="false" table="STUDENTS" fetch="select">

            <key>

                <column name="CLASSID" not-null="true" />

            </key>

            <one-to-many class="com.stud.Students" />

        </set>

    </class>

</hibernate-mapping>
 
Students.hbm.xml内容:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<!-- Generated 2010-6-11 13:58:04 by Hibernate Tools 3.2.4.GA -->

<hibernate-mapping>

    <class name="com.stud.Students" table="STUDENTS" schema="dbo" catalog="test">

        <id name="id" type="int">

            <column name="ID" />

            <generator class="identity" />

        </id>

        <many-to-one name="classes" class="com.stud.Classes" fetch="select">

            <column name="CLASSID" not-null="true" />

        </many-to-one>

        <property name="sname" type="string">

            <column name="SNAME" length="20" not-null="true" />

        </property>

        <property name="age" type="java.lang.Integer">

            <column name="AGE" />

        </property>

        <property name="address" type="string">

            <column name="ADDRESS" length="50" />

        </property>

    </class>

</hibernate-mapping>

你可能感兴趣的:(Flex)