40 likes | 133 Views
Filiale pea09. Die Einbindung der MySQL-Datenbank in das Servlet. Filiale pea09. Das Servlet. import java.io.IOException; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest;
E N D
Filiale pea09 Die Einbindung der MySQL-Datenbank in das Servlet
Filiale pea09 Das Servlet import java.io.IOException; import java.sql.*; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class StorageDB extends HttpServlet { protected void doGet(HttpServletRequest request ,HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); java.io.PrintWriter out = response.getWriter(); [...] out.close(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
Filiale pea09 Die Einbindung der MySQL-Datenbank in das Servlet protected void doGet(HttpServletRequest request ,HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=UTF-8"); java.io.PrintWriter out = response.getWriter(); try{ Class.forName("org.gjt.mm.mysql.Driver").newInstance(); Connection con = java.sql.DriverManager.getConnection( "jdbc:mysql://salvator.ibr.cs.tu-bs.de/pea09","pea09","jiiab12"); Statement s = con.createStatement(); ResultSet rs=s.executeQuery("select * from storage"); [...] while (rs.next()) { out.println(rs.getString("name")); } [...] } catch (SQLException ex) { out.println("Error1: "+ex); } catch (ClassNotFoundException ex) { out.println("Error2: "+ex); } catch (InstantiationException ex) { out.println("Error3: "+ex); } catch (IllegalAccessException ex) { out.println("Error4: "+ex); } out.close(); }
Filiale pea09 Die Darstellung des Servlets