简体   繁体   中英

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?

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM