Tampilkan postingan dengan label Java. Tampilkan semua postingan
Tampilkan postingan dengan label Java. Tampilkan semua postingan

JDBC - Make a connection Java and MySQL



On this occasion I
will share about how to make a connection with MySQL Java. What we need here is NetBeans as the IDE, which would free version of what happened to me using NetBeans 7.0, but that we also need the XAMPP for MySQL database. Immediately, I explain codingnya below:

1. First we make a Java class in NetBeans with the name "Koneksi.java"
2. Then type in the script as below
:
-------------------------------------------------------------------------------
public class Koneksi {
Connection con;
String driver,url,user,pass;

public Koneksi() {
this.driver="com.mysql.jdbc.Driver";
this.url="jdbc:mysql://localhost:3306/kuliah";
this.user="root";
this.pass="root";
}

public Koneksi(Connection con, String driver, String url, String user, String pass) {
this.con = con;
this.driver = driver;
this.url = url;
this.user = user;
this.pass = pass;
}

public void BukaKoneksi(){
try {
Class.forName(driver).newInstance();
con = DriverManager.getConnection(url,user,pass);
} catch (SQLException ex) {
Logger.getLogger(Koneksi.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(Koneksi.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(Koneksi.class.getName()).log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Koneksi.class.getName()).log(Level.SEVERE, null, ex);
}
}

public Connection getCon() {
return con;
}

public void setCon(Connection con) {
this.con = con;
}
}
-------------------------------------------------------------------------------

Reading Input from User Using JOptionPane in Java

In addition to using a BufferedReader like my post before this. Read input from the user can also use the JOptionPane. I am here using JCreator. Simply, in the example script :

------------------------------------------------------------------------------------------------
import javax.swing.*;
public class inputjop {
public static void main(String [] args) {
String input = JOptionPane.showInputDialog("Masukkan nama anda : ");

String nama = "Terimakasih, " + input;
JOptionPane.showMessageDialog(null,nama);
}
}
------------------------------------------------------------------------------------------------


Having completed the above script is typed, press F5, then the output will appear as shown below :




Reading Input from User Using BufferedReader in Java

Below I will give you an example script for reading the input string typed by the user. In this example I am using JCreator where this application can compile and run pulled we type java file, without opening Commandprompt. But if you do not have JCreator please use Notepad, because it just wrote.
On this occasion I will use the BufferedReader, direct me give you an example script:
 
------------------------------------------------------------------------------------------
import java.io.*;
public class inputbuffer {
public static void main (String [] args) {
System.out.println("Masukkan nama anda : ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String nama = null;
try {
nama = br.readLine();
}catch (IOException ex) {
System.out.println("System Error!");
System.exit(0);
}
System.out.println("Terima kasih " + nama);
}
}

------------------------------------------------------------------------------------------

Once the script is written above then press F5, then the script will run on JCreator:
 
 
Input / entry your name, example: iToru. Kaan then the output looks like this:


The introduction of Java (Hello World!)

To start learning java, I'll give an example of a simple coding that is commonly given if we want to learn Java for the first time, the Hello World!.

To be easy here I use the Windows Operating System (XP or the free will of 7). First open "Notepad". Then type the following script:

-----------------------------------------------------------------
public class HelloWorld {
public static void main {String [] args) {
System.out.println("Hello World!");
}
}
-----------------------------------------------------------------
Then save the name HelloWorld.java, remember the file name must match the class name is used. Then open Commandprompt. For example, we store the files on drive C: \. So on our Commandprompt typing the following:

---------------------------------
C:\javac HelloWorld.java
C:\java HelloWorld
HelloWorld!
---------------------------------
HelloWorld! , The output of the script that we wrote above earlier. How? not easy.