Skip to content

Instantly share code, notes, and snippets.

@IanRamosC
Created March 27, 2015 13:50
Show Gist options
  • Save IanRamosC/847e24fa9225e18a97ef to your computer and use it in GitHub Desktop.
Save IanRamosC/847e24fa9225e18a97ef to your computer and use it in GitHub Desktop.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Alunos extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
String url = "jdbc: mysql:alunos";
Connection con;
Statement stmt;
String query = "select * from alunos";
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
}catch(PException e) {
out.println("<p>ClassNotFoundException: ");
out.println("<p>"+e.getMessage());
}
try{
con = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root&password=");
stmt = con.createStatement();
String nome = req.getParameter("nome");
String cod = req.getParameter("codigo");
stmt.executeUpdate("insert into alunos " + "values("+ cod +", "+ nome + ")");
ResultSet rs = stmt.executeQuery(query);
out.println("<p>Código e Alunos Cadastrados");
while(rs.next()) {
String s = rs.getString("nome");
int n = rs.getInt("codigo");
out.println("<p>"+ s +" "+ n);
}
stmt.close();
con.close();
}catch(SQLException ex){
out.println("<p>SQLException: "+ ex.getMessage());
}
out.println("<html>");
out.println("<title Obrigado!</title>");
out.println("Obrigado por participar");
out.println("<html>");
out.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment