• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Struts - MYSQL connection not working  RSS feed

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Some how managed to bring up my form. Now I have problem in sending data from the GUI. Below is the connection code. Can some one place check and tell what may be the problem.

package com.admin;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;


public class Admin {
    public static Connection conn() throws Exception {
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");
    }

    public static int Register(RegisterAction jjform) {
        int flag = 0;
        try {
            PreparedStatement ps = conn().prepareStatement("INSERT INTO jjform VALUES(?,?,?,?)");
            ps.setInt(1, jjform.getIdjjform());
            ps.setString(2, jjform.getFname());
            ps.setString(3, jjform.getLname());
            ps.setString(4, jjform.getCname());
            flag = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

}


I dont get any http or any error. When I input the data in the GUI I get my error.jsp page displayed.
 
Ranch Hand
Posts: 71
3
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, allthough it's hard to read w/o code tags and bad translation, there seems nothing wrong with your code. Also: please explain what you expect to happen and what actualy happen instead.

A few notes:
Class.forName is no longer needed since jdbc4 - as long as the driver is in classpath it gets loaded by serviceloader when calling drivermanager

also - you connect as root w/o password - wich is only possible in a fresh clean uninitialized install of mysql/mariadb - but if this would be your problem you would get an exception on the line you connect - so this seems also not a problem

add debug lines to check if the posted code gets called as you think it is - most likely you passing in an invalid object - in the off-chance the code is run at all
 
Vani Jay
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suddenly when all is well, I am getting an error with this Struts-tags even though I have imported the Struts lib to Eclipse under Web-INF/lib. When I Run in Eclipse, it gives error like below:

HTTP Status 500 – Internal Server Error
Type Exception Report

Message Unable to find taglib [s] for URI: [/struts-tags]

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

org.apache.jasper.JasperException: Unable to find taglib [s] for URI: [/struts-tags]
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:55)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:293)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:80)
org.apache.jasper.compiler.TagLibraryInfoImpl.<init>(TagLibraryInfoImpl.java:176)
org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:434)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:492)
org.apache.jasper.compiler.Parser.parseElements(Parser.java:1448)
org.apache.jasper.compiler.Parser.parse(Parser.java:145)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:244)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:105)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:203)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:374)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:351)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:335)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:595)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:368)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Note The full stack trace of the root cause is available in the server logs.

Apache Tomcat/8.5.30

--------------------------------------------------------------

My Code:

<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head><title>Participant Contact Form</title></head>
<body style="background-color: lightyellow;">
<div style="margin-left: 25%; margin-top: 50px;">
<h1>Participant Info</h1>
<s:form action="Register.action"/>
<s:lable name="Participant ID" value="idjjform" />
  <s:lable name="Participant ID" value="idjjform" />
<s:textfield label="fname" name="fname" />
<s:textfield label="Lname" name="lname" />
<s:textfield label="Cname" name="cname" />

<s:submit value="Register" />
</s:form>
</div>
</body>
</html>
 
Rancher
Posts: 506
15
Java Notepad
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not familiar with Struts, but the error encountered may be a configuration issue (this might be within the JSP or WEB.XML). I found these while I searched the net:

- Tag-lib issue with migrating Struts2 to Tomcat 8.Its working on tomcat7: https://stackoverflow.com/questions/41744815/tag-lib-issue-with-migrating-struts2-to-tomcat-8-its-working-on-tomcat7
- Struts 2 Annotation Tutorial 1: https://dzone.com/tutorials/java/struts-2/struts-2-example/struts-2-annotation-example-1.html

Also, the link to how to UseCodeTags on JavaRanch posts.
 
Prasad Saya
Rancher
Posts: 506
15
Java Notepad
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When working with web apps, a database connection can be acquired using a Data Source (rather than a DriverManager). According to Oracle's Java documentation its a preferred way of to get connection objects.

Connecting with DataSource Objects (Oracle's Java Tutorials): https://docs.oracle.com/javase/tutorial/jdbc/basics/sqldatasources.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!