Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.sql.*;
- public class MainSQL {
- private Connection connection;
- private Statement statement;
- public MainSQL() {
- try {
- Connect();
- } catch (Exception e) {
- System.out.println(e);
- }
- System.out.println("Connection established");
- try {
- receiveData();
- } catch (SQLException e) {
- System.out.println(e);
- }
- // Disconnect();
- }
- private void receiveData() throws SQLException {
- String sqlStatement = "select * from listofartists where ArtistID IN (select ArtistID from artists.work);";
- statement = connection.createStatement();
- ResultSet rs = statement.executeQuery(sqlStatement);
- int cols = rs.getMetaData().getColumnCount();
- for (int i = 1; i <= cols; i++) {
- System.out.print(rs.getMetaData().getColumnName(i) + " ");
- }
- System.out.println();
- while(rs.next()){
- for (int i = 1; i <= cols; i++) {
- String value = rs.getString(i);
- System.out.print(value + " ");
- }
- System.out.println();
- }
- rs.close();
- statement.close();
- }
- private void Connect() throws Exception {
- if(connection != null) return;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- throw new Exception("Driver not found");
- }
- String url = "jdbc:mysql://localhost:3306/artists?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
- connection = DriverManager.getConnection(url, "root", "chen");
- }
- private void Disconnect() {
- if(connection != null){
- try {
- connection.close();
- } catch (SQLException e) {
- System.out.println("Could not close connection");
- }
- }
- }
- public static void main(String[] args) {
- new MainSQL();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement