Minggu, 19 Juni 2016

SQL Dan JDBC

 membuat koneksi ke Database Melalui class DriverManager, membuat koneksi ke Database Melalui class DataSource, hingga melakukan Operasi INSERT, UPDATE, DELETE dan SELECT
disini saya menggunakan MariaDB 10.1
berikut adalah cara menginstal MariaDB 10.1 :
1.JPG
pilih next jika muncul tampilan seperti gambar diatas
2.JPG
3.JPG
klik next
4.JPG
masukan password root
5
6
7
setelah selesai menginstal MariaDB kemudian buat database baru dngan nama jeni_sql
kemudian buat table friends
CREATE TABLE friends (
id int(10) unsigned NOT NULL auto_increment,
Nama varchar(45) NOT NULL,
Email varchar(100) NOT NULL,
Website varchar(55) NOT NULL,
Alamat varchar(255) NOT NULL,
PRIMARY KEY (‘id’)
Memasukkan data sesuai urutan field.
 INSERT INTO friends VALUES (1, ‘Agung Pribadi’,
‘agung@negaraku.com’, ‘http://www.agung.info’, ‘Surabaya, Indonesia’);
setelah itu buat project baru
• Buat project web baru dengan nama sql_jdbc
• Tambahkan librari mysql-connector-java_xx.jar ke dalam CLASSPATH aplikasi. Dapat dilakukan dengan 2 cara:
• Secara manual, kopikan librari tersebut ke dalam folder WEB-INF/lib.
• Melalui Netbeans IDE: Pada tab Projects, klik kanan pada Libraries, pilih Add JAR/Folder,  dan pilih file librari yang akan ditambahkan. Ketika proses Build Project, file tadi akan dikopikan ke folder WEB-INF/lib oleh Netbeans secara otomatis.
koneksi ke database melalui class DriverManager dalam web Java.
Langkah-langkahnya:
Mengetes koneksi DriverManager dalam aplikasi Servlet :
• Buat link dalam file index.jsp:
<h4>Driver Manager</h4> <a href=”TestDMConnection”>Test Koneksi Driver Manager (Servlet)</a><br/> <a href=”TestJspDMConnection.jsp”>Test Koneksi Driver Manager (JSP)</a><br/>
• Pada Source Packages Projects Explorer, buat class dengan nama
TestServletDMConnection, dan isikan kode berikut:
import java.io.*;
import java.util.Enumeration;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.net.*;
public class TestServletDMConnection extends HttpServlet{
Connection theConnection;
private ServletConfig config;
public void init(ServletConfig config)
throws ServletException{
this.config=config;
}
public void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
HttpSession session = req.getSession(true);
res.setContentType(“text/html”);
PrintWriter out = res.getWriter();
out.println(“<HTML><HEAD><TITLE>Emai List.</TITLE>”);
out.println(“</HEAD>”);
out.println(“<BODY bgColor=blanchedalmond text=#008000 topMargin=0>”);
out.println(“<P align=center><FONT face=Helvetica><FONT
color=fuchsia style=\”BACKGROUND-COLOR: white\”><BIG><BIG>List of Address
Book.</BIG></BIG></FONT></P>”);
out.println(“<P align=center>”);
out.println(“<TABLE align=center border=1 cellPadding=1
cellSpacing=1 width=\”75%\”>”);
out.println(“<TR>”);
out.println(“<TD>Name</TD>”);
out.println(“<TD>E-mail</TD>”);
out.println(“<TD>Website</TD>”);
out.println(“<TD>Alamat</TD>”);
out.println(“</TR>”);
try{
Class.forName(“com.mysql.jdbc.Driver”);
theConnection =
DriverManager.getConnection(“jdbc:mysql://localhost/jeni_sql”,”root”,”passw
ord”);
Statement theStatement=theConnection.createStatement();
ResultSet theResult=theStatement.executeQuery(“select * from
friends”); //Select all records from emaillists table.
while(theResult.next()) //Fetch all the records and print in table
{
out.println();
out.println(“<TR>”);
out.println(“<TD>” + theResult.getString(“nama”) +
“</TD>”);
out.println(“<TD>” + theResult.getString(“email”) +
“</TD>”);
String s=theResult.getString(“website”);
out.println(“<TD><a href=” + s + “>” + s + “</a></TD>”);
out.println(“<TD>” + theResult.getString(“alamat”) +
“</TD>”);
out.println(“</TR>”);
}
theResult.close();//Close the result set
theStatement.close();//Close statement
theConnection.close(); //Close database Connection
}catch(Exception e){
out.println(e.getMessage());//Print trapped error.
}
out.println(“</TABLE></P>”);
out.println(“<P> </P></FONT></BODY></HTML>”);
}
public void destroy(){
}
}
• Buka file web.xml, tambahkan konfigurasi servlet berikut:
<web-app>
<servlet>
<servlet-name>TestDM</servlet-name>
<servlet-class>TestServletDMConnection</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TestDM</servlet-name>
<url-pattern>/TestDMConnection</url-pattern>
</servlet-mapping>
</web-app>
• Run → Run Project
Mengetes koneksi DriverManager dalam aplikasi JSP:
• Buat file JSP dengan nama TestJspDMConnection.jsp dalam folder Web Pages
<%@ page import=”java.sql.*” %>
<%
String connectionURL = “jdbc:mysql://localhost:3306/jeni_sql”;
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
%>
<html><body>
<p align=center>
<font face=”Helvetica” color=”fuchsia” style=”background-color:
white”><big><big>List of Address Book.</big></big></font></p>
<p align=center>
<table align=center border=1 cellPadding=1 cellSpacing=1 width=”75%”>
<tr align=”center”>
<td><b>Name</b></td>
<td><b>E-mail</b></td>
<td><b>Website</b></td>
<td><b>Alamat</b></td>
</tr>
<%
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
connection = DriverManager.getConnection(connectionURL, “root”, “password”);
statement = connection.createStatement();
rs = statement.executeQuery(“SELECT * FROM friends”);
while (rs.next()) {
out.println(“<tr>”);
out.println(“<td>” + rs.getString(“nama”) + “</td>”);
out.println(“<td>” + rs.getString(“email”) + “</td>”);
String s=rs.getString(“website”);
out.println(“<td><a href=” + s + “>” + s + “</a></td>”);
out.println(“<td>” + rs.getString(“alamat”) + “</TD>”);
out.println(“</tr>”);
}
rs.close();
statement.close();
connection.close();
%>
</table>
</body>
</html>
• Run → Run Project

Tidak ada komentar:

Posting Komentar