Tuesday, April 28, 2015

How to write a hindi in text field and store it into database.







Step 1- Create text field which should be support the hindi. For this I’m using “keyboard.js” of hinkhoj. Include this js in your page and create input field.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<html>
    <head>
        <script src="http://www.hinkhoj.com/common/js/keyboard.js"></script>
        <link rel="stylesheet" type="text/css" href="http://www.hinkhoj.com/common/css/keyboard.css" />
    </head>
    <body>
        <form action="HINDI.jsp" method="POST"/>
        <b>कृपया अपना नाम प्रवेश करे</b>:<br>
        <script language="javascript">
            CreateHindiTextBox("hindiText");
        </script>
        <input type="submit" value="submit"/>
    </form>
</html>

Step 2- create a table in database (I’m using MySql as database). 

1
2
CREATE TABLE `hindi` (`data` varchar(200) COLLATE utf8_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Note: CHARSET=utf8 COLLATE=utf8_unicode_ci;
This is for hindi support.

Step 3- Create jsp (hindi.jsp). In this jsp create a form with one hindi text field and submit button. Send request to same jsp and get the entered name in hindi text field as request parameter. And store it into to db and later get it and display into page.

Example:
hindi.jsp
1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <script src="http://www.hinkhoj.com/common/js/keyboard.js"></script>
        <link rel="stylesheet" type="text/css" href="http://www.hinkhoj.com/common/css/keyboard.css" />
    </head>
    <body>
        <form action="HINDI.jsp" method="POST"/>
        <b>कृपया अपना नाम प्रवेश करे</b>:<br>
        <script language="javascript">
            CreateHindiTextBox("hindiText");
        </script>
        <input type="submit" value="submit"/>
    </form>
</html>
<%
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    
    if (request.getParameter("hindiText") != null) {
        out.println("<b>आपने लिखा  :</b>" + request.getParameter("hindiText"));
        out.println("</br><b>आप लिख चुके है : </b></br>");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8", "root", "root");
        PreparedStatement ps=con.prepareStatement("insert into hindi values(?)");
        ps.setString(1,request.getParameter("hindiText"));
        ps.executeUpdate();

        PreparedStatement ps1=con.prepareStatement("select * from hindi");
        ResultSet rs=ps1.executeQuery();
        while(rs.next()){
        out.println(rs.getString(1)+", ");
        }
        
    }
%>

No comments :

Post a Comment