import java.sql.*;
public class DatabaseConnectionExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/your_database";
String username = "root";
String password = "your_password";
try {
Connection connection = DriverManager.getConnection(url, username, password);
System.out.println("Connection established!");
Statement statement = connection.createStatement();
String query = "SELECT * FROM your_table";
ResultSet resultSet = statement.executeQuery(query);
while (resultSet.next()) {
System.out.println(resultSet.getString("column_name"));
}
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter