Frequently Asked Questions

Help Center Search

Connecting to a MySQL Database on Your Linux Hosting Account Using JSP

Print this Article
Comment on this Article
Last Updated: February 8, 2008 11:43 AM

This example describes using JSP to connect to a MySQL Database on a Linux hosting account.

<%@ page contentType="text/html; charset=iso-8859-1" language="java"
import="java.sql.*" errorPage="" %>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body><h1>GDtest
</h1>
<% String url = "jdbc:mysql://mysql server:3306/databasename";
String user= "";
String pass= "";
try{
Class.forName ("com.mysql.jdbc.Driver").newInstance ();
Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("mysql statment");

 


while(rs.next())
{
%>
<table> <tr>
<td><%= rs.getString("fname") %></td>
<td><%= rs.getString("lname") %></td>
</tr></table>
<%}
rs.close();
conn.close();
}catch(Exception e)
{
out.println(e.toString());
}
%>
</body>
</html>