起点:
该程序需要连接到数据库。数据结构未知。访问数据在单独的文件中提供。
需要对数据进行分析并在以后更新。
使用jdbc
和Statement
、ResultSet
和ResultSetMetaData
建立连接。
为了进一步操作数据,有必要将它们全部存储在一个
对象,一个LinkedHashMap
,key是列索引,value是列条目。
public class TableRepresentation {
private final LinkedHashMap<Integer, DataType> tableData;
因为数据类型不同,所以使用了新的接口(interface)
DataType
。
public interface DataType {
public void putDataToObject(int key, int value);
public void putDataToObject(int key, String value);
public void putDataToObject(int key, Date value);}
想法是使用工厂模式在运行时创建数据类型对象。下一步是将数据添加到每个 DataType
对象。问题就在这里。
这样一来,我当然要在每个类中实现每个方法:
public class IntegerObject implements DataType{
private final Map<Integer, Integer> attributeData;
public IntegerObject(){
attributeData = new LinkedHashMap<>();
}
/**
* @return the attributeData
*/
public Map<Integer, Integer> getAttributeData() {
return attributeData;
}
@Override
public void putDataToObject(int key, int value){
attributeData.put(key, value);
}
@Override
public void putDataToObject(int key, String value) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void putDataToObject(int key, Date value) {
throw new UnsupportedOperationException("Not supported yet.");
}
其中有很多未使用的并且肯定是危险的代码。
问题 1: 有没有办法重载从接口(interface)实现的方法?
问题 2: 有没有更好的方法来实现目标?
最佳答案
Question 1: Is there a way to overload methods, which are implemented from an interface?
所有这些方法签名都有点多余,您可以通过使用单个方法创建接口(interface)来概括:
public interface DataType {
public void putDataToObject(int key, Object value);
}
然后你可以用任何你想要的方式覆盖它:
public class IntegerObject implements DataType{
private final Map<Integer, Object> attributeData = new LinkedHashMap<>();
public Map<Integer, Integer> getAttributeData() {
return attributeData;
}
@Override
public void putDataToObject(int key, Object value){
attributeData.put(key, value);
}
}
现在,假设每个类只需要 1 个 putDataToObject
实现。如果您需要多个实现,使用 Java 中的 instanceof
关键字分支出不同的行为并不难。
Question 2: Is there a better way to achive the goal?
是的,在 JDBC 中有一种标准方法可以使用 SQLData 执行此操作界面。
例子:
public class MyUser implements SQLData, Serializable
{
private static final long serialVersionUID = -4235126659654360181L;
public String name;
public String address;
public String phone;
private String sql_type;
public MyUser(String name, String address, String phone) {
this.name = name;
this.address = address;
this.phone = phone;
}
@Override
public String getSQLTypeName() throws SQLException {
return sql_type;
}
@Override
public void readSQL(SQLInput stream, String typeName) throws SQLException {
sql_type = typeName;
name = stream.readString();
address = stream.readString();
phone = stream.readString();
}
@Override
public void writeSQL(SQLOutput stream) throws SQLException {
stream.writeString(name);
stream.writeString(address);
stream.writeString(phone);
}
}
然后,很容易将这些类读写到数据库中:
// Writing an object to a database
PreparedStatement pstmt = conn.prepareStatement("insert into MY_USER_TABLE values(?,?)");
pstmt.setInt(1, 1);
pstmt.setObject(2, new MyUser("Alice", "aAddress", "111-1111"));
pstmt.addBatch();
pstmt.executeBatch(); // Insert the users
pstmt.close();
// Reading an object from a database
Statement stmt = conn.createStatement();
stmt.execute("select * from MY_USER_TABLE");
ResultSet rs = stmt.getResultSet();
rs.next();
MyUser userFromDB = rs.getObject(2, MyUser.class);
rs.close();
stmt.close();
关于java - 如何使用 JDBC 将类存储到数据库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34273757/