怎么用java和flex实现增删改查

 我来答
育知同创教育
2016-07-12 · 百度知道合伙人官方认证企业
育知同创教育
1【专注:Python+人工智能|Java大数据|HTML5培训】 2【免费提供名师直播课堂、公开课及视频教程】 3【地址:北京市昌平区三旗百汇物美大卖场2层,微信公众号:yuzhitc】
向TA提问
展开全部
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));
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;
}
}
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;
}
}
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.getUsername());
pstmt.setString(2, 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();
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式