Welcome to Abdul Malik Ikhsan's Blog

jsp dan mysql (pendahuluan)

Posted in Tutorial JSP by samsonasik on April 4, 2007

<%
/*
**pertama kita buat database di mysql, contoh dengan nama “nama_database”
**dan kita buat table dengan nama “t_mhs” dengan field-field-nya
**npm,nama,alamat,kota
**kedua, buat file koneksi, contoh opendb.jsp (pastikan anda mempunyai web-inf
**yang sudah ada com-nya dalam folder classes)
**
*/
%>

<%@ page import=”java.sql.*”%>
<%
Class.forName(“com.mysql.jdbc.Driver”);
String url=”jdbc:mysql:3306//nama_database?user=root&password=”;
Connection con=DriverManager.getConnection(url);

Statement st=con.createStatement();
%>
==============================================
kita buat file baru, contoh tampilmhs.jsp
<%@ include file=”opendb.jsp” %>
<table width=”79%” border=”1″>
<tr bgcolor=yellow>
<td>No.</td>
<td>NPM</td>
<td>Nama Mahasiswa</td>
<td>Alamat</td>
<td>Kota</td>
</tr>
<%
//seleksi table t_mhs
ResultSet rs=st.executeQuery(“select * from t_mhs order by npm”);

int no=0;
while (rs.next())
{ //mulai looping
no++;
String npm=rs.getString(“npm”);
String nama=rs.getString(“nama”);
String alamat=rs.getString(“alamat”);
String kota=rs.getString(“kota”);
%>
<tr>
<td><%=no%>.</td>
<td><%=npm%></td>
<td><%=nama%></td>
<td><%=alamat%></td>
<td><%=kota%></td>
</tr>
<%
}  //akhir looping
%>
/table>

*************************************************
run opendb.jsp, http://localhost:8080/opendb.jsp
run tampilmhs.jsp, http://localhsot:8080/tampilmhs.jsp

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: