Program JDBC pisanan

All lectures for JV purposes
tingkat , wulangan
kasedhiya

Maven

Rincian sing paling penting yaiku perpustakaan Driver JDBC kanggo server MySQL dhewe. Iku mung ora bakal katon ing komputer, dadi sampeyan kudu nambah ing kono.

Yen sampeyan nggunakake Maven, sampeyan kudu nulungi dheweke kanthi nuduhake perpustakaan sing bener.

MySQL JDBC driver :

   	<dependency>
        	<groupId>mysql</groupId>
        	<artifactId>mysql-connector-java</artifactId>
        	<version>8.0.29</version>
    	</dependency>

Cukup tambahake kode iki menyang pom.xml sampeyan .

Aku uga bakal menehi sawetara pembalap liyane populer.

Driver JDBC kanggo PostgeSQL :

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.4.0</version>
</dependency>

Driver JDBC kanggo Oracle :

<dependency>
    <groupId>com.oracle.database.jdbc</groupId>
    <artifactId>ojdbc8</artifactId>
    <version>21.5.0.0</version>
</dependency>

Driver JDBC kanggo H2 :

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>2.1.214</version>
</dependency>

Pitakonan database pisanan

Kabeh perpustakaan sing dibutuhake wis kalebu, saiki wektune kanggo mbukak program akses database pisanan. Kita bakal nulis kabeh kode ing cara utama () .

tataran 1 . Tambah sawetara impor sadurunge cara utama () - iki bakal nggawe urip luwih gampang:

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;

tataran 2 . Pisanan kita kudu nggawe sambungan database. Kanggo nindakake iki, ing cara utama () nulis kode ing ngisor iki:

Connection connection  = DriverManager.getConnection(
           "jdbc:mysql://localhost:3306/test",
           "login", "password");

Sampeyan uga bisa nindakake ing siji baris - endi sing luwih trep kanggo sampeyan. Mlebet lan sandhi, mesthi, sampeyan kudu ngganti sing asli saka server MySQL lokal.

tataran 3 . Kita nggawe pitakon menyang database. Ayo entuk kabeh pangguna saka tabel pangguna. Banjur sampeyan kudu nambah baris kode iki:

Statement statement = connection.createStatement();
ResultSet results = statement.executeQuery("SELECT * FROM user");

Iku rong baris. Ing kawitan kita nggawe obyekPratelan, lan ing kaloro, kita nggunakake kanggo query database. Metode executeQuery() nglakokake query database lan ngasilake obyek saka jinisset asil.

tataran 4 . Tampilake data sing ana ing obyek kasebutset asil.

set asil- iku ora pesawat, iku mung disebut. Iki nyimpen asil eksekusi query. Objek iki meh padha karo iterator: ngidini sampeyan nyetel / ngganti baris asil saiki, banjur sampeyan bisa entuk data saka baris saiki. Tambah kode ing ngisor iki kanggo conto sampeyan:

while (results.next()) {
        	Integer id = results.getInt(1);
        	String name = results.getString(2);
        	System.out.println(results.getRow() + ". " + id + "\t"+ name);
}

Cara sabanjure () ngganti baris asil saiki menyang sabanjure. Ngasilake bener yen ana baris kasebut, lan salah yen ora ana garis maneh.

Banjur saka baris obyek saikiset asilsampeyan bisa entuk data saka kolom kasebut:

  • getRow () - ngasilake nomer baris saiki ing obyekset asil
  • getInt (N) - bakal ngasilake data kolom N saka baris saiki minangka int
  • getString (N) - bakal ngasilake data kolom N saka baris saiki minangka String

Daftar program lengkap

Kanggo nyilikake kesalahan, iki dhaptar lengkap program:

package org.example;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class JdbcApplicatin {
    public static void main(String[] args) throws  Exception{
 	   Connection connection  = DriverManager.getConnection(
          	"jdbc:mysql://localhost:3306/test",
          	"root", "secret");

        Statement statement = connection.createStatement();
    	ResultSet results = statement.executeQuery("SELECT * FROM user");

    	while (results.next()) {
        	Integer id = results.getInt(1);
        	String name = results.getString(2);
        	System.out.println(results.getRow() + ". " + id + "\t"+ name);
    	}
    	connection.close();
    }
}

Lan output layar sawise mbukak program:

"C:\Program Files\Java\jdk-17.0.3.1\bin\java.exe...
sewelas Iwan Iwan
2.2 Petrov Nikolay
3.3 Sidorov Vitaly
Proses rampung kanthi kode metu 0
Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION