Pembuatan Database Students

2:59 PM Admin 0 Comments

SCRIPT:

/*
 * 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 database;
import java.sql.*;

public class Database {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      Connection conn = null;
      Statement stmt = null;
      try{
        //Register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        //Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "");
        //Execute a query
        System.out.println("Creating database...");
        stmt = conn.createStatement();    
        stmt.executeUpdate("create database students");
     
        System.out.println("Database created successfully...");    
      } catch(Exception e) {
        //Handle errors
        e.printStackTrace();
      }//end try
      //System.out.println("Goodbye!");    
   }//end main
}


PEMBUATAN TABLE

Keterangan: Pembuatan Tabel Mahasiswa dengan kolom NIM dan Nama.
SCRIPT:
/*
 * 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 database;
import java.sql.*;

public class Database {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
            Connection conn = null;
      Statement stmt = null;
      try{
        //Register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        //Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root", "");
//Execute a query
        System.out.println("Creating tables...");
        stmt = conn.createStatement();    
        stmt.executeUpdate("create table mahasiswa(nim char(8), nama varchar(30))");
     
        System.out.println("Tables created successfully...");    
      } catch(Exception e) {
        //Handle errors
        e.printStackTrace();
      }//end try
      //System.out.println("Goodbye!");    
   }//end main
}

Memasukan Data

Keterangan: Data yang simasukan adalah
Nama: Anto Ibrahim
NIM: 58552.004.KL
SCRIPT:
/*
 * 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 database;
import java.sql.*;

public class Database {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      Connection conn = null;
      Statement stmt = null;
      try{
        //Register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        //Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root", "");
//Execute a query
        System.out.println("Insert into table...");
        stmt = conn.createStatement();    
        stmt.executeUpdate("insert into mahasiswa values('14N10001','Akbar Ibrahim')");
     
        System.out.println ("pengisian tabel sudah dilakukan dengan sukses...!");
      } catch(Exception e) {
        //Handle errors
        e.printStackTrace();
      }//end try
      //System.out.println("Goodbye!");    
   }//end main
}

HAPUS DATA

/*
 * 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 database;
import java.sql.*;

public class Database {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      Connection conn = null;
      Statement stmt = null;
      try{
        //Register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        //Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root", "");
//Execute a query
        System.out.println("Hapus data");
        stmt = conn.createStatement();    
        stmt.executeUpdate("delete from student where nim='14N10001'");
     
        System.out.println ("Data sudah dihapus dengan sukses...!");
      } catch(Exception e) {
        //Handle errors
        e.printStackTrace();
      }//end try
      //System.out.println("Goodbye!");    
   }//end main
}

GANTI DATA

/*
 * 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 database;
import java.sql.*;
import static org.omg.CORBA.AnySeqHelper.insert;

public class Database {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
      Connection conn = null;
      Statement stmt = null;
      try{
        //Register JDBC driver
        Class.forName("com.mysql.jdbc.Driver");

        //Open a connection
        System.out.println("Connecting to database...");
        conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root", "");
//Execute a query
        System.out.println("Perbarui data");
        stmt = conn.createStatement();    
        stmt.executeUpdate("update into mahasiswa values('14N10001','Bagus Ibrahim')");
     
        System.out.println ("Data sudah Diperbarui dengan sukses...!");
      } catch(Exception e) {
        //Handle errors
        e.printStackTrace();
      }//end try
      //System.out.println("Goodbye!");    
   }//end main
}


0 comments: