mysql -u root -p
show databases;
create database hostelapp_db;
use
In order to connect to a database we need to use a java.sql.Connection object. We can do this using the getConnection()
//MySQL driver is loaded Class.forName( "com.mysql.jdbc.Driver" ); //Connection object is created using the db host and credentials Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/countries?" + "user=root&password=root" );
DBC connection supports two different ways of working: autocommit=true and autocommit=false. The first one commits all transactions directly to the database, the second one needs an special command in order to commit or rollback the transactions
The DriverManager class is in charge of loading the proper database, after this is loaded, the code that access the database for querying and modifying data will remain (more or less) unchanged.
JDBC offers the possibility to execute a list of SQL statements as a batch, that is, all in a row.