Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --/ index.html /--
- <!doctype html>
- <html>
- <head>
- <title>Accesso</title>
- </head>
- <body>
- <h1>Donazione</h1>
- <form action="ciao.jsp" method="post">
- Nome<input type="text" name="nome" required><br>
- Cognome<input type="text" name="cognome" required><br>
- Donazione<input type="text" name="donazione" required><br>
- <input type="submit">
- <br>
- </form>
- </body>
- </html>
- --/ ciao.jsp /--
- <html>
- <head>
- <title>JSP e Database mySQL</title>
- </head>
- <body>
- <!--Import API JDBC-->
- <%@ page import="java.sql.*" %>
- <table border="1">
- <%
- // Caricamento driver per connessione al database mySQL
- try{
- Class.forName("com.mysql.jdbc.Driver");
- }
- catch(Exception e)
- {
- out.println("Errore: " + e);
- }
- // Apertura connessione verso il database
- Connection connessione = null;
- String databaseURL = "jdbc:mysql://localhost:3306/esercitazione";
- try{
- connessione = DriverManager.getConnection(databaseURL, "root", "");
- // connessione = DriverManager.getConnection("jdbc:mysql://localhost:3306/esercitazione?user=root&password=");
- }
- catch(Exception e){
- out.println("Errore: " + e);
- }
- // Ottenimento dello statement per interagire con il database
- try{
- // Creazione dello statement
- Statement statement = connessione.createStatement();
- String query = "SELECT * FROM persone";
- // Creazione del result set
- ResultSet rs = statement.executeQuery(query);
- if(rs != null)
- {
- ResultSetMetaData rsmd = null;
- rsmd = rs.getMetaData();
- out.println("<pre>");
- int numeroColonne = rsmd.getColumnCount();
- out.println("Sono presenti " + numeroColonne + " colonne nel database" + "\t");
- for (int i = 1; i <= numeroColonne; i++)
- {
- out.println(rsmd.getColumnName(i) + "\t tipo " + rsmd.getColumnTypeName(i));
- }
- out.println("<br><br>");
- for (int i = 1; i <= numeroColonne; i++)
- {
- out.print("<b>"+rsmd.getColumnName(i)+"</b>" + "\t");
- }
- while (rs.next())
- {
- out.println();
- for (int i = 1; i <= numeroColonne; i++)
- {
- out.print(rs.getString(i) + "\t");
- }
- }
- out.println("</pre>");
- }
- }
- catch(Exception e){
- out.println("Errore: " + e);
- }
- // Chiusura della connessione
- try{
- connessione.close();
- }
- catch (Exception e)
- {
- out.println("Errore: " + e);
- }
- out.close();
- %>
- </table>
- </body>
- </html>
- // RICERCA
- <%@ page import="java.sql.*" %>
- <%
- if(request.getParameter("btn")!=null)
- {
- // carico il driver per la connessione al DB MySQL
- String DRIVER = "com.mysql.jdbc.Driver";
- try{
- Class.forName(DRIVER);
- }
- catch (ClassNotFoundException e) {
- //System.err.println("Driver non trovato" + e);
- out.println("ERRORONE");
- }
- // riferimento al database: connessione Mysql
- String URL_mioDB = "jdbc:mysql://localhost:3306/db_cali"; //3306 di default
- // definizione della query
- String w = request.getParameter("w");
- String query="SELECT * FROM users WHERE user LIKE '%"+w+"%' OR nome LIKE '%"+w+"%' OR cognome LIKE '%"+w+"%';";
- Connection conn=null;
- try
- {
- conn=DriverManager.getConnection(URL_mioDB,"root","");
- }
- catch(Exception e){
- out.println(e);
- }
- try{
- Statement st=conn.createStatement();
- ResultSet rs=st.executeQuery(query);
- while(rs.next())
- {
- out.println(rs.getString(2)+ " "+rs.getString(2));
- }
- }
- catch(Exception e)
- {
- out.println(e);
- }
- /*finally
- {
- conn.close();
- }*/
- }
- else
- {
- %>
- <form action="">
- <input type="text" name="w">
- <input type="submit" name="btn" value="Invia">
- </form>
- <%
- }
- %>
- // GESTIONE
- <%@ page import="java.sql.*" %>
- <%
- if(request.getParameter("btn")!=null)
- {
- // carico il driver per la connessione al DB MySQL
- String DRIVER = "com.mysql.jdbc.Driver";
- try{
- Class.forName(DRIVER);
- }
- catch (ClassNotFoundException e) {
- //System.err.println("Driver non trovato" + e);
- out.println("ERRORONE");
- }
- // riferimento al database: connessione Mysql
- String URL_mioDB = "jdbc:mysql://localhost:3306/db_cali"; //3306 di default
- // definizione della query
- String query = request.getParameter("query");
- Connection conn=null;
- try
- {
- conn=DriverManager.getConnection(URL_mioDB,"root","");
- }
- catch(Exception e){
- out.println(e);
- }
- try{
- Statement st=conn.createStatement();
- int ris=st.executeUpdate(query);
- out.println(ris);
- }
- catch(Exception e)
- {
- out.println(e);
- }
- finally
- {
- conn.close();
- }
- }
- else
- {
- %>
- <form action="">
- <input type="text" name="query">
- <input type="submit" name="btn" value="Invia">
- </form>
- <%
- }
- %>
- // INTERROGA
- <%@ page import="java.sql.*" %>
- <%
- if(request.getParameter("btn")!=null)
- {
- // carico il driver per la connessione al DB MySQL
- String DRIVER = "com.mysql.jdbc.Driver";
- try{
- Class.forName(DRIVER);
- }
- catch (ClassNotFoundException e) {
- //System.err.println("Driver non trovato" + e);
- out.println("ERRORONE");
- }
- // riferimento al database: connessione Mysql
- String URL_mioDB = "jdbc:mysql://localhost:3306/db_cali"; //3306 di default
- // definizione della query
- String query = request.getParameter("query");
- Connection conn=null;
- try
- {
- conn=DriverManager.getConnection(URL_mioDB,"root","");
- }
- catch(Exception e){
- out.println(e);
- }
- try{
- Statement st=conn.createStatement();
- ResultSet rs=st.executeQuery(query);
- while(rs.next())
- {
- out.println(rs.getString(1)+ " "+rs.getString(2));
- }
- }
- catch(Exception e)
- {
- out.println(e);
- }
- finally
- {
- conn.close();
- }
- }
- else
- {
- %>
- <form action="">
- <input type="text" name="query">
- <input type="submit" name="btn" value="Invia">
- </form>
- <%
- }
- %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement