我在java中执行一个从表中选择的查询后收到一个异常。该查询在mySQL中正常运行,但当它在java中使用时,我会得到一个异常。我试图在mySQL中运行完全相同的查询,它运行良好,但它会在java中给予一个异常。
public static void main(String[] args) throws SQLException,ClassNotFoundException {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection Con = DriverManager.getConnection("jdbc:mysql://localhost:3306/eldenringbuildgenerator" , "root", "admin");
Statement teststmt = Con.createStatement();
ResultSet rs = teststmt.executeQuery("select * ;"); // test query to make sure connection is real
rs = teststmt.executeQuery("select ArmorTypeID,ArmorTypeName from armortypes;");
// will delete later
while (rs.next()) {
System.out.print(rs.getInt(1) + " " + rs.getString(2) + " " );
}
} catch (SQLException e){
System.out.print(e);
}
1条答案
按热度按时间k3bvogb11#
从评论中总结出解决方案,并添加一些信息,希望对其他人在这里有所帮助。
解决方案是将
"select *;"
替换为"select 1"
,抛出异常是因为JDBC无法从以前的SQL查询中确定列。