我正在制作一款在线游戏。我正在使用MySQL数据库。我试图遍历每一行并选择客户端需要连接的端口,因此我可以将客户端连接到我通过行收集的服务器。这是我的代码:
public class Main {
private static Connection con;
static ResultSet rs;
static String getServers = "SELECT * FROM servers";
public static void main(String[] args) {
connect();
Window window = new Window("BLAZE", 800, 600, false);
while(!window.isCloseRequested()) {
update();
render();
Display.update();
Display.sync(60);
}
}
private static void connect() {
try{
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://sql2.freesqldatabase.com/sql233105", "sql233105", "eD7*wA7!");
PreparedStatement st = con.prepareStatement(getServers);
rs = st.executeQuery();
//Loop through each row and get info
// Psuodo code
for(int i = 0; i < NUMBER OF ROWS; i++) {
int port = ROW.getColumn().getValue();
}
} catch(Exception e) {
e.printStackTrace();
}
}
private static void update() {
}
private static void render() {
}
}
答案 0 :(得分:0)
使用rs.next()
来迭代ResultSet
并使用rs.getInt()
您可以获取行的列值。
ResultSet rs = st.executeQuery();
while (rs.next()) {
int port = rs.getInt("PORT");
// do process...
}