Telekommunikatsiya texnologiyalari va kasb ta’limi fakulteti “ims ga kirish” fanidan




Download 2.96 Mb.
bet5/10
Sana25.03.2024
Hajmi2.96 Mb.
#176220
1   2   3   4   5   6   7   8   9   10
Bog'liq
H.S
1. optik aloqa tizimlari Optik aloqa, uning afzalliklari va qo’l, 31-22-gr F. Yusupov, 10, Alisher Navoiy - Vikipediya, Web dasturlashga asoslangan amaliy tizimlar. Reja, 20-64 Xоразм мақомлари II (2), KLASSIK MAKTAB VAKILLARINING IQTISODIY QARASHLARI, МИКРОБИОЛОГИЧЕСКИЕ АСПЕКТЫ ГНОЙНО, Kalkulyator haqida umumiy tushuncha, (Lee)관광사업론-학생-100문제(한-우-출제용), KRASVORD, Pedagogik kompetentlik, Abdullayev Abdivaxob Xolmo’minovich, kOMPYTR TASHKIL ETISH doc, 197671223

Ishni bajarish tartibi:


Ishni NetBeans IDE ishlab chiqarish muhitini yuklash bilan boshlaymiz. Buning uchun ish stolida turgan NetBeans IDE muhiti yorlig‘ini sichqonchaning chap tomonini ikki marta bosib yuklaymiz. Bizning holatda NetBeans IDE muhiti ishga tushadi. Quyidagi rasmda NetBeans IDE muhitining asosiy darchasi yuklangan.

3.1- rasm. NetBeans IDE muhiti asosiy darchasi
Yaratilgan “Talabaning_Ismi” loyihasi ustiga sichqonchaning o‘ng tomoni bosiladi. Paydo bo‘lgan kontekst menyudan “Новый”→ “Класс Java” tanlanadi.

3.2- rasm. Yangi класс Javani yaratish


“New Класс Java” darchasida “Имя класса” maydoniga “CreateDB”, “Пакет” maydoniga “jdbc” kiritilib “Готово” tugmasi bosiladi.

3.3- rasm. NetBeans IDE muxiti “New Класс Java” darchasi
Yaratilgan CreateDB.java fayliga JDBC yordamida ma’lumotlar bazasini yaratuvchi Java kod kiritiladi.

3.4- rasm. Ma’lumotlar bazasini yaratish Java kodda quyidagilarni


bajarish lozim:

package jdbc;


import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class CreateDB {
static final String DATABASE_URL = "jdbc:mysql://localhost/"; static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String USER = "root";
static final String PASSWORD = "admin";
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection connection = null; Statement statement = null; try {
System.out.println("JDBC drayverni ro‘yxatdan o‘tkazish..."); Class.forName(JDBC_DRIVER); System.out.println("Ma'lumotlar bazasiga bog‘lanish...");
connection = DriverManager.getConnection(DATABASE_URL, USER, PASSWORD);
System.out.println("Ma'lumotlar bazasini yaratish..."); statement = connection.createStatement();
String SQL = "CREATE DATABASE talaba"; statement.executeUpdate(SQL);
System.out.println("Ma'lumotlar bazasi muvaffaqiyatli yaratildi...");
}
finally {
if(statement!=null){ statement.close();
}
if(connection!=null){ connection.close();
}
}
}
}
Ma’lumotlar bazasida jadvalni yaratish
NetBeans IDE muxitida CreateTable.java fayli yuqorida yaratilgan fayl kabi yaratiladi.

3.5- rasm. Jadvalni yaratish
Jadvalga yangi yozuv qo‘shish
NetBeans IDE muxitida InsertRecord.java fayli yuqorida yaratilgan fayllar kabi yaratilgan.

3.6- rasm. Jadvalga yozuvlar qo‘shish
import java.sql.*; public class DatabaseExample { public static void main(String[] args) { final String DB_URL = "jdbc:mysql://localhost:3306/mydatabase"; final String USER = "username"; final String PASS = "password"; try (Connection conn = DriverManager.getConnection(DB_URL, USER, PASS)) { System.out.println("Ma'lumotlar bazasiga muvaffaqiyatli ulanildi."); // Ma'lumot kiritish Scanner scanner = new Scanner(System.in); System.out.print("Ismingizni kiriting: "); String ism = scanner.nextLine(); System.out.print("Yoshingizni kiriting: "); int yosh = scanner.nextInt(); String sql = "INSERT INTO user (ism, yosh) VALUES (?, ?)"; try (PreparedStatement statement = conn.prepareStatement(sql)) { statement.setString(1, ism); statement.setInt(2, yosh); int rowCount = statement.executeUpdate(); System.out.println(rowCount + " qator ma'lumotlar bazasiga qo'shildi."); } // Ma'lumotlarni olish System.out.println("Bazadagi barcha ma'lumotlar:"); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT * FROM user"); while (rs.next()) { System.out.println("Ism: " + rs.getString("ism") + ", Yosh: " + rs.getInt("yosh")); } } catch (SQLException e) { System.err.println("Xatolik: " + e.getMessage()); } } }


  1. Download 2.96 Mb.
1   2   3   4   5   6   7   8   9   10




Download 2.96 Mb.

Bosh sahifa
Aloqalar

    Bosh sahifa



Telekommunikatsiya texnologiyalari va kasb ta’limi fakulteti “ims ga kirish” fanidan

Download 2.96 Mb.