à e có thay trong method layDanhsachDuLieu() cái return listMon; rồi nhưng vẫn ko đc. nó in mỗi dòng : “danh sach mon hoc:” thế thôi ạ
Dùng 1 đối tượng được tạo ra từ một lớp khác trong java như thế nào?
trong ham showDanhSachDuLieu phai co dang the nay
List a = layDanhSachDuLieu(), khong phai lam nhu em nha
e viết lại như này cũng ko đc anh à:
public static void showDanhSachDuLieu(){
List listMon =layDanhSachDuLieu();
System.out.println("\nDanh sach mon hoc: “);
for (int i = 0; i < listMon.size(); i++) {
System.out.printf(”\n%-4s %-5s %-5s\n", listMon.get(i).getIdMONHOC(),
listMon.get(i).getTenmon(),listMon.get(i).getMamon());
}
}
neu chung 1 class
vâng cùng 1 class mà
à e chạy đc rồi a, giờ e muốn viết 1 hàm update() thì có cách nào dùng đc cái list của hàm layDanhSachDuLieu() luôn ko hay vẫn phải viết 1 hàm i sì hàm layDanhSachDuLieu() ạ?
Viet ham rieng nha em
em co the su dung listmonhoc lam parameter voi du lieu da duoc set vao list de update
(sorry may anh ko co tieng viet)
anh @nguyenhuuca cái method lấy ra 1 môn e làm thế này sai ở đoạn nào mà ko chạy đc ạ?
public static TaiLieuHoc layMotMonHoc(int idMONHOC) {
List<TaiLieuHoc> listMon = layDanhSachDuLieu();
TaiLieuHoc tlh = new TaiLieuHoc();
String query = "select * from monhoc where idMONHOC= ?";
PreparedStatement pts = null;
try {
pts = connection.prepareStatement(query);
pts.setInt(1, idMONHOC);
listMon.add(tlh);
if (pts.executeUpdate() > 0) {
System.out.print("Get data success!");
} else {
System.out.print("Can't get data!");
}
} catch (SQLException e) {
System.out.print("error:get data");
}
return null;
}
Example:
String sql = “select * from people where firstname=? and lastname=?”;
PreparedStatement preparedStatement =
connection.prepareStatement(sql);
preparedStatement.setString(1, “John”);
preparedStatement.setString(2, “Smith”);
ResultSet result = preparedStatement.executeQuery();
e thấy cũng có khác gì cái của e đâu ạ?
em dang select sao lai update??
Nhưng e làm như của a cũng ko ra vì method có kiểu TaiLieuHoc mà mình trả về giá trị “result” nó ko cùng kiểu.
kieu Resultset. em lam nhu em lam truoc do ay
Nhưng e muốn để kiểu TaiLieuHoc thì làm sao anh? dưới là đoạn e code, biết là sai chỗ return nhưng ko biết làm sao để sd đc cả kiểu TaiLieuHoc và sử dụng cả List trong này?
public static TaiLieuHoc layMotMonHoc(int idMONHOC) {
List listMon = layDanhSachDuLieu();
TaiLieuHoc tlh=new TaiLieuHoc();
String query = “select * from monhoc where idMONHOC= ?”;
ResultSet rs = null;
try {
statement = connection.createStatement();
rs = statement.executeQuery(query);
while (rs.next()) {
tlh.setIdMONHOC(rs.getInt("idMONHOC"));
listMon.add(tlh);
}
}catch(Exception e){}
return tlh;
}
y em la add vao ca listMon va obj tlh
ý e là viết 1 method lấy 1 môn học từ database có kiểu TaiLieuHoc, kiểu như này :
public static TaiLieuHoc layMotMonHoc(int idMONHOC) {
return ;
}
lam nhu em dung roi ma, bi du cho listMon.add(tlh) thoi
Do lúc in ra cái môn đó e gọi tới method showDanhSachDuLieu() mà cái show này lại liên quan đến list nên …
package Connect;
import java.util.List;
import java.util.ArrayList;
import java.sql.*;
public class SampleApp {
private static Connection connection;
private static Statement statement;
public static List<TaiLieuHoc> layDanhSachDuLieu() {
List<TaiLieuHoc> listMon = new ArrayList<TaiLieuHoc>();
String query = "SELECT * FROM monhoc";
TaiLieuHoc tlh=null;
ResultSet rs = null;
try {
connection = ConnectHelper.connectToDataBase();
statement = connection.createStatement();
rs = statement.executeQuery(query);
while (rs.next()) {
tlh = new TaiLieuHoc();
tlh.setIdMONHOC(rs.getInt("idMONHOC"));
tlh.setTenmon(rs.getString("tenmon"));
tlh.setMamon(rs.getString("mamon"));
listMon.add(tlh);
}
}catch(Exception e){}
return listMon;
}
public static void showDanhSachDuLieu(){
List<TaiLieuHoc> listMon = layDanhSachDuLieu();
System.out.println("\nDanh sach mon hoc: ");
for (int i = 0; i < listMon.size(); i++) {
System.out.printf("\n%-4s %-5s %-5s\n", listMon.get(i).getIdMONHOC(),
listMon.get(i).getTenmon(),listMon.get(i).getMamon());
}
}
public static TaiLieuHoc layMotMonHoc(int idMONHOC) {
List<TaiLieuHoc> listMon = layDanhSachDuLieu();
TaiLieuHoc tlh=new TaiLieuHoc();
String query = "select * from monhoc where idMONHOC= ?";
ResultSet rs = null;
try {
statement = connection.createStatement();
rs = statement.executeQuery(query);
while (rs.next()) {
tlh.setIdMONHOC(rs.getInt("idMONHOC"));
listMon.add(tlh);
}
}catch(Exception e){}
return tlh;
}
public static void main(String[] args) {
SampleApp.layMotMonHoc(1);
SampleApp.showDanhSachDuLieu();
}
}
Mình nghĩ bạn có thể chia nhỏ công việc trên thành mô hình MVC như sau để tăng hiệu năng cũng như cấu trúc mã đẹp hơn , bạn tham khảo nhé . Mình lần đầu tiên viết bài nên chưa quen cách dùng các công cụ
của diễn đàn . Nếu muốn thì bạn cho mình emai mình gửi code sang cho .
1 - Đầu tiên là Class DBUtil
package DBUtil;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/**
*
-
@author : Huu Quyen
*/
public class DBUtil {
private static final String url = “”;
private static final String username = “”;
private static final String password = “”;
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(“com.mysql.jdbc.Driver”);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException | SQLException e) {
System.out.print(e);
}
return conn;
}
public static void closeStatement(Statement s)
{
try {
if(s != null) {
s.close();
}
}catch(SQLException e) {
System.out.println(e);
}
}
public static void closePreparedStatement(PreparedStatement ps)
{
try {
if(ps != null) {
ps.close();
}
}catch(SQLException e) {
System.out.println(e);
}
}
public static void closeResultSet(ResultSet rs)
{
try {
if(rs != null) {
rs.close();
}
}catch(SQLException e) {
System.out.println(e);
}
}
public static void closeConnection(Connection conn) {
try {
if(conn != null) {
conn.close();
}
}catch(SQLException e) {
System.out.println(e);
}
}
}
2 - Đoi tượng TaiLieuHoc
public class TaiLieuHoc {
private int idMonHoc;
private String tenMon;
private String maMon;
public TaiLieuHoc () {}
public int getIdMonHoc() {
return idMonHoc;
}
public void setIdMonHoc(int idMonHoc) {
this.idMonHoc = idMonHoc;
}
public String getTenMon() {
return tenMon;
}
public void setTenMon(String tenMon) {
this.tenMon = tenMon;
}
public String getMaMon() {
return maMon;
}
public void setMaMon(String maMon) {
this.maMon = maMon;
}
}
3 - Đối tượng Controller
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 java.util.List;
/**
*
-
@author Huu Quyen
*/
public class TaiLieuHocController {
private TaiLieuHoc taiLieuHoc;
public List layDanhSachDuLieu() {
List listMon = new ArrayList<>();
String query = “SELECT * FROM monhoc”;
ResultSet rs = null;
Statement st = null;
Connection conn = null;
try {
conn = DBUtil.getConnection();
st = conn.createStatement();
rs = st.executeQuery(query);
while (rs.next()) {
taiLieuHoc = new TaiLieuHoc();
taiLieuHoc.setIdMonHoc(rs.getInt("idMONHOC"));
taiLieuHoc.setTenMon(rs.getString("tenmon"));
taiLieuHoc.setMaMon(rs.getString("mamon"));
listMon.add(taiLieuHoc);
}
} catch (SQLException e) {
System.err.println(e);
return null;
} finally {
DBUtil.closeResultSet(rs);
DBUtil.closeStatement(st);
DBUtil.closeConnection(conn);
}
return listMon;
}
public TaiLieuHoc layMotMonHoc(int idMONHOC) {
String query = " SELECT * FROM monhoc "
+ " WHERE idMONHOC = ?";
PreparedStatement pts = null;
ResultSet rs = null;
Connection conn = null;
TaiLieuHoc tlh = new TaiLieuHoc();
try {
conn = DBUtil.getConnection();
pts = conn.prepareStatement(query);
pts.setInt(1, idMONHOC);
rs = pts.executeQuery();
while (rs.next()) {
tlh.setIdMonHoc(rs.getInt("idMONHOC"));
tlh.setTenMon(rs.getString("tenmon"));
tlh.setMaMon(rs.getString("mamon"));
}
} catch (SQLException e) {
System.err.print(e);
return null;
} finally {
DBUtil.closeResultSet(rs);
DBUtil.closeConnection(conn);
DBUtil.closePreparedStatement(pts);
}
return tlh;
}
}
4 - Lớp View để xem dữ liệu
import java.util.List;
/**
*
-
@author Huu Quyen
*/
public class MainService {
public static void main(String[] args) {
TaiLieuHocController taiLieuHocController = new TaiLieuHocController();
List list = taiLieuHocController.layDanhSachDuLieu();
if (list != null) {
for (TaiLieuHoc value : list) {
System.out.println("Ten Mon Hoc : " + value.getTenMon());
}
} else {
System.out.println("Khong Co Du Lieu De Hien Thi");
}
// Lay Mon Hoc Qua Id
TaiLieuHoc tlh = taiLieuHocController.layMotMonHoc(1);
if (tlh != null) {
System.out.println("Ten Mon Hoc Vua Tim Kiem: " + tlh.getTenMon());
} else {
System.out.println("Xin loi, Mon Hoc Ban Vua Tim Kiem Khong Ton Tai");
}
}
}
Chúc bạn thành công