Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,158,432 members, 7,836,729 topics. Date: Wednesday, 22 May 2024 at 12:01 PM |
Nairaland Forum / Science/Technology / Programming / Java With Mysql (999 Views)
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)
(1) (Reply)
Java With Mysql by touthtek(m): 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> } |
(1) (Reply)
Programming in Theory verses Programming in Practice / Help Me Upload My Ftp Files In Binary Mode Abeg. / .
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 8 |