Java With Mysql - Programming - Nairaland
Nairaland Forum › Science/Technology › Programming › Java With Mysql (1089 Views)
1 Reply
| Java With Mysql by touthtek(op): 8:13pm On Oct 19, 2012 |
Please I a mysql code with java that will able to delete from a table in a database |
| Re: Java With Mysql by naijaswag1: 9:27pm On Oct 19, 2012 |
http://docs.oracle.com/javase/tutorial/jdbc/index.html http://www.vogella.com/articles/MySQLJava/article.html http://www.tutorialspoint.com/jdbc/jdbc-sample-code.htm A sample servlet doing mysql/jdbc.Am currently using it. public class AdvanceBorder extends HttpServlet { private static Connection connection; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String username = request.getUserPrincipal().getName(); Class.forName("com.mysql.jdbc.Driver" ;Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(" Select user_id userId from USERS where username = ?" ;ps.setString(1,username); ResultSet rs = ps.executeQuery(); int userId = 0; if(rs.next()) userId = rs.getInt("userId" ; Query q = em.createQuery("Select a from Roleuser a where a.userId=:userId" ;q.setParameter("userId", userId); List rowz = (List)q.getResultList(); Iterator itil = rowz.iterator(); ArrayList<String> arr = new ArrayList<String>(); HttpSession session = request.getSession(); while(itil.hasNext()) arr.add(((Roleuser)itil.next()).getRolename()); session.setAttribute("arr", arr); request.getRequestDispatcher("home.jsp" .forward(request, response);;} catch (SQLException ex) { Logger.getLogger(AdvanceBorder.class.getName()).log(Level.SEVERE, null, ex); } catch (ClassNotFoundException ex) { Logger.getLogger(AdvanceBorder.class.getName()).log(Level.SEVERE, null, ex); } } private static Connection getConnection(){ try { if(connection == null || connection.isClosed() ) { connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/testengine", "root", "gugusomto" ;} } catch (SQLException ex) { Logger.getLogger(AdvanceBorder.class.getName()).log(Level.SEVERE, null, ex); } return connection; } private static void closeConnection(){ try { if(connection !=null && !connection.isClosed()) { connection.close(); } } catch (SQLException ex) { Logger.getLogger(AdvanceBorder.class.getName()).log(Level.SEVERE, null, ex); } } scription"; }// </editor-fold> } |
Generating Crystal Report With MYSQL Database For A VB.NET Application • Genrating Crystal Report With Mysql Database In A Vb.net Application • How To Connet C++ Mfc Application With Mysql • 2 • 3 • 4
Starting An Ecommerce Website, Which CMS Will You Recommend • Please Gurus Help A Java Beginner. • Win N500,000 Naira On This Competition Challenge!!!
;