Presentation is loading. Please wait.

Presentation is loading. Please wait.

J2EE (Enterprise Programing)

Similar presentations


Presentation on theme: "J2EE (Enterprise Programing)"— Presentation transcript:

1 J2EE (Enterprise Programing)
Koneksi Database

2 KONEKSI DATABASE Database DSN Coding java Convert ke class
Deploy tools Cek di Browser

3 Membuat Database Buat database “dbmahasiswami” yang di dalamnya terdapat 1 tabel : tbMahasiswa FIELD TYPE KETERANGAN Nim Varchar (8) Primary key Nama Varchar (20) Alamat Varchar (30)

4 Membuat DSN Buka Control Panel >> Administrative Tools >> Data Sources (ODBC) Pada tab User DSN Pilih Add >> MySQL ODBC 5.1 Driver Data Source Name = mahasiswami Server = localhost User = root

5 Coding Koneksi (1) import java.io.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; public class koneksi extends HttpServlet{ PrintWriter out=null; Connection con=null; ResultSet rs=null; public void service (HttpServletRequest rq, HttpServletResponse rs) throws ServletException, IOException{ out=rs.getWriter();

6 Coding Koneksi (2) try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:mahasiswami","root",""); out.println("Koneksi Berhasil"); } catch(Exception e){ out.println (e.toString()); out.println("Koneksi Gagal");

7 Ubah coding koneksi ke file class
Jalankan di “deploytool” Cek koneksi di browser anda

8 Menampilkan Isi Database
Coding java Convert ke class Deploy tools Cek di Browser

9 Mengisi Database Isi tbMahasiswa dengan data sebagai berikut : NIM
Nama Alamat 101001 John Jakarta 101002 Peter Sukabumi 101003 Clara Cirebon 101004 Veronica Jogja 101005 Michael Bandung

10 Coding (1) import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.sql.*; public class lihatdatabase extends HttpServlet{ static int i; Connection con; PrintWriter out; ResultSet rs; public void init(){ i = 0; con=null; out=null; rs=null; } //close void init()

11 Coding (2) public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ i++; out=response.getWriter(); out.println("<B>You are user no. " + i +" to visit this site.</B><BR><BR>"); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); con=DriverManager.getConnection("jdbc:odbc:mahasiswa","root",""); PreparedStatement pstmt=null; String query=null; query= "select * from tb_mahasiswa"; pstmt=con.prepareStatement(query); rs=pstmt.executeQuery(); out.println("<B><center>Data Mahasiswa</center></B>"); ResultSetMetaData rsmd= rs.getMetaData(); int colcount=rsmd.getColumnCount();

12 Coding (3) out.println("<TABLE ALIGN = CENTER BORDER=1 CELLPADDING=2>"); out.println("<TR>"); out.println("<TH>NIM</TH>"); out.println("<TH>Nama</TH>"); out.println("<TH>Alamat</TH>"); out.println("</TR>"); while(rs.next()){ out.println("<TD>" + rs.getString("nim") + "</TD>"); out.println("<TD>" + rs.getString("nama") + "</TD>"); out.println("<TD>" + rs.getString("alamat") + "</TD>"); } // close while out.println("</TABLE>"); out.println("</BODY>"); } // close try

13 Coding (4) catch(Exception e){ out.println(e.toString()); } // close catch } // close void doGet public void destroy(){ try{ i = 0; con.close(); out.close(); rs.close(); } // close try catch (SQLException se){ out.println(se.toString()); } //close void destroy() } //close class

14 Simpan coding di lihatdatabasemi.java Ubah file java ke file class
Deploytools Context Root : praktikumtigami Aliases : lihatdatabasemi

15 Cek di Browser

16 TERIMA KASIH


Download ppt "J2EE (Enterprise Programing)"

Similar presentations


Ads by Google