java面试题总结(一)
1、写一个标准的单例模式
public class Singleton {
private static Singleton singleton;
static{
singleton=new Singleton();
}
private Singleton(){}
public static Singleton getInstance(){
return singleton;
}
}
2、排序一个数组 [F,a,f,h,A,g,K],要求输出[A,a,F,f,g,h,K]
答:
核心代码:
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);//根据指定的字母方式排序
package lp;
import java.text.SimpleDateFormat;
import java.util.*;
public class ListTest {
public static void main(String [] args ){
//String[] strArray = new String[] {"z", "a", "C"};
// List list = Arrays.asList(strArray);
// Collections.sort(list);//根据元素自然顺序排序
// Collections.sort(list, String.CASE_INSENSITIVE_ORDER);//根据指定的字母方式排序
// Collections.sort(list, Collections.reverseOrder());//根据反转自然顺序方式排序
// Collections.reverse(list);//反转列表排序
}
}
*****************************************
public class Test_Array{
public static char[] sort(char[] c){
for(int i=0;i<c.length;i++){
for(int j=i;j<c.length;j++){
if(c[i]>='A'&&c[i]<='Z'){
if((c[j]>='A'&&c[j]<='Z'&&c[i]>c[j])||(c[j]>='a'&&c[j]<='z'&&c[i]+32>c[j])){
char temp = c[j];
c[j] = c[i];
c[i] = temp;
}
}else if(c[i]<='a'&&c[i]>='z'){
if((c[j]>='a'&&c[j]<='z'&&c[i]>c[j])||(c[j]>='A'&&c[j]<='Z'&&c[i]-32>=c[j])){
char temp = c[j];
c[j] = c[i];
c[i] = temp;
}
}
}
}
}
public void show(char[] d){
for(char u:d){
System.out.println(u);
}
}
public static void main(String[] args){
String str = "FafhAgK";
char[] c = str.toCharArray();
char[] d = Test_Array.sort(c);
Test_Array.show(d);
}
}
***********************************
3、写一个通用的查询语句,连接Orcle数据库的,用prepareStatement
public void testJdbc(){
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
//step1:注册驱动;
Class.forName("oracle.jdbc.driver.OracleDriver");
//step 2:获取数据库连接;
con=DriverManager.getConnection(
"jdbc:oracle:thin:@192.168.0.39:1521:tarena","openlab","open123");
/************************查询************************/
//step 3:创建Statement;
String sql = "SELECT id, fname, lname, age, FROM
Person_test";
ps = con.prepareStatement(sql);
//step 4 :执行查询语句,获取结果集;
rs = ps.executeQuery();
//step 5:处理结果集—输出结果集中保存的查询结果;
while (rs.next()){
System.out.print("id = " + rs.getLong("id"));
System.out.print(" , fname = " +rs.getString("fname"));
System.out.print(" , lname = " +rs.getString("lname"));
System.out.print(" , age = " + rs.getInt("age"));
}
/************************JDBC 修改*********************/
sql = "UPDATE Person_Tbl SET age=23 WHERE id = ?";
ps = con.prepareStatement(sql);
ps.setLong(1, 88);
int rows = ps.executeUpdate();
System.out.println(rows + " rows affected.");
} catch (Exception e){
e.printStackTrace();
} finally{
try{
con.close(); //关闭数据库连接,以释放资源。
} catch (Exception e1) {
}
}
}