java面试回忆录

时间:2014-06-20 10:05:56   收藏:0   阅读:347

本文地址:http://blog.csdn.net/sushengmiyan/article/details/28479895

作者:sushengmiyan

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

1.报文格式定义如下:
20字符长的姓名+1字符长的性别+3字符长的年龄
姓名长度不足20的右边补空格 性别中0表示男,1表示女
年龄不足3字符的左边补0
如:
denny     0026
这一段报文解析后表示姓名为denny性别为男,年龄为26


数据库表结构如下:
create table test
(
  name varchar(20) primary key,
  sex varchar(1),
  age varchar(3)
);
java
一,写一个方法
public String makePackage(String name, int sex, int age);
由输入的姓名,性别,年龄返回对应的报文


二、写一个方法
public void parserPackage(String package);
由输入的报文,解析后写入数据库表test中.
sql
从test中取出年龄大于16岁的前10条记录
从test中取出给定name的记录的sex,age以及按年龄排的名次


javascript
页面如下
<html>
  <body>
    <input id="package" type = "text"/>
    <input type="button" onclick="parse()"/>
    <input id="name" type="text"/>
    <input id="sex" type="text"/>
    <input id="age" type="text"/>
  </body>
</html>


用javascript 实现方法parser() 当点击按钮的时候,按照报文格式解析id为package的文本域的内容,并将解析完的结果写入到相应的id的文本域里。


css
用css给上面的button加上背景图片


web
请求页面如下:
<html>
  <body>
    <form action = "testServer">
      <input name = "package"type="text"/>
      <input type="submit"/>
    </form>
  </body>
</html>
写一个servlet调用parsePackage方法将请求页面表单域中的package的值解析并写入数据库,将此servlet配置到tomcat中

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




java答案如下:</p><pre code_snippet_id="376946" snippet_file_name="blog_20140604_2_2610834" name="code" class="javascript">import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class YuchengFacetoFace 
{
	//报文格式定义如下:
    //	20字符长的姓名+1字符长的性别+3字符长的年龄
    //	姓名长度不足20的右边补空格 性别中0表示男,1表示女
    //	年龄不足3字符的左边补0
    //	如:
    //	denny     0026
    //	这一段报文解析后表示姓名为denny性别为男,年龄为26
	public String makePackage(String name, int sex, int age)
	{
		//输入的合法性校验未做
		String Result = "";
		int length = name.length();
		if (length < 20)
		{
			int cutLength = 20 - length;
			for (int i = 0; i < cutLength; i++) 
			{
				Result += " ";
			}
			Result += name;
		}
		Result += sex;
		//099的情况呢?
		if (age < 10) 
		{
		  Result = Result + 0 + 0 + age;
		}
		else
		if (age < 100) 
		{
			Result = Result + 0 + age;
		}
		else
		{
			Result += age;
		}
		return Result;
	}

	//由输入的报文,解析后写入数据库表test中.
	public void parserPackage(String Package)
	{
		String name = Package.substring(0, 19);;
		int sex = Integer.parseInt(Package.substring(20, 20));
		int age = Integer.parseInt(Package.substring(21, 23));
		/*
		 * insert into test values(name, sex, age);
		 * select top 10 * from test where age < 16
		 * select * from test where name = name odered by age
		 */
	}
	//连接MYSQL数据库并创建数据库和表
	public static void CreateDataBase() throws SQLException, ClassNotFoundException
	{
	    String driver = "com.mysql.jdbc.Driver";
	    Class.forName(driver);//加载mysql数据库,用Class.forName("驱动名称")进行加载
		//第二步,创建数据库连接,将数据库与当前文件连接起来,后面才可以对数据库进行操作
		//格式:
		String url = "jdbc:mysql://localhost:3306/Yuchengtech";//建立数据库连接地址
		Connection conn =  DriverManager.getConnection(url, "root", "admin");//连接数据库	
		Statement s = conn.createStatement();
		String SQL = "insert into test(name, sex,age) values(" +'"'+ "susheng" + '"' +  ", 1, 25);";
		s.execute(SQL);
	}
	public static void main(String[] args) throws SQLException, ClassNotFoundException 
	{
//		YuchengFacetoFace yff = new YuchengFacetoFace();
//		System.out.println(yff.makePackage("susheng", 0, 12));
		CreateDataBase();
	}

}

二、js的代码答案

<html>
  <head>
    <script language="javascript">
      function parse()
      {
         var content = document.getElementById("package").value;
         var name = content.substring(1,20).trim();
         var sex = content.substring(20,21);
         var age = content.substring(21,24);  
         document.getElementById("name").value = name;
         document.getElementById("sex").value = sex;
         document.getElementById("age").value = age;     
      }
    </script>
  </head>
  <body>
    <input id="package" type = "text"/>
    <input type="button" onclick="parse()"/>
    <input id="name" type="text"/>
    <input id="sex" type="text"/>
    <input id="age" type="text"/>
  </body>
</html>

css很简单 有个background-image:url(a.jpg);

web的答案

package com.yuchengtech;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestServer extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String Src = request.getParameter("package");
		YuchengFacetoFace yff = new YuchengFacetoFace();
		yff.parserPackage(Src);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String Src = request.getParameter("package");
		YuchengFacetoFace yff = new YuchengFacetoFace();
		yff.parserPackage(Src);
	}

}


java面试回忆录,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!