《flex与Java整合完整的增删改查》由会员分享,可在线阅读,更多相关《flex与Java整合完整的增删改查(9页珍藏版)》请在金锄头文库上搜索。
1、flex 与 Java 整合( 完整的增删改查 )连接数据库: package com.db;import java.sql.*;public class DBUtlie private static final String DRIVER=com.microsoft.sqlserver.jdbc.SQLServerDriver;private static final String URL=jdbc:sqlserver:/127.0.0.1:1433;databaseName=mytest;private static final String USER=sa;private static
2、final String PWD=123;private Connection con=null;public Connection getConnection()tryClass.forName(DRIVER); con=DriverManager.getConnection(URL,USER,PWD); catch(Exception e)e.printStackTrace();return con;public static void close(Connection con, PreparedStatement ps, ResultSet rs) try if (rs != null)
3、 rs.close(); if (ps != null) ps.close(); if (con != null) con.close(); catch (SQLException se) se.printStackTrace();实体类:package com.pojo;public class Student private int sid;private String sname;private String ssex;private String saddress;private String sremark;public int getSid() return sid;public
4、void setSid(int sid) this.sid = sid;public String getSname() return sname;public void setSname(String sname) this.sname = sname;public String getSsex() return ssex;public void setSsex(String ssex) this.ssex = ssex;public String getSaddress() return saddress;public void setSaddress(String saddress) t
5、his.saddress = saddress;public String getSremark() return sremark;public void setSremark(String sremark) this.sremark = sremark;dao 类:package com.dao;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import
6、com.db.DBUtlie;import com.pojo.Student;public class StudentDao private Connection con = null; private PreparedStatement ps; private ResultSet rs;/ 查询所有public Student getStudent()Student stuList=null; ArrayList list=null;trylist=new ArrayList();con = new DBUtlie().getConnection(); String sql=select *
7、 from student; ps=con.prepareStatement(sql); rs=ps.executeQuery(); while(rs.next()Student student=new Student(); student.setSid(rs.getInt(sid);student.setSname(rs.getString(sname); student.setSsex(rs.getString(ssex); student.setSaddress(rs.getString(saddress); student.setSremark(rs.getString(sremark
8、); list.add(student);stuList=new Studentlist.size();for(int i=0;istuList.length;i+)stuListi=list.get(i);catch(Exception e)e.printStackTrace();finally try rs.close();ps.close();con.close(); catch (SQLException e) e.printStackTrace();return stuList;/ 查找单个对象public Student findStudent(Student stu)Studen
9、t student=null;trycon = new DBUtlie().getConnection();String sql=select * from student where sid=?; ps=con.prepareStatement(sql);ps.setInt(1, stu.getSid(); rs=ps.executeQuery();while(rs.next()student=new Student(); student.setSid(rs.getInt(sid); student.setSname(rs.getString(sname); student.setSsex(
10、rs.getString(ssex); student.setSaddress(rs.getString(saddress); student.setSremark(rs.getString(sremark);catch(Exception e)e.printStackTrace();finally try rs.close(); ps.close(); con.close(); catch (SQLException e) e.printStackTrace();return student;/ 添加 public void addStudent(Student stu) tryString
11、 sql=insert into student values(?,?,?,?); con=new DBUtlie().getConnection(); ps=con.prepareStatement(sql);ps.setString(1, stu.getSname();ps.setString(2, stu.getSsex();ps.setString(3, stu.getSaddress();ps.setString(4, stu.getSremark(); ps.executeUpdate();catch(Exception e) e.printStackTrace();finally
12、 try ps.close();con.close(); catch (SQLException e) e.printStackTrace();/ 修改public void updateStudent(Student stu) tryString sql=update student set sname=?,ssex=?,saddress=?,sremark=? where sid=?;con=new DBUtlie().getConnection(); ps=con.prepareStatement(sql);ps.setString(1, stu.getSname();ps.setStr
13、ing(2, stu.getSsex();ps.setString(3, stu.getSaddress();ps.setString(4, stu.getSremark(); ps.setInt(5, stu.getSid(); ps.executeUpdate();catch(Exception e) e.printStackTrace();finally try ps.close(); con.close(); catch (SQLException e) e.printStackTrace();/ 删除public void deleteStudent(Student stu) try
14、String sql=delete from student where sid=?; con=new DBUtlie().getConnection(); ps=con.prepareStatement(sql);ps.setInt(1, stu.getSid(); ps.executeUpdate(); catch(Exception e) e.printStackTrace(); finally try ps.close(); con.close(); catch (SQLException e) e.printStackTrace();flex: student.as: package com.studentRemoteClass(alias=com.pojo.Student)public class Studentpublic function Student()public var sid:int;public var sname:String;public var ssex:String;public var saddress:String;public var sremark:String;remoting-config.xml:?xml v