JDBC - Java Database Connectivity

 I started to work JDBC project on Maven. First of all we have to add properties and dependency for it:

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
so version in the future may be different so please pay attention to it.
Then via Java we have to connect to our Database(DB).
String url = "jdbc:mysql://localhost:3306/resume?serverTimezone = UTC";
String username = "root";
String password = "112358";

Connection c = DriverManager.getConnection(url, username, password);
Statement stmt = c.createStatement();
stmt.execute("select * from user");
ResultSet rs = stmt.getResultSet();
Connection is an interface in JDBC library. DriverManager is a class and has
static getConnection method which returns Connection.

Connection is like a line between our application and DB. Statement is like a ship which traverls
between Java and DB. Execure it is command that we send to DB via ship. And eventually 
DB gives us ResultSet that contains information according to execute.
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
String surname = rs.getString("surname");
String email = rs.getString("email");
String phone = rs.getString("phone");

System.out.println("id = " + id);
System.out.println("name = " + name);
System.out.println("surname = " + surname);
System.out.println("email = " + email);
System.out.println("phone = " + phone);
System.out.println("---------------------");
}
Statement is an interface executes the given SQL statement which returns a signle object.
Statement is also in JDBC libarary.
RsultSet is an interface in JDBC library that moves the cursor forward one row from its current position.
PreparedStatement is an interface in JDBC libraty, which executes the SQL query.
%3CmxGraphModel%3E%3Croot%3E%3CmxCell%20id%3D%220%22%2F%3E%3CmxCell%20id%3D%221%22%20parent%3D%220%22%2F%3E%3CmxCell%20id%3D%222%22%20value%3D%22%22%20style%3D%22whiteSpace%3Dwrap%3Bhtml%3D1%3Baspect%3Dfixed%3B%22%20vertex%3D%221%22%20parent%3D%221%22%3E%3CmxGeometry%20x%3D%2240%22%20y%3D%22100%22%20width%3D%2280%22%20height%3D%2280%22%20as%3D%22geometry%22%2F%3E%3C%2FmxCell%3E%3C%2Froot%3E%3C%2FmxGraphModel%3E

Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class