Write a program using servlet and JDBC for developing an onine application to create a database?

1 answer

Answer

1238868

2026-03-31 16:00

+ Follow

Java
Java

// You can use this sample servlet as a starting point to connect to

// your database included with useractive account.

//

MySQL
MySQL

// Portions copied from Sun's HelloWorldServlet.Java

// You are free to use this code in any way you chose

import Java.io.*;

import Javax.servlet.*;

import Javax.servlet.http.*;

import Java.sql.*;

public

class HelloWorldServlet extends HttpServlet {

public void doGet (HttpServletRequest req, HttpServletResponse res)

throws ServletException, IOException {

res.setContentType("text/html");

ServletOutputStream out = res.getOutputStream();

out.println("<html>");

out.println("<head><title>Hello World</title></head>");

out.println("<body>");

try {

// The newInstance() call is a work around for some

// broken Java implementations

out.println("asdf");

Class.forName("org.gjt.mm.MySQL.Driver").newInstance();

}

catch (Exception E) {

out.println("Unable to load driver.");

E.printStackTrace();

}

out.println("<br><hr>");

try {

Connection Conn =

DriverManager.getConnection("jdbc:MySQL://sql.useractive.com/USERNAME?user=USERNAME&passWord=PASSWord");

// Do something with the Connection

Statement Stmt = Conn.createStatement();

ResultSet RS = Stmt.executeQuery("SELECT * from SOMETABLE");

while (RS.next()) {

out.println(RS.getString(1));

}

// Clean up after ourselves

RS.close();

Stmt.close();

Conn.close();

}

catch (SQLException E) {

out.println("SQLException: " + E.getMessage());

out.println("SQLState: " + E.getSQLState());

out.println("VendorError: " + E.getErrorCode());

}

out.println("<h1>Hello World. Sun... 1.4</h1>");

out.println("</body></html>");

}

public String getServletInfo() {

return "Create a page that says <i>Hello World</i> and send it back";

}

}

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.