JSP pages can be put under public_html folder or under any
sub directory
in public_html folder.
Your JavaBeans and Classes should be packaged and put under
public_html -> WEB-INF -> classes -> YourPackageName. If you don't
package your Java codes, it may not work.
Before you connect to your database, you need to create a database from
your control panel's MySQL. On this server, your database name looks like
YourUserName_DBName. So, if your control panel username is
foo and you
give the DB name MyDB from control panel, it will be referred to as
foo_MyDB. When you refer to your database from JSP or
Servlet, you should
refer your database as foo_MyDB instead of MyDB.
Driver and connection strings should be like follows:
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost/YourDBNameHere";
You should also specify your username and password. The default username
and password are the ones you use for your control panel.
You can use the following JSP code to test your DB connection. You just need
to replace the database name, username and password with your own. Put this
page under public_html. Again, before you test it, you have to make
sure you have the driver .jar file in the
lib folder.
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%!
// mysql driver
String driver = "com.mysql.jdbc.Driver";
// the "url" to our DB, the last part is the name of the DB
String url = "jdbc:mysql://localhost/YourDBName";
// the default DB username and password may be the same as your control panel login
String name = "username";
String pass = "password";
%>
<html>
<head>
<title>testServlet</title>
</head>
<body>
<p>Attempting to open JDBC connection to:... </p> <%=url%>
<%
try
{
// Test the DB connection by making an empty table
String tableStr = "CREATE TABLE test (testid mediumint(8), name varchar(100))";
Class.forName( driver );
// initialize the Connection, with our DB info ...
Connection con = DriverManager.getConnection( url, name, pass );
Statement stat = con.createStatement();
%>
<p> executing: <%=tableStr%></p>
<%
stat.executeUpdate( tableStr );
%>
<p> success.... </p>
<%
// close connection
con.close();
}
catch (SQLException sqle)
{ out.println("<p> Error opening JDBC, cause:</p> <b> " + sqle + "</b>"); }
catch(ClassNotFoundException cnfe)
{ out.println("<p> Error opening JDBC, cause:</p> <b>" + cnfe + "</b>"); }
%>
</body>
</html>