₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,327,288 members, 8,430,211 topics. Date: Saturday, 20 June 2026 at 06:00 AM

Toggle theme

Java With Mysql - Programming - Nairaland

Nairaland ForumScience/TechnologyProgrammingJava 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"wink;

Connection conn = getConnection();


PreparedStatement ps = conn.prepareStatement(" Select user_id userId from USERS where username = ?"wink;
ps.setString(1,username);


ResultSet rs = ps.executeQuery();
int userId = 0;
if(rs.next())
userId = rs.getInt("userId"wink;

Query q = em.createQuery("Select a from Roleuser a where a.userId=:userId"wink;
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"wink.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"wink;
}
} 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

Generating Crystal Report With MYSQL Database For A VB.NET ApplicationGenrating Crystal Report With Mysql Database In A Vb.net ApplicationHow To Connet C++ Mfc Application With Mysql234

Starting An Ecommerce Website, Which CMS Will You RecommendPlease Gurus Help A Java Beginner.Win N500,000 Naira On This Competition Challenge!!!