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";
ublic 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
qodir_habibullayev"; statement.executeUpdate(SQL);
System.out.println("Ma'lumotlar bazasi
muvaffaqiyatli yaratildi...");
}
finally {
if(statement!=null){
statement.close();
}
if(connection!=null){
connection.close();
}
}
}
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package laboratoriya_13;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class CreateTable {
static final String DATABASE_URL =
"jdbc:mysql://localhost/qodir_habibullayev";
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
tatic 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("Tanlangan ma'lumotlar bazasida
jadval yaratish...");
statement = connection.createStatement();
String SQL = "CREATE TABLE dasturchilar
"
+ "(id INTEGER not NULL, "
+ " name VARCHAR(50), "
+ " specialty VARCHAR (50), "
+ " salary INTEGER not NULL, "
+ "
PRIMARY
KEY
(id))";
statement.executeUpdate(SQL);
System.out.println("Jadval muvaffaqiyatli yaratildi...");
finally {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}
}
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package laboratoriya_13;
import java.sql.*;
public class InsertRecord {
static final String DATABASE_URL =
"jdbc:mysql://localhost/qodir_habibullayev";
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("Yozuvlarni qo'shish...");
statement = connection.createStatement();
String SQL = "INSERT into dasturchilar VALUE (1,
'Qodir Khabibullayev', 'Frontend Developer', 1000)";
tatement.executeUpdate(SQL);
SQL = "INSERT into dasturchilar VALUE (2, 'Sherzod
Pirmatov', 'Backend Developer', 2500)";
statement.executeUpdate(SQL);
SQL = "INSERT into dasturchilar VALUE (3, 'Aliyev
Vali', 'Mobile Developer', 2000)";
statement.executeUpdate(SQL);
SQL = "INSERT into dasturchilar VALUE (4, 'Sarvar
Karimov', 'Frontend Developer', 1000)";
statement.executeUpdate(SQL);
} finally {
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
}
}
}
Kod natijasi
|