SQL2O Data Retrieve


Project Structure:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package coresql;

/**
 *
 * @author Pukar
 */
public class Users {
    private int userid;
    private String username;
    private String userpass;

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserpass() {
        return userpass;
    }

    public void setUserpass(String userpass) {
        this.userpass = userpass;
    }


}

--------------------------------------------

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package coresql;

import org.sql2o.Sql2o;
import java.sql.*;
import java.util.*;

/**
 *
 * @author Pukar
 */
public class UserDao {

    private Sql2o sql2o;

    public UserDao() {
        this.sql2o = new Sql2o("jdbc:mysql://localhost:3306/db_java", "root", "");
    }

    public List<Users> getAllTasks() {
        String sql = "SELECT userid, username, userpass FROM users";
        try (org.sql2o.Connection con = sql2o.open()) {
            return con.createQuery(sql).executeAndFetch(Users.class);
        }
    }
}
--------------------------------------------------
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package coresql;

import org.sql2o.Sql2o;

/**
 *
 * @author Pukar
 */
import java.util.*;
public class CoreSQL {

    public static void main(String[] args) {
        UserDao dao = new UserDao();
        List<Users> list = dao.getAllTasks();
        for(int i = 0; i<list.size();i++){
            System.out.println(list.get(i).getUserid()+" "+list.get(i).getUsername()+" "+list.get(i).getUserpass());
        }
    }
}




Share on Google Plus

About Ram Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment