-4

我已经编写了一个代码,它给了我数据库表的输出....现在我必须使用 oops 概念编写一个用户类,请提供我的代码。我的代码是

package com.glomindz.mercuri.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.glomindz.mercuri.util.MySingleTon;

public class UserServicesDAO {

private Connection connection;

public UserServicesDAO() {
    // connection = new MySingleTon().getConnection();
    connection = MySingleTon.getInstance().getConnection();

}

public List<Map<Integer, String>> get_all_data() {
    List<Map<Integer, String>> allRows = new ArrayList<Map<Integer, String>>();
    String query = "SELECT * FROM spl_user_master";
    try {
        PreparedStatement stmt = connection.prepareStatement(query);
        boolean execute = stmt.execute();
        System.out.println(execute);
        ResultSet resultSet = stmt.getResultSet();
        System.out.println(resultSet.getMetaData());
        while (resultSet.next()) {
            int id = resultSet.getInt("id");
            String Name = resultSet.getString("name");
            String email = resultSet.getString("email");
            String mobile = resultSet.getString("mobile");
            String password = resultSet.getString("password");
            String role = resultSet.getString("role");
            String status = resultSet.getString("status");
            String last_update = resultSet.getString("last_update");

            // print the results
            System.out.format("%s, %s, %s, %s, %s, %s, %s, %s\n", id, Name,
                    email, mobile, password, role, status, last_update);
        }
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return allRows;
}

public static void main(String[] args) {
    new UserServicesDAO().get_all_data();
}
}

我的数据库输出是

connection created
true
com.mysql.jdbc.ResultSetMetaData@538773
1, Glomindz Support, support@glomindz.com, 9854087006,          cbf91a71c11d5ec348b0c7e9b2f0055e, admin, 1, 2013-05-02 22:05:14
2, Amarjyoti Das, amarjyotidas@splcare.com, 9864092598,     88f2dccb02b2a20615211e5492f85204, admin, 1, 2013-04-26 05:44:41
4

1 回答 1

0

这是您可能需要的 User 类,根据您的需要进行修改:

public class User {

      private int id;

      private String  Name;

      private String  email;

      private String  mobile;

      private String  password;

      private String  role;

      private String  status;

      private String  last_update;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        Name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getLast_update() {
        return last_update;
    }

    public void setLast_update(String last_update) {
        this.last_update = last_update;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", Name=" + Name + ", email=" + email
                + ", mobile=" + mobile + ", password=" + password + ", role="
                + role + ", status=" + status + ", last_update=" + last_update
                + "]";
    }




}

在您的 JDBC 代码中,您将需要创建用户类对象的集合并将数据库结果存储在其中。像这样的东西:

List<User> usersList = new ArrayList<User>();
while (resultSet.next()) {
            User user  = new User();
            user.setId(resultSet.getInt("id"));
            user.setName(resultSet.getString("name"));
            user.setEmail(resultSet.getString("email"));
            user.setMobile(resultSet.getString("mobile"));
            user.setPassword(resultSet.getString("password"));
            user.setRole(resultSet.getString("role"));
            user.setStatus(resultSet.getString("status"));
            user.setLast_udpate(resultSet.getString("last_update"));

            // print the results
            System.out.println(user);
            usersList.add(user);
        }
于 2013-07-17T06:27:14.380 回答