Skip to content

Instantly share code, notes, and snippets.

@extsalt
Created June 19, 2021 03:54
Show Gist options
  • Save extsalt/2ad18a53f546d35f9f7ac9475e42018e to your computer and use it in GitHub Desktop.
Save extsalt/2ad18a53f546d35f9f7ac9475e42018e to your computer and use it in GitHub Desktop.
Read from database
import java.sql.*;
public class Main {
public static void main(String... args) throws ClassNotFoundException, SQLException {
Connection connection = null;
//Loading of driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Getting connection from driver
connection = DriverManager.getConnection("jdbc:mysql://localhost/srgp?user=root&password=");
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from students");
System.out.println("Id\t\tName\tEno\tGender\tEmail");
while (resultSet.next()) {
System.out.println(
resultSet.getInt("id") + "\t\t" +
resultSet.getString("name") + "\t" +
resultSet.getString("e_no") + "\t" +
resultSet.getString("gender") + "\t" +
resultSet.getString("email")
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment