flex与java实现增删改查
发布时间:2020-12-15 01:33:01 所属栏目:百科 来源:网络整理
导读:flex与java实现增删改查 用的是MySQL数据库。 1,建一个userdb库,再建userinfo表,字 段:id(int),username(varchar),password(varchar)。 view plaincopy to clipboardprint? create database userdb; use userdb; create table userinfo( id int(10) not
flex与java实现增删改查 用的是MySQL数据库。 1,建一个userdb库,再建userinfo表,字 段:id(int),username(varchar),password(varchar)。 view plaincopy to clipboardprint? create database userdb; use userdb; create table userinfo( id int(10) not null auto_increment,username varchar(20),password varchar(20),primary key(id)); create database userdb; use userdb; create table userinfo( id int(10) not null auto_increment,primary key(id)); 2,DBConnection.java view plaincopy to clipboardprint? package com.datainfo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBConnection { public static Connection getConnection() throws ClassNotFoundException,SQLException { Connection conn = null; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/userdb"; String username = "root"; String password = "mysql"; Class.forName(driver); conn = DriverManager.getConnection(url,username,password); return conn; } } package com.datainfo; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class DBConnection { public static Connection getConnection() throws ClassNotFoundException,password); return conn; } } 3,User.java view plaincopy to clipboardprint? package com.datainfo; public class User { private int id; private String username; private String password; public User() { } /** * @return the id */ public int getId() { return id; } /** * @param id * the id to set */ public void setId(int id) { this.id = id; } /** * @return the username */ public String getUsername() { return username; } /** * @param username * the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the password */ public String getPassword() { return password; } /** * @param password * the password to set */ public void setPassword(String password) { this.password = password; } } package com.datainfo; public class User { private int id; private String username; private String password; public User() { } /** * @return the id */ public int getId() { return id; } /** * @param id * the id to set */ public void setId(int id) { this.id = id; } /** * @return the username */ public String getUsername() { return username; } /** * @param username * the username to set */ public void setUsername(String username) { this.username = username; } /** * @return the password */ public String getPassword() { return password; } /** * @param password * the password to set */ public void setPassword(String password) { this.password = password; } } 4,UserDAO.java view plaincopy to clipboardprint? package com.datainfo; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import com.datainfo.DBConnection; public class UserDAO { public ArrayList getUserList() throws ClassNotFoundException,SQLException { Connection conn = DBConnection.getConnection(); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select * from userinfo"); ArrayList userList = null; try { userList = new ArrayList(); while (rs.next()) { User user = new User(); user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); userList.add(user); } rs.close(); stmt.close(); conn.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return userList; } public void addUser(User user) throws ClassNotFoundException,SQLException { Connection conn = DBConnection.getConnection(); String sql = "insert into userinfo (username,password) values (?,?)"; try { PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,user.getUsername()); pstmt.setString(2,user.getPassword()); pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void updataUser(User user) throws ClassNotFoundException,SQLException { Connection conn = DBConnection.getConnection(); String sql = "update userinfo set username=?,password=? where id=?"; try { PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1,user.getPassword()); pstmt.setInt(3,user.getId()); pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public void deleteUser(User user) throws ClassNotFoundException,SQLException { Connection conn = DBConnection.getConnection(); String sql = "delete from userinfo where id =?"; try { PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setInt(1,user.getId()); pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package com.datainfo; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import com.datainfo.DBConnection; public class UserDAO { public ArrayList getUserList() throws ClassNotFoundException,user.getId()); pstmt.executeUpdate(); pstmt.close(); conn.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 5,配置文件remoting-config.xml view plaincopy to clipboardprint?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |