I'm rather new to the world of Java, and i am not quite sure how to do this.
I have a MySQL class which extracts a table just fine, and prints it to the console. However, i need to do custom SQL queries from another class, and i can't figure out how to do that. Anyone who can help out?
Here's the mysql class:
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class mySQL {
public static void main(String[] args) {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
String url = "jdbc:mysql://host:port/database";
String user = "user";
String password = "password";
try {
con = DriverManager.getConnection(url, user, password);
pst = con.prepareStatement("SELECT * FROM NFT_users");
rs = pst.executeQuery();
con = DriverManager.getConnection(url, user, password);
while (rs.next()) {
System.out.print(rs.getInt(1));
System.out.print(": ");
System.out.println(rs.getString(2));
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(mySQL.class.getName());
lgr.log(Level.SEVERE, ex.getMessage(), ex);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pst != null) {
pst.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger lgr = Logger.getLogger(mySQL.class.getName());
lgr.log(Level.WARNING, ex.getMessage(), ex);
}
}
}
}