package jdbc;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCConnection2
{
public static void main(String[] args) throws SQLException, ClassNotFoundException
{
// Maven dependency "mysql-connector-java" can also be used instead of below line
// Load the MySQL JDBC driver
Class.forName("com.sql.cj.jdbc.Driver");
// Connect to the MySQL database
String url = "jdbc:mysql://localhost:3306/mydatabase";
String username = "root";
String password = "root";
Connection connection = DriverManager.getConnection(url, username, password);
// Create a statement
Statement statement = connection.createStatement();
// Execute a query
ResultSet resultSet = statement.executeQuery("SELECT * FROM users");
// Process the results
while (resultSet.next())
{
System.out.println(resultSet.getString("username"));
}
// Close the connection
connection.close();
}
}
No comments:
Post a Comment