Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

MySQL In Servlet Problem . . .

843838May 12 2007 — edited May 12 2007
When I Try To Compile The Following Code It Gives Error I Don't Know Why . . I'm Totally A Newbie to Java & JSP
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
public class HelloWorldExample extends HttpServlet {
    public void doGet(HttpServletRequest request,HttpServletResponse response)
	throws IOException, ServletException
	{
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();	
		Class.forName("com.mysql.jdbc.Driver").newInstance();
		Connection DB_CONNECTION = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?user=root;password="); 
		Statement DB_STATEMENT = DB_CONNECTION.createStatement();	
		ResultSet RS = DB_STATEMENT.executeQuery("SELECT * FROM test ORDER BY test_id");
		String x="";
		int y=1;
		while (RS.next()) {
			x=RS.getString("test_name");
			if(x.length()>4){
				out.print(x.substring(3)+"<br />");
			}
		}		
		
    }
}
I Got The Following Errors:


HelloWorldExample.java:24: unreported exception java.lang.ClassNotFoundException
; must be caught or declared to be thrown
        Class.forName("com.mysql.jdbc.Driver").newInstance();
                     ^
HelloWorldExample.java:24: unreported exception java.lang.InstantiationException
; must be caught or declared to be thrown
        Class.forName("com.mysql.jdbc.Driver").newInstance();
                                                          ^
HelloWorldExample.java:25: unreported exception java.sql.SQLException; must be c
aught or declared to be thrown
        Connection DB_CONNECTION = DriverManager.getConnection("jdbc:mysql://loc
alhost:3306/test?user=root;password=");
                                                              ^
HelloWorldExample.java:26: unreported exception java.sql.SQLException; must be c
aught or declared to be thrown
        Statement DB_STATEMENT = DB_CONNECTION.createStatement();
                                                              ^
HelloWorldExample.java:29: unreported exception java.sql.SQLException; must be c
aught or declared to be thrown
        ResultSet RS = DB_STATEMENT.executeQuery("SELECT * FROM test ");
                                                ^
HelloWorldExample.java:32: unreported exception java.sql.SQLException; must be c
aught or declared to be thrown
        while (RS.next()) {
                      ^
HelloWorldExample.java:33: unreported exception java.sql.SQLException; must be c
aught or declared to be thrown
                x=RS.getString("test_name");
                              ^
7 errors
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 9 2007
Added on May 12 2007
1 comment
239 views