Java JDBC-MYSQL-FK实现
我正在尝试使用JDBC将FK关系从MySQL实现到JAVA。我有一个Java JDBC-MYSQL-FK实现,java,jdbc,Java,Jdbc,我正在尝试使用JDBC将FK关系从MySQL实现到JAVA。我有一个Garaz对象列表,每个Garaz都有一个Auto(汽车)对象列表。我有非常复杂的数据 我的MySQl数据库还可以,我试着这样做: public static ArrayList <Garaz> selectRecords() throws SQLException { Connection dbConnection = null; Statement statement = null; S
Garaz
对象列表,每个Garaz
都有一个Auto
(汽车)对象列表。我有非常复杂的数据
我的MySQl数据库还可以,我试着这样做:
public static ArrayList <Garaz> selectRecords() throws SQLException {
Connection dbConnection = null;
Statement statement = null;
String selectTableSQL = "SELECT Garaz.G_ID, Garaz.Nazwa, Garaz.Adres, Garaz.LiczbaMiejsc, Garaz.LiczbaPoziomow, " +
"Garaz.Czynny, Auta.A_Id, Auta.Model, Auta.Kolor, Auta.IloscDrzwi, Auta.Rejestracja\n" +
"FROM Garaz\n" +
"LEFT JOIN Auta\n" +
"ON Garaz.G_Id=Auta.G_Id\n" +
"ORDER BY Garaz.G_Id; ";
// ArrayList lista = new ArrayList <Garaz>();
try {
dbConnection = getDBConnection();
statement = dbConnection.createStatement();
System.out.println(selectTableSQL);
// execute select SQL stetement
ResultSet rs = statement.executeQuery(selectTableSQL);
while (rs.next()) {
int g_id = rs.getInt("G_ID");
String nazwa = rs.getString("NAZWA");
String adres = rs.getString("ADRES");
int lmiejsc = rs.getInt("LICZBAMIEJSC");
int lpoz = rs.getInt("LICZBAPOZIOMOW");
boolean czynny = rs.getBoolean("CZYNNY");
ArrayList lista2 = new ArrayList <Auto>();
int a_id = rs.getInt("A_Id");
String model = rs.getString("Model");
String kolor = rs.getString("Kolor");
int ildrzwi = rs.getInt("IloscDrzwi");
String rejestracja = rs.getString("Rejestracja");
Auto d = new Auto(a_id, model, kolor, ildrzwi, rejestracja);
if (a_id !=0){
lista2.add(d);
}
Garaz f = new Garaz(g_id, nazwa, lista2, adres, lmiejsc, lpoz, czynny);
lista.add(f);
//System.out.println("nazwa : " + nazwa);
//System.out.println("adres : " + adres);
// return lista;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
if (statement != null) {
statement.close();
}
if (dbConnection != null) {
dbConnection.close();
}
}
return lista;
}
publicstaticarraylistselectrecords()抛出SQLException{
连接dbConnection=null;
Statement=null;
String selectTableSQL=“SELECT Garaz.G_ID,Garaz.Nazwa,Garaz.Adres,Garaz.LiczbaMiejsc,Garaz.LiczbaPoziomow,”+
“Garaz.Czynny,Auta.A_Id,Auta.Model,Auta.Kolor,Auta.IloscDrzwi,Auta.Rejestracja\n”+
“来自Garaz\n”+
“左加入Auta\n”+
“在Garaz.G_Id=Auta.G_Id上\n”+
“Garaz.G_Id.的命令;”;
//ArrayList lista=新的ArrayList();
试一试{
dbConnection=getDBConnection();
statement=dbConnection.createStatement();
System.out.println(selectTableSQL);
//执行select SQL语句
ResultSet rs=statement.executeQuery(selectTableSQL);
while(rs.next()){
int g_id=rs.getInt(“g_id”);
字符串nazwa=rs.getString(“nazwa”);
字符串adres=rs.getString(“adres”);
int-lmiejsc=rs.getInt(“LICZBAMIEJSC”);
int-lpoz=rs.getInt(“LICZBAPOZIOMOW”);
布尔czynny=rs.getBoolean(“czynny”);
ArrayList lista2=新的ArrayList();
int a_id=rs.getInt(“a_id”);
字符串模型=rs.getString(“模型”);
字符串kolor=rs.getString(“kolor”);
int ildrzwi=rs.getInt(“IloscDrzwi”);
String rejestracja=rs.getString(“rejestracja”);
Auto d=新汽车(a_id、型号、kolor、ildrzwi、rejestracja);
如果(a_id!=0){
表A2.添加(d);
}
Garaz f=新的Garaz(g_id、nazwa、lista2、adres、lmiejsc、lpoz、czynny);
列表a.添加(f);
//System.out.println(“nazwa:+nazwa”);
//System.out.println(“adres:+adres”);
//返回列表a;
}
}捕获(SQLE异常){
System.out.println(e.getMessage());
}最后{
if(语句!=null){
语句。close();
}
if(dbConnection!=null){
dbConnection.close();
}
}
返回列表a;
}
我不明白如何从ResultSet rs
中读取:ArrayList-Garaz
包含对象(Garaz
),而每个Garaz
对象都包含ArrayList-Auto
。因此,通过从rs
(ResultSet
)读取数据来创建两个列表(一个是另一个的一部分)时,我遇到了很大的问题。我有DB表中的所有Garaz
和所有Auto
,但关系是混合的。像Garaz1
包含随机Auto
(汽车)
如何创建两个列表(一个是另一个列表的一部分)以基于
G_ID
保持关系Auto
是Garaz
的一部分?您的结果集将为每个Garaz和Auto提供一个结果(aka行),因为select语句就是这样做的。所以你可以
要么按原样解析结果集,然后手动创建所需的每个Garaz&Auto记录,但必须处理重复的Garaz数据
或
您可以使用MyBatis之类的框架获取对象,或者
或
对Garaz列表执行一个SELECT语句,然后执行另一个SELECT语句以获取每个Garaz的AUTO列表
Sudo代码。。。。。
@存储库
公共类StoreDbDAO
{
@自动连线
public void init(@Qualifier(“dataSourceCDB”)数据源数据源){
this.dataSource=数据源;
this.simpleJdbcTemplate=新的simpleJdbcTemplate(数据源);
}
私有静态最终字符串GET\u Available\u RECORDS=“选择d.ID,d.HARDWARE\u ID”+
“来自设备d”+
“o.X_ID=d.X_ID上的左联接表B o”+
“哪里”+
“d.DEVC\u硬件\u ID不为空”+
“和o.CODE=?”;
公共列表getStores(字符串代码)
{
返回simpleJdbcTemplate.queryForList(获取可用记录,代码);
}
}
@自动连线 StoreDbDAO StoreDbDAO 公共无效调用方(){ List>stores=storeDbDAO.getStores()
List-storeRecords=new-ArrayList[stores.size()];
用于(地图商店:商店)
{
最终字符串storeId=(String)store.get(“硬件_ID”);
StoreRecord x=新的StoreRecord(storeId)
存储记录。添加(x);
List devicesinestore=storeDbDAO.getDevicesForStore(storeId);
//把这些转化成有用的东西。
x、 SetDeviceInstore(转换列表(DeviceInstore));
}
}您需要迭代结果,检查您是否已经为行的
G\u ID
创建了Garaz
对象,并使用该对象或创建一个新对象。这可以通过对G\u ID
字段排序来简化,只要在G\u ID
更改时创建一个新的Garaz
对象即可
当您评论说您不知道如何做到这一点时,下面是一个完整的示例:
public List<Garaz> getAllGaraz() throws SQLException {
List<Garaz> garazList = new ArrayList<Garaz>();
try (
Connection con = getDBConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(
"SELECT Garaz.G_ID, /* other garaz columns */ " +
"Auta.A_Id /*other auta columns */\n" +
"FROM Garaz\n" +
"LEFT JOIN Auta\n" +
"ON Garaz.G_Id=Auta.G_Id\n" +
"ORDER BY Garaz.G_Id");
) {
Garaz currentGaraz = null;
while (rs.next()) {
int garazId = rs.getInt("G_ID");
// Create Garaz only if it is different
if (currentGaraz == null || currentGaraz.getId() != garazId) {
// retrieve other columns
currentGaraz = new Garaz(g_id /* + other garaz columns */);
garazList.add(currentGaraz);
}
int a_id = rs.getInt("A_Id");
// replacement of your condition of a_id != 0
// 0 could be a valid value, check for null instead
if (!rs.wasNull()) {
// retrieve other columns
Auto auta = new Auta(a_id /* + other auta columns */);
// The list of Auta is part of the garaz
currentGaraz.addAuta(auta);
}
}
return garazList;
}
}
public class Garaz {
private final List<Auta> autaList = new ArrayList<Auta>();
private final int id;
public Garaz(int g_id /* + other fields*/) {
id = g_id;
}
public int getId() {
return id;
}
public void addAuta(Auta auta) {
autaList.add(auta);
}
public List<Auta> getAutaList() {
return new ArrayList<Auta>(autaList);
}
}
public List getAllGaraz()抛出SQLException{
List-garazList=new-ArrayList();
试一试(
Connection con=getDBConnection();
语句stmt=con.createStatement();
结果集rs=stmt.executeQuery(
“选择Garaz.G_ID,/*其他Garaz列*/”+
“Auta.A_Id/*其他Auta列*/\n”+
“来自Garaz\n”+
“左加入Auta\n”+
“在Garaz.G_Id=Auta.G_Id上\n”+
“Garaz.G_Id命令”);
) {
Garaz currentGaraz=null;
while(rs.next()){
int-garazId=rs.getInt(“G_-ID”);
//仅当Garaz不同时才创建Garaz
if(currentGaraz==null | | currentGaraz.getId()!=garazId){
//检索其他
List<Stores> storeRecords = new ArrayList[stores.size()];
for (Map<String, Object> store: stores)
{
final String storeId = (String) store.get("HARDWARE_ID");
StoreRecord x = new StoreRecord(storeId)
storeRecords.add(x);
List<Map<String, Object>> devicesInTheStore = storeDbDAO.getDevicesForStore(storeId);
// convert these into something useful.
x.setDevicesInStore(convertToList(devicesInTheStore));
}
public List<Garaz> getAllGaraz() throws SQLException {
List<Garaz> garazList = new ArrayList<Garaz>();
try (
Connection con = getDBConnection();
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(
"SELECT Garaz.G_ID, /* other garaz columns */ " +
"Auta.A_Id /*other auta columns */\n" +
"FROM Garaz\n" +
"LEFT JOIN Auta\n" +
"ON Garaz.G_Id=Auta.G_Id\n" +
"ORDER BY Garaz.G_Id");
) {
Garaz currentGaraz = null;
while (rs.next()) {
int garazId = rs.getInt("G_ID");
// Create Garaz only if it is different
if (currentGaraz == null || currentGaraz.getId() != garazId) {
// retrieve other columns
currentGaraz = new Garaz(g_id /* + other garaz columns */);
garazList.add(currentGaraz);
}
int a_id = rs.getInt("A_Id");
// replacement of your condition of a_id != 0
// 0 could be a valid value, check for null instead
if (!rs.wasNull()) {
// retrieve other columns
Auto auta = new Auta(a_id /* + other auta columns */);
// The list of Auta is part of the garaz
currentGaraz.addAuta(auta);
}
}
return garazList;
}
}
public class Garaz {
private final List<Auta> autaList = new ArrayList<Auta>();
private final int id;
public Garaz(int g_id /* + other fields*/) {
id = g_id;
}
public int getId() {
return id;
}
public void addAuta(Auta auta) {
autaList.add(auta);
}
public List<Auta> getAutaList() {
return new ArrayList<Auta>(autaList);
}
}