RamChithra
RamChithra

Reputation: 57

MySql database access JDBC driver

It always return error page.

class file to access database in Struts2. my database username:root and password:mysql database name is "code" and table name is "user".

 public String execute()  
 {
      String ret = ERROR;
      Connection conn = null;

      try {
           String URL = "jdbc:mysql://localhost/code";
           Class.forName("com.mysql.jdbc.Driver");
           conn = DriverManager.getConnection(URL, "root", "mysql");
           String sql = "SELECT username FROM user WHERE";
           sql+=" username = ? AND password = ?";
           PreparedStatement ps = conn.prepareStatement(sql);
           ps.setString(1, username);
           ps.setString(2, password);
           ResultSet rs = ps.executeQuery();

           while (rs.next()) {
               name = rs.getString(1);
               ret = SUCCESS;
           }
     } catch (Exception e) {
           ret = ERROR;
     } 
     finally {
           if (conn != null) {
               try {
                   conn.close();
               }
               catch (Exception e){}
            }
     }
     return ret;
 }

this is my struts.xml

<struts>
    <constant name="struts.devMode" value="true" />
    <package name="com.code" extends="struts-default">
        <action name="loginaction" class="com.code.Login"  method="execute">
           <result name="success">/Login.jsp</result>
           <result name="error">/error.jsp</result>
        </action>   
    </package>
</struts>

Why this is not return success page?

Upvotes: 1

Views: 52

Answers (1)

Vivek Singh
Vivek Singh

Reputation: 2073

The port number for the database is missing in your database url.

If you are using MySql, default port no is 3306. Try to change the url as below:

String URL = "jdbc:mysql://localhost:3306/code";

Upvotes: 1

Related Questions