organizationtype.java
来自「利用多线程从搜索引擎下载网页并提取数据到数据库。」· Java 代码 · 共 105 行
JAVA
105 行
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 + =
减小字号Ctrl + -
显示快捷键?