📄 empmodel.java
字号:
/*
*EmpModel.java:建立职工实体类。
*包括:与数据库进行增、删、改、查操作。
*/
package zichan;
import java.sql.*;
import java.math.*;
public class EmpModel {
String empno;
String ename;
String position;
String remarks;
DBConnt dbc;
Connection conn;
public EmpModel() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public EmpModel(String n,String e,String p,String r) {
empno = n;
ename = e;
position = p;
remarks = r;
}
public void setAll(String e,String p,String r) {
ename = e;
position = p;
remarks = r;
}
public String getEmpno() {
return empno;
}
public String getEname() {
return ename;
}
public String getPosition() {
return position;
}
public String getRemarks() {
return remarks;
}
public void insert() {
DBConnt dbc = new DBConnt();
conn = dbc.getConnection();
try {
PreparedStatement pstmt =
conn.prepareStatement("insert into EMPLOYEE (EMPNO,ENAME,POSITION,REMARKS) values (?, ?, ?, ?)");
pstmt.setInt(1, Integer.parseInt(empno));
pstmt.setString(2, ename);
pstmt.setString(3, position);
pstmt.setString(4, remarks);
pstmt.execute();
conn.commit();
conn.close();
}
catch (java.sql.SQLException s) {
s.printStackTrace();
}
}
public void select() {
dbc = new DBConnt();
conn = dbc.getConnection();
String sql;
String sql = "SELECT empno,rtrim(ename),rtrim(position),rtrim(remarks) FROM employee where empno = "+Integer.parseInt(empno);
try {
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery (sql);
while (rset.next()) {
ename = rset.getString(2);
position = rset.getString(3);
remarks = rset.getString(4);
}
conn.commit();
rset.close();
stmt.close();
conn.close();
}
catch (java.sql.SQLException s) {
System.out.println("exception: " + s.getMessage());
}
}
public void update() {
dbc = new DBConnt();
conn = dbc.getConnection();
try {
PreparedStatement pstmt =
conn.prepareStatement("update EMPLOYEE set ENAME = ?,POSITION = ?,REMARKS = ? where empno = "+Integer.parseInt(empno));
pstmt.setString(1, ename);
pstmt.setString(2, position);
pstmt.setString(3, remarks);
pstmt.execute();
conn.commit();
conn.close();
}
catch (java.sql.SQLException s) {
System.out.println("exception: " + s.getMessage());
}
}
public void delete() {
dbc = new DBConnt();
conn = dbc.getConnection();
try {
PreparedStatement pstmt =
conn.prepareStatement("delete EMPLOYEE where empno = "+Integer.parseInt(empno));
pstmt.execute();
conn.commit();
empno = "";
ename = null;
position = null;
remarks = null;
conn.close();
}
catch (java.sql.SQLException s) {
System.out.println("exception: " + s.getMessage());
}
}
public int selEmpCnt() {
dbc = new DBConnt();
conn = dbc.getConnection();
int cnt=0;
if (conn == null) {
System.out.println("Can not connected yet!");
return -1;
}
try {
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery ("select count(empno) from employee");
while (rset.next()) {
cnt = rset.getInt(1);
}
rset.close();
stmt.close();
conn.close();
}
catch (java.sql.SQLException s) {
System.out.println("exception: " + s.getMessage());
}
return cnt;
}
public void selEmpNo(String [] scnt) {
dbc = new DBConnt();
conn = dbc.getConnection();
int k=0;
String sql;
if (conn == null) {
System.out.println("Can not connected yet!");
return;
}
try {
sql = "select empno from employee";
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery (sql);
while (rset.next()) {
scnt[k++] = rset.getString(1);
}
rset.close();
stmt.close();
conn.close();
}
catch (java.sql.SQLException s) {
System.out.println("exception: " + s.getMessage());
}
}
private void jbInit() throws Exception {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -