- 相关推荐
java基础面试题(2012完整版)
141、JAVA 多线程编程。 用 JAVA 写一个多线程程序,如写四个线程,二个加 1,二个对一
个变量减一,输出。
希望大家补上,谢谢
142、可能会让你写一段 Jdbc连Oracle 的程序,并实现数据查询.
答:程序如下:
package hello.ant;
import java.sql.*;
public class jdbc
{
String dbUrl="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
String theUser="admin";
String thepw="manager";
Connection c=null;
Statement conn;
ResultSet rs=null;
public jdbc()
{
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
c = DriverManager.getConnection(dbUrl,theUser,thepw); conn=c.createStatement();
}catch(Exception e){
e.printStackTrace();
}
}
public boolean executeUpdate(String sql)
{
---------- 16 ----------
*** JAVA面试题.txt ***
try
{
conn.executeUpdate(sql);
return true;
}
catch (SQLException e)
{
e.printStackTrace();
return false;
}
}
public ResultSet executeQuery(String sql)
{
rs=null;
try
{
rs=conn.executeQuery(sql);
}
catch (SQLException e)
{
e.printStackTrace();
}
return rs;
}
public void close()
{
try
{ conn.close();
c.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
ResultSet rs;
jdbc conn = new jdbc();
rs=conn.executeQuery("select * from test");
try{
while (rs.next())
{
System.out.println(rs.getString("id"));
System.out.println(rs.getString("name"));
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
143、ORACLE 大数据量下的分页解决方法。一般用截取 ID方法,还有是三层嵌套方法。
答:一种分页方法
<%< b="">
int i=1;
int numpages=14;
String pages = request.getparameter("page") ;
int currentpage = 1;
currentpage=(pages==null)?(1):{Integer.parseInt(pages)}
sql = "select count(*) from tables";
ResultSet rs = DBLink.executeQuery(sql) ;
while(rs.next()) i = rs.getInt(1) ;
int intpageCount=1;
intpageCount=(i%numpages==0)?(i/numpages):(i/numpages+1);
int nextpage ;
int uppage;
nextpage = currentpage+1; if (nextpage>=intpageCount) nextpage=intpageCount;
uppage = currentpage-1;
if (uppage<=1) uppage=1;
---------- 17 ----------
*** JAVA面试题.txt ***
rs.close();
sql="select * from tables";
rs=DBLink.executeQuery(sql);
i=0;
while((i
%>
//输出内容
//输出翻页连接
合计:<%=currentpage%>/<%=intpagecount%>
上一页
<%< b="">
for(int j=1;j<=intpagecount;j++){< b="">
if(currentpage!=j){
%>
<%< b="">
}else{
out.println(j);
}
}
%>
144、用 jdom解析xml 文件时如何解决中文问题?如何解析?
答:看如下代码,用编码方式加以解决
package test;
import java.io.*;
public class DOMTest
{
private String inFile = "c:\\people.xml";
private String outFile = "c:\\people.xml";
public static void main(String args[])
{
new DOMTest();
}
public DOMTest()
{
try
{
javax.xml.parsers.DocumentBuilder builder =
javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document doc = builder.newDocument();
org.w3c.dom.Element root = doc.createElement_x("老师");
org.w3c.dom.Element wang = doc.createElement_x("王");
org.w3c.dom.Element liu = doc.createElement_x("刘");
wang.appendChild(doc.createTextNode("我是王老师"));
root.appendChild(wang);
doc.appendChild(root);
javax.xml.transform.Transformer transformer =
javax.xml.transform.TransformerFactory.newInstance().newTransformer();
transformer.setOutputproperty(javax.xml.transform.OutputKeys.ENCODING, "gb2312");
transformer.setOutputproperty(javax.xml.transform.OutputKeys.INDENT, "yes");
transformer.transform(new javax.xml.transform.dom.DOMSource(doc),
new
javax.xml.transform.stream.StreamResult(outFile));
---------- 18 ----------
*** JAVA面试题.txt ***
}
catch (Exception e)
{
System.out.println (e.getMessage());
}
}
}
145、编程用JAVA 解析 XML 的方式.
答:用 SAX方式解析XML,XML文件如下:
王小明
信息学院
6258113
男,1955 年生,博士,95 年调入海南大学
事件回调类SAXHandler.java
import java.io.*;
import java.util.Hashtable;
import org.xml.sax.*;
public class SAXHandler extends HandlerBase
{
private Hashtable table = new Hashtable(); private String currentElement = null;
private String currentValue = null;
public void setTable(Hashtable table)
{
this.table = table;
}
public Hashtable getTable()
{
return table;
}
public void startElement(String tag, AttributeList attrs)
throws SAXException
{
currentElement = tag;
}
public void characters(char[] ch, int start, int length)
throws SAXException
{
currentValue = new String(ch, start, length);
}
public void endElement(String name) throws SAXException
{
if (currentElement.equals(name))
table.put(currentElement, currentValue);
}
} JSp内容显示源码,SaxXml.jsp:
剖析XML文件people.xml
<%@ page errorpage="Errpage.jsp"
contentType="text/html;charset=GB2312" %>
<%@ page="" import="java.io.*">
<%@ page="" import="java.util.Hashtable">
<%@ page="" import="org.w3c.dom.*">
<%@ page="" import="org.xml.sax.*">
<%@ page="" import="javax.xml.parsers.SAXparserFactory">
<%@ page="" import="javax.xml.parsers.SAXparser">
<%@ page="" import="SAXHandler">
<%< b="">
File file = new File("c:\\people.xml");
FileReader reader = new FileReader(file);
parser parser;
SAXparserFactory spf = SAXparserFactory.newInstance();
SAXparser sp = spf.newSAXparser();
SAXHandler handler = new SAXHandler();
sp.parse(new InputSource(reader), handler);
---------- 19 ----------
*** JAVA面试题.txt ***
Hashtable hashTable = handler.getTable();
out.println("
教师信息表");
out.println("
姓名" + "
" + |
(String)hashTable.get(new String("name")) + "");
out.println("
学院" + "
" + |
(String)hashTable.get(new String("college"))+"");
out.println("
电话" + "
" + |
(String)hashTable.get(new String("telephone&
【java基础面试题完整版】相关文章:
java 基础面试题07-11
Java面试题07-12
JAVA题库:JAVA面试题目整理07-11
java面试题五07-11
华为Java面试题07-11
华为的JAVA面试题07-11
求java面试题07-11
华为公司 java人员面试题07-11
华为全套面试题(基础版)07-11
一道Java面试题,题目如下07-11