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