日期:2014-05-16 浏览次数:20713 次
首先呢,一些基本配置这里就不作一一介绍了(可以去网上查查 很多的配置视频、文档,这里只是代码)
?
1)建立一个Java+Flex工程
?
2)新建java类
?
? 1.AddDataImpl.java
?
?
package com; import java.sql.*; import com.ConnectionFactory; public class AddDataImpl { ?String sql;? // 定义类型 ?Connection conn = null; ?Statement stmt = null; ?int rs; ?public AddDataImpl(){} ?public NoticeInfo[] addData(){? ?? try{ ??? conn = ConnectionFactory.getConnection(); ??? stmt = conn.createStatement(); ??? String sql = "insert into userinfo values ('11','11','11','1111-11-11') "; ??? rs=stmt.executeUpdate(sql); ??? stmt.close();??? ?? }catch(SQLException e){ ??? e.printStackTrace(); ?? } ?return null; ?} }
?
2.ConnectionFactory.java
?
package com;
import java.sql.*; ? public class ConnectionFactory { ?private static ConnectionFactory ref = new ConnectionFactory(); ? // 连接mysql数据库, database : test? user : root? password : 1272107226 ? private ConnectionFactory() ? { ?? try{ ??? Class.forName("com.mysql.jdbc.Driver"); ?? }catch(ClassNotFoundException e) ?? { ??? System.out.println("ERROR: exception loading driver class"); ?? } ? } ? public static Connection getConnection() throws SQLException{ ?? ?? String url = new String ("jdbc:mysql://localhost:3306/test?user=root&password=root"); ?? return DriverManager.getConnection(url); ? } ? ? public static void close(ResultSet rs) ? { ?? try{ ??? rs.close(); ?? }catch(Exception ignored){} ? } ? public static void close(Statement stmt) ? { ?? try{ ??? stmt.close(); ?? }catch(Exception ignored){} ? } ? public static void close(Connection conn) ? { ?? try{ ??? conn.close(); ?? }catch(Exception ignored){} ? }?? }
?3.DataServiceImpl.java
?
package com; import java.sql.*; import java.util.ArrayList; import com.ConnectionFactory; public class DataServiceImpl { String sql; // 定义类型 Connection conn = null; Statement stmt = null; ResultSet rs = null; public DataServiceImpl(){} public NoticeInfo[] getNotices(){ ArrayList noticeList = new ArrayList(); try{ conn = ConnectionFactory.getConnection(); stmt = conn.createStatement(); String sql = "select userid, username, contents, dates from userinfo "; rs = stmt.executeQuery(sql); while(rs.next()){ NoticeInfo temp = new NoticeInfo(); temp.setUserid(rs.getString("userid")); temp.setUsername(rs.getString("username")); temp.setContents(rs.getString("contents")); temp.setDates(rs.getString("dates")); noticeList.add(temp); } NoticeInfo[] notices = new NoticeInfo[noticeList.size()]; for(int i=0;i < noticeList.size();i++){ notices[i] = (NoticeInfo) noticeList.get(i); } return notices; }catch(SQLException e){ e.printStackTrace(); return null; } } }
?4.NoticeInfo.java
?
package com; public class NoticeInfo { private String userid; private String username; private String contents; private String dates; public String getContents() { return contents; } public void setContents(String contents) { this.contents = contents; } public String getDates() { return dates; } public void setDates(String dates) { this.dates = dates; } public String getUserid() { return userid; } public void setUserid(String userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
?
?
?
3)新建Flex页面
?
1.? AddDataText.mxml
?
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"