Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: combobox unicode problem

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default combobox unicode problem

    hi,
    I am a beginner in Java, JSP, and Ajax and I'm trying to create a simple form with a combobox which reads names of minerals from a database and then prints their ID.
    My problem is, if I get name of mineral with special letters (š,ž,č), it get me error. My database is Unicode (UTF-8). I think there is problem with AJAX, bud i dont know where.
    Can anybody help me with this?

    table:
    NERASTID            NERASTNAZOV
    ----------------------------------------
          2                       ankerit
          3                       magnetit
          4                       markazit
          9                       nežiaruvzdorné íly
         110                      železné rudy
    .
    .
    .
    .etc..

    combo.jsp:
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%@page import="java.sql.*"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=7" />
    <title>podpora</title>
     
    <script type="text/javascript">
     
    	function showEmp(nerast_value)
    		{ 
    		if(document.getElementById("nerastid").value!="-1")
    		{
     			xmlHttp=GetXmlHttpObject();
     
    			var url="getuser.jsp";
    			url=url+"?nerastnazov="+nerast_value;
     
     
     
    			xmlHttp.onreadystatechange=stateChanged;
    			xmlHttp.open("GET",url,true);
    			xmlHttp.send(null);
     
    		}
    		else
    		{
    			 alert("Please Select mineral Id");
    		}
     
    	}
     
    	function stateChanged() 
    		{ 
     
    		document.getElementById("nerastid").value ="";
    			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     			{ 
     
     			 var showdata = xmlHttp.responseText; 
       			 var strar = showdata.split(":");
     
    			 if(strar.length==1)
    	 		{
    		  		document.getElementById("nerastid").focus();
    		  		alert("Please Select mineral Id");
    		  		document.getElementById("nerastnazov").value =" ";
    		  		document.getElementById("nerastid").value =" ";
     
    	 		}
    	 		else if(strar.length>1)
    	 			{
    				var strname = strar[1];
    				document.getElementById("nerastid").value= strar[1];
     
    				 }
     
     			} 
    		}
     
    	function GetXmlHttpObject() 
    	{
    		var xmlHttp=null;
    		try
     			{
     			// Firefox, Opera 8.0+, Safari
     			xmlHttp=new XMLHttpRequest();
     			}
    		catch (e)
     			{
     			//Internet Explorer
     			try
      			{
      				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      			}
    	   catch (e)
      		{
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		}
     	}
    	return xmlHttp;
    	}
     
     
    </script>
     
    </head>
    <body>
     
    <form name="nerasty"><br>
    <br>
    <table border="0" width="400px" align="center" bgcolor="#ffffff">
    	<div id="mydiv"></div>
    	<tr>
    		<td><b>Druh nerastu</b></td>
    		<td><select name="nerasty" onchange="showEmp(this.value);">			
    			<option value="-1">----------Select-----------</option>
    			<%
     
     
    				Connection conn = null;
     
    				int sumcount = 0;
    				Statement st;
    				try {
    					Class.forName("oracle.jdbc.driver.OracleDriver");
    					conn = DriverManager.getConnection(
    							"jdbc:oracle:thin:@w08:1521:ORCL", "user","pass");
    					String query = "select * from V_TBL_NERASTY";
     
    					st = conn.createStatement();
    					ResultSet rs = st.executeQuery(query);
    			%>
     
    			<%
    				while (rs.next()) {
    			%>
     
    			 <option value="<%=rs.getString(2)%>"><%=rs.getString(2)%></option> 
     
    			<%
    				}
    			%>
     
    			<%
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			%>
    		</select></td>
     
    	</tr>
    		<td><b>Kod:</b></td>
    		<td> <input type="text" id="nerastid" name="nerastid" value=""></td>
    	</tr>
     
    </table>
    </form>
    <table border="0" width="100%" align="center">
    	<br>
    	<br>
    </table>
    </body>
    </html>

    getuser.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*"%>
    <%
     
     
    	String nerastnazov = request.getParameter("nerastnazov").toString();
     
     
    	String data = "";
     
    	Connection conn = null;
     
    	int sumcount = 0;
    	Statement st;
    	try {
    		Class.forName("oracle.jdbc.driver.OracleDriver");
    		conn = DriverManager.getConnection(
    				"jdbc:oracle:thin:@w08:1521:ORCL", "user","pass");
    		String query = "select * from V_TBL_NERASTY where nerastnazov = '"+nerastnazov+"'";
     
     
    		st = conn.createStatement();
    		ResultSet rs = st.executeQuery(query);
     
     
    		while (rs.next()) {
    			data = ":" + " " + rs.getString(1) + ":";
     
    		}
     
    		out.println(data);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    %>


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: combobox unicode problem

    My problem is, if I get name of mineral with special letters (š,ž,č), it get me error
    Please post the error in its entirety...

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: combobox unicode problem

    it get me error " 'document.getElementById(...)' is null or not an object" from line 49.
    Line 49: document.getElementById("nerastnazov").value =" ";
    (This line is in the function stateChanged() after if(strar.length==1).)
    Could you help me with this?

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: combobox unicode problem

    ok. i figured it out. There was problem with sending String.. Now i send ID instead of String..

    combo.jsp
    ...
    function showEmp(nerast_value)
          {
          if(document.getElementById("nerastid").value!="-1")
          {
              xmlHttp=GetXmlHttpObject();
     
             var url="getuser.jsp";
             url=url+"?nerastid="+nerast_value;
     
     
     
             xmlHttp.onreadystatechange=stateChanged;
             xmlHttp.open("GET",url,true);
             xmlHttp.send(null);
    ...
    <option value="<%=rs.getString(1)%>"><%=rs.getString(2)%></option> 
    ...

    getuser.jsp

    ...
    String nerastid = request.getParameter("nerastid").toString(); 
    ...
    String query = "select * from V_TBL_NERASTY where nerastid = '"+nerastid+"'";

Similar Threads

  1. java and unicode ...
    By abesha in forum Java Theory & Questions
    Replies: 2
    Last Post: November 15th, 2010, 11:25 AM
  2. print unicode charecter
    By zulqar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 31st, 2010, 01:54 PM
  3. do actions in ComboBox
    By java_cs in forum AWT / Java Swing
    Replies: 2
    Last Post: October 1st, 2009, 10:53 AM
  4. Use of Unicode 5.1 in MySQL DB with Java
    By Desert Fox in forum JDBC & Databases
    Replies: 2
    Last Post: November 12th, 2008, 08:29 AM
  5. Java program to display 2D Array in ComboBox
    By crazydeo in forum AWT / Java Swing
    Replies: 1
    Last Post: May 29th, 2008, 09:13 AM