📄 industry.java
字号:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class Industry {
public int id;
public String name;
static Connection cnn;
static SQLException e;
static Map<String, Integer> map;
static {
fillBuffer();
}
private static void fillBuffer() {
try {
cnn = DataAccess.getConnection();
map = new HashMap<String, Integer>();
PreparedStatement stmt;
stmt = cnn.prepareStatement("select * from industries");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
map.put(rs.getString("name"), new Integer(rs.getInt("id")));
}
} catch (SQLException e1) {
e = e1;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public Industry(String industry) throws SQLException {
this.name = industry;
cnn = DataAccess.getConnection();
if (e != null)
throw e;
}
private int getIDLocal() {
if (map.containsKey(this.name))
return map.get(this.name).intValue();
else
return 0;
}
public int getID() throws SQLException {
if (name == null || name == "")
return 0;
if (id != 0)
return id;
id=getIDLocal();
if (id != 0)
return id;
if (!populateFromDB())
insert();
populateFromDB();
map.put(name, id);
return id;
}
public synchronized static boolean exists(String industry)
throws SQLException {
fillBuffer();
return new Industry(industry).populateFromDB();
}
public boolean populateFromDB() throws SQLException {
PreparedStatement stmt;
stmt = cnn.prepareStatement("select id from industries where name=?");
stmt.setString(1, name);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
this.id = rs.getInt("id");
return true;
} else
return false;
}
public void insert() throws SQLException {
PreparedStatement stmt;
stmt = cnn.prepareStatement("insert into industries(name) select ?");
stmt.setString(1, name);
stmt.execute();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -