gpt4 book ai didi

java - 如何从不同的 MySQL 表中获取特定的 id

转载 作者:行者123 更新时间:2023-11-29 00:10:41 24 4
gpt4 key购买 nike

我知道这听起来很糟糕,但我找不到表达我需要的方法。我目前正在构建一个类来检查篮球队是否通过获取 id 在同一事件中“击中”同一张票来自2个不同的表。我的数据库由3个表组成。第一个是'tickets'表,用户在其中给出名称,年份和门票名称。第二个是'events'表,用户在其中给出篮球赛事和年份。现在最后一张 table 让我感到困惑。(我已经检查过同一事件是否发生在同一年,因为在锦标赛中不可能在同一个体育场比赛 2 次)。我没有检查过是否同一张票出现在同一个事件中,这就是我想要做的。

第三张表是“检查”表。我有 3 个整数变量(id、ticket_id、event_id),我想从门票表和第一列(这是自动递增的 id)中获取 ticket_id,并对事件表中的 event_id 执行相同的操作(第一列是 id当然)。

然后我想检查并确保它们不匹配超过 1 次并返回一个 boolean 值。

这就是我到目前为止的想法。

P.S 很抱歉发了这么长的帖子,但我对此很困惑......

import java.sql.ResultSet;
import java.sql.SQLException;

public class check {

private int ticket_id,event_id;

public int getTicket_id() {
return ticket_id;
}

public void setTicket_id(int ticket_id) {
this.ticket_id = ticket_id;
}

public int getEvent_id() {
return event_id;
}

public void setEvent_id(int event_id) {
this.event_id = event_id;
}

public check(int ticket_id, int event_id) {
super();
this.ticket_id = ticket_id;
this.event_id = event_id;
}

public boolean checkExistance_3 (){

String errMsg = "";
// class that connects with the MySQL DB
ServerConn sc = new ServerConn();
String sql3 = "SELECT Count(*) AS RowCount FROM events INNER JOIN tickets on tickets.id = events.id WHERE tickets.id = '" + ticket_id + "' AND events.id =" + event_id ;
// HOW TO GIVE VALUES AT ticket_id and event_id??? this is my problem
// I dont now if a need a SELECT with rs.next() because it would return all id's!

// class that executes queries
ResultSet rs3 = sc.dbQuery(sql3);

if (errMsg.equals(""))
{
try {
rs3.next();
int rows = rs3.getInt("RowCount");
} catch (SQLException case1) {
case1.printStackTrace();
} catch (Exception case2) {
case2.printStackTrace();}
return true;
}
return false;
}

}

我选择不介绍其他 4 个类的工作原理,因为这样会更令人困惑。如果需要其他类(class)来澄清问题,我会将其添加到我的帖子中。

门票表

tickets table

事件表

events table

我创建了一个将 id 保存在第 3 个表中的类,但是 sql 和 sql_1 语句中的变量无法解析,因为它们属于不同的类。是否可以继承来自不同类的变量?

public String SaveId() {

String errMsg2 = "";


if ( errMsg2.equals("") ) {
try
{

ServerConn sc = new ServerConn();
// sql_game and ticketNumber appear on other classes. Can I inherit them?
// wont find the variables, error.
String sql = "SELECT (id) FROM events WHERE decsrp = '" + sql_game + "'";
String sql_1 = "SELECT (id) FROM tickets WHERE ticket = '" + ticketNumber + "'";
ResultSet rs = sc.dbQuery(sql);
ResultSet rs_1 = sc.dbQuery(sql_1);
boolean allowed = true;
while (rs.next())
allowed = false;
while (rs_1.next())
allowed = false;
if (allowed == true)
{
try {
Connection cn = sc.OpenDatabase();
PreparedStatement ps2 = cn.prepareStatement("INSERT INTO elenxos (event_id,ticket_id) VALUES (?,?)");
ps2.setInt(1,this.event_id);
ps2.setInt(2,this.ticket_id);
ps2.executeUpdate();
JOptionPane.showMessageDialog(null,"Valid Ticket");
} catch (SQLException case1) {
case1.printStackTrace();
} catch (Exception case2){
case2.printStackTrace();}
}
else if (allowed == false)
errMsg2 += "Impossible to use the same ticket 2 times!!";
}catch (SQLException e)
{
System.out.println("8");
}
catch (Exception e)
{
System.out.println("9");
}
} else {
errMsg2 += "FAILURE!!" ;
}
return errMsg2;
}

最佳答案

您似乎正在为如何执行每次执行查询时将具有不同查询值的查询而苦恼。您应该使用 PreparedStatement 来处理这种情况。这将允许您在创建后使用相同的语句,并为其提供不同的值。此外,如果您只是想查看是否出现重复,则可以改进您的数据库查询。

考虑以下几点:

package com.example.db;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Check {
// Class fields
private PreparedStatement check;
private String checkQuery = "Select Count(*)"
+ " from events inner join tickets on tickets.id = events.id"
+ " where tickets.id = ? " + " and events.id = ? ";

// Object fields
private int ticket_id;
private int event_id;

public Check(int ticket_id, int event_id) {
super();
this.ticket_id = ticket_id;
this.event_id = event_id;

}

public int getTicket_id() {
return ticket_id;
}

public void setTicket_id(int ticket_id) {
this.ticket_id = ticket_id;
}

public int getEvent_id() {
return event_id;
}

public void setEvent_id(int event_id) {
this.event_id = event_id;
}

public boolean checkExistance_3(Connection sc) {
boolean returnValue = false;
// TODO previously established db connnection passed performance reasons

try {
check = sc.prepareStatement(checkQuery);
// the first argument is the varialbe indes, the second the value to
// supstitute
check.setInt(1, ticket_id);
check.setInt(2, event_id);
ResultSet results = check.executeQuery();
results.next();
int countResult = results.getInt("RowCount");
if (countResult > 1) {
returnValue = true;
}

} catch (SQLException e) {
System.out.println("Error querying database: " + e.getMessage());
}
return returnValue;
}
}

关于java - 如何从不同的 MySQL 表中获取特定的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25241953/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com