我遇到了 sql 查询的问题,我需要你的帮助 :(。在我的代码中,我想在输入本课程的 id 时显示来自 c#Log 的学生姓名,这是 c#Log 表:HERE 这是课程表:这里
这是我的代码:
public void searchRows() throws ClassNotFoundException, SQLException
{
String logName=null;
Class.forName("com.mysql.jdbc.Driver");
Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/psd_DB?"
+ "user=root&password=123");
Scanner s=new Scanner(System.in);
System.out.println("Enter the course id you want to display its log:");
courseId=Integer.parseInt(s.nextLine());
PreparedStatement st1 = (PreparedStatement) con.prepareStatement(" SELECT * FROM courses WHERE courseId="+courseId);
ResultSet r=(ResultSet) st1.executeQuery();
while(r.next())
{
logName=r.getString("courseName")+"Log";
}
//System.out.println(logName);
PreparedStatement st2 = (PreparedStatement) con.prepareStatement("SELECT "+logName+"."+"studentName FROM "+logName+" INNER JOIN courses ON "+logName+"."+courseId+"="+"courses."+courseId+";");
st2.executeQuery();
}
当我输入 2 时,输出是这个异常:
线程“主”com.mysql.jdbc.exceptions.MySQLSyntaxErrorException 中的异常:“字段列表”中的未知列“C”