Java面试笔试题及答案汇总终结篇:代码与编程
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) {
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方法,还有是三层嵌套方法。
答:一种分页方法:
输出内容:
//输出翻页连接
合计:/首页 上页 [] 下页 末页
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("老师");
org.w3c.dom.Element wang = doc.createElement("王");
org.w3c.dom.Element liu = doc.createElement("刘");
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));
} catch (Exception e) {
System.out.println (e.getMessage());
}
}
}