DAO
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
public class DAO {
Connection con;
public void getCon() {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/p2";
String user="root";
String password="1111";
con = DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
}
}
}
MemberDAO
Statement stmt;
PreparedStatement pstmt;
ResultSet rs;
DAO d = new DAO();
public Vector<Member> listAllMember(int startRow, int endRow, String field, String search ) {
d.getCon();
Vector<Member> list = new Vector<Member>();
String str = "";
if(search != null) { // 검색어 존재시
str = " and "+field+" like '%"+search+"%' ";
}
try {
String sql = "select * from member4 where (1) "+str+" limit ?,?";
pstmt = d.con.prepareStatement(sql);
pstmt.setInt(1, startRow);
pstmt.setInt(2, endRow);
rs = pstmt.executeQuery();
while(rs.next()) {
Member m = new Member();
m.setId(rs.getString("id"));
m.setPass(rs.getString("pass"));
m.setName(rs.getString("name"));
m.setAge(rs.getInt("age"));
m.setGender(rs.getString("gender"));
m.setEmail(rs.getString("email"));
m.setLevel(rs.getString("level"));
m.setFile1(rs.getString("file1"));
list.add(m);
}
rs.close();
pstmt.close();
d.con.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
Statement방식
String sql_counts = "(select "+s+" from free "+c+") UNION (select "+s+" from meet4 "+c+") UNION (select "+s+" from meet3 "+c+") UNION (select "+s+" from meet2 "+c+") UNION (select "+s+" from meet1 "+c+") UNION (select "+s+" from meet "+c+") UNION (select "+s+" from infor4 "+c+") UNION (select "+s+" from infor3 "+c+") UNION (select "+s+" from infor2 "+c+") UNION (select "+s+" from infor1 "+c+") UNION (select "+s+" from infor "+c+") UNION (select "+s+" from gallery4 "+c+") UNION (select "+s+" from gallery3 "+c+") UNION (select "+s+" from gallery2 "+c+") UNION (select "+s+" from gallery1 "+c+") UNION (select "+s+" from gallery "+c+") UNION (select "+s+" from qna4 "+c+") UNION (select "+s+" from qna3 "+c+") UNION (select "+s+" from qna2 "+c+") UNION (select "+s+" from qna1 "+c+") UNION (select "+s+" from qna "+c+") UNION (select "+s+" from free1 "+c+") UNION (select "+s+" from free2 "+c+") UNION (select "+s+" from free3 "+c+") UNION (select "+s+" from free4 where id = '"+session_id+"')";
// out.print(sql_count);
Connection con_counts = DriverManager.getConnection(url, user, password);
Statement stmt_counts = con_counts.createStatement();
ResultSet rs_counts = stmt_counts.executeQuery(sql_counts);
int total_records = 0;
int a = 0;
while(rs_counts.next()){
a = rs_counts.getInt("count");
total_records += a;
}
rs_counts.close();
stmt_counts.close();
con_counts.close();
'Database > MySQL' 카테고리의 다른 글
Tomcat Always 실행시 아무것도 안묻는는거 되돌릴때 (0) | 2023.04.27 |
---|---|
JOIN,UNION (0) | 2023.04.26 |
MySQL 명령문~ (0) | 2023.01.21 |
MySQL 환경구축 (0) | 2023.01.21 |
댓글