/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package m9; import com.mysql.jdbc.Connection; import com.mysql.jdbc.Statement; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author ASUS */ public class Database { private Connection con = null; private Statement stmt = null; private ResultSet rs = null; private String sql = null; public void openDataBase() throws Exception { try { Class.forName("com.mysql.jdbc.Driver"); con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost/Udinus?" + "user=root&password="); stmt=(Statement) con.createStatement(); System.out.println("Database Opened"); } catch (Exception e) { throw e; } } void InsertData(String nim, String nama, String alamat, int umur) throws SQLException{ try{ sql = "insert into mahasiswa values ('"+ nim +"','"+ nama +"','"+ alamat +"',"+ umur +")"; stmt.executeUpdate(sql); } catch (SQLException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex); } } ResultSet SelectData() throws SQLException{ try { rs = null; sql = "select * from mahasiswa"; rs = stmt.executeQuery(sql); } catch(Exception ex){ System.out.println("Select error : "+ex.getMessage()); } return rs; } void Close(){ try { stmt.close(); con.close(); System.out.println("Databse Closed"); } catch (SQLException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex); } } void CreateTable(){ try { sql = "CREATE TABLE IF NOT EXISTS `mahasiswa2` ( " + "`NIM` varchar(15) NOT NULL, " + "`Nama` varchar(20) NOT NULL, " + "`Alamat` varchar(30) NOT NULL, " + "`Umur` int(4) NOT NULL, " + "PRIMARY KEY (`NIM`))"; stmt.executeUpdate(sql); } catch (SQLException ex) { Logger.getLogger(Database.class.getName()).log(Level.SEVERE, null, ex); } } }