我用ResultSetMetadata 类得到了该表的字段信息,用该类的什么方法可以完成我的要求? 或其它有什么方法来判断主键? 请指教!
2 个解决方案
#1
看看这段代码
public void tablesToXml(StringBuffer xml,Connection conn,String tName,String where) throws java.sql.SQLException{
//System.err.println("select * from "+tName+" where "+where);
String keyName=null,keyValue=null;
ResultSet rsM =null;
DatabaseMetaData dbmd= conn.getMetaData();
ResultSet rs = dbmd.getPrimaryKeys(null,null,tName);
while (rs.next()) keyName=rs.getString(4);
rs.close();
Statement stmt = conn.createStatement();
rs = stmt.executeQuery("select * from "+tName+' '+where);
ResultSetMetaData rsmd=rs.getMetaData();
int count=rsmd.getColumnCount()+1;
while (rs.next()){
xml.append('<'+tName+">\n");
if (keyName!=null)
keyValue=rs.getString(keyName);
for (int i=1;i<count;i++)
xml.append('<'+rsmd.getColumnName(i)+'>'+rs.getString(i)+"</"+rsmd.getColumnName(i)+">\n");
//xml.append(keyName+'='+keyValue);
if (keyName!=null){
rsM = dbmd.getExportedKeys(null,null,tName);
while (rsM.next())
tablesToXml(xml,conn,rsM.getString(7),"where "+rsM.getString(8)+"='"+keyValue+"'");
rsM.close();
};
xml.append("</"+tName+">\n");
};
rs.close();
stmt.close();
}
public void tablesToXml(StringBuffer xml,Connection conn,String tName,String where) throws java.sql.SQLException{
//System.err.println("select * from "+tName+" where "+where);
String keyName=null,keyValue=null;
ResultSet rsM =null;
DatabaseMetaData dbmd= conn.getMetaData();
ResultSet rs = dbmd.getPrimaryKeys(null,null,tName);
while (rs.next()) keyName=rs.getString(4);
rs.close();
Statement stmt = conn.createStatement();
rs = stmt.executeQuery("select * from "+tName+' '+where);
ResultSetMetaData rsmd=rs.getMetaData();
int count=rsmd.getColumnCount()+1;
while (rs.next()){
xml.append('<'+tName+">\n");
if (keyName!=null)
keyValue=rs.getString(keyName);
for (int i=1;i<count;i++)
xml.append('<'+rsmd.getColumnName(i)+'>'+rs.getString(i)+"</"+rsmd.getColumnName(i)+">\n");
//xml.append(keyName+'='+keyValue);
if (keyName!=null){
rsM = dbmd.getExportedKeys(null,null,tName);
while (rsM.next())
tablesToXml(xml,conn,rsM.getString(7),"where "+rsM.getString(8)+"='"+keyValue+"'");
rsM.close();
};
xml.append("</"+tName+">\n");
};
rs.close();
stmt.close();
}
#2
String keyName=null,keyValue=null;
ResultSet rsM =null;
DatabaseMetaData dbmd= conn.getMetaData();
ResultSet rs = dbmd.getPrimaryKeys(null,null,tName);
while (rs.next()) keyName=rs.getString(4);
rs.close();
这部分代码就可以得到对应表的主键信息
ResultSet rsM =null;
DatabaseMetaData dbmd= conn.getMetaData();
ResultSet rs = dbmd.getPrimaryKeys(null,null,tName);
while (rs.next()) keyName=rs.getString(4);
rs.close();
这部分代码就可以得到对应表的主键信息
#1
看看这段代码
public void tablesToXml(StringBuffer xml,Connection conn,String tName,String where) throws java.sql.SQLException{
//System.err.println("select * from "+tName+" where "+where);
String keyName=null,keyValue=null;
ResultSet rsM =null;
DatabaseMetaData dbmd= conn.getMetaData();
ResultSet rs = dbmd.getPrimaryKeys(null,null,tName);
while (rs.next()) keyName=rs.getString(4);
rs.close();
Statement stmt = conn.createStatement();
rs = stmt.executeQuery("select * from "+tName+' '+where);
ResultSetMetaData rsmd=rs.getMetaData();
int count=rsmd.getColumnCount()+1;
while (rs.next()){
xml.append('<'+tName+">\n");
if (keyName!=null)
keyValue=rs.getString(keyName);
for (int i=1;i<count;i++)
xml.append('<'+rsmd.getColumnName(i)+'>'+rs.getString(i)+"</"+rsmd.getColumnName(i)+">\n");
//xml.append(keyName+'='+keyValue);
if (keyName!=null){
rsM = dbmd.getExportedKeys(null,null,tName);
while (rsM.next())
tablesToXml(xml,conn,rsM.getString(7),"where "+rsM.getString(8)+"='"+keyValue+"'");
rsM.close();
};
xml.append("</"+tName+">\n");
};
rs.close();
stmt.close();
}
public void tablesToXml(StringBuffer xml,Connection conn,String tName,String where) throws java.sql.SQLException{
//System.err.println("select * from "+tName+" where "+where);
String keyName=null,keyValue=null;
ResultSet rsM =null;
DatabaseMetaData dbmd= conn.getMetaData();
ResultSet rs = dbmd.getPrimaryKeys(null,null,tName);
while (rs.next()) keyName=rs.getString(4);
rs.close();
Statement stmt = conn.createStatement();
rs = stmt.executeQuery("select * from "+tName+' '+where);
ResultSetMetaData rsmd=rs.getMetaData();
int count=rsmd.getColumnCount()+1;
while (rs.next()){
xml.append('<'+tName+">\n");
if (keyName!=null)
keyValue=rs.getString(keyName);
for (int i=1;i<count;i++)
xml.append('<'+rsmd.getColumnName(i)+'>'+rs.getString(i)+"</"+rsmd.getColumnName(i)+">\n");
//xml.append(keyName+'='+keyValue);
if (keyName!=null){
rsM = dbmd.getExportedKeys(null,null,tName);
while (rsM.next())
tablesToXml(xml,conn,rsM.getString(7),"where "+rsM.getString(8)+"='"+keyValue+"'");
rsM.close();
};
xml.append("</"+tName+">\n");
};
rs.close();
stmt.close();
}
#2
String keyName=null,keyValue=null;
ResultSet rsM =null;
DatabaseMetaData dbmd= conn.getMetaData();
ResultSet rs = dbmd.getPrimaryKeys(null,null,tName);
while (rs.next()) keyName=rs.getString(4);
rs.close();
这部分代码就可以得到对应表的主键信息
ResultSet rsM =null;
DatabaseMetaData dbmd= conn.getMetaData();
ResultSet rs = dbmd.getPrimaryKeys(null,null,tName);
while (rs.next()) keyName=rs.getString(4);
rs.close();
这部分代码就可以得到对应表的主键信息