栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > Java面试题

华数2016年校招笔试题

Java面试题 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

华数2016年校招笔试题

1. 写一个JDBC连接数据库的代码
public class JDBC {
//public static final String DRIVER = “com.mysql.jdbc.Driver”;
public static final String DRIVER = “sum.jdbc.odbc.jdbcOdbcDriver”;
public static final String URL = “jdbc:odbc:people”;
public static final String USER = “root”;
public static final String PASS = “root”;

public static Connection conn = null;
public static Statement stmt = null;
public static PreparedStatement pstmt = null;
public static ResultSet rs = null;

public static void getInfo() {
try {
Class.forName(DRIVER);//加载驱动类
conn = DriverManager.getConnection(URL,USER,PASS);
stmt = conn.createStatement();
rs = stmt.executeQuery(“select * from Staff where id=’001′”);

//或者使用这样的
String sql = “select * from Staff where id = ?”;
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, “001”);
rs = pstmt.executeQuery();

while(rs.next()){
String name = rs.getString(“name”);
String name1 = rs.getString(1);//此方法更高效
String phone = rs.getString(“phone”);
String address = rs.getString(“address”);
int age = rs.getInt(“age”);
System.out.println(name + ” ” +phone + ” ” + address + ” ” + age);
}
} catch (ClassNotFoundException e) {
System.out.println(“找不到驱动类”);
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (rs != null) {
rs.close();
rs = null;
}
if(stmt != null){
stmt.close();
stmt = null;
}
if(conn != null){
conn.close();
conn = null;
}
} catch (SQLException e) {
e.printStackTrace();
}
}

}
2. 常见的七种排序算法:快速、归并、堆、选择、冒泡、插入和希尔。
public class Sort {
public static void main(String[] args) {
int a[] = {213,45,67,234,5};
//bubbleSort(a);

//selectSort(a);

//insertSort(a);

shellSort(a);

for (int i = 0; i < a.length; i++) {System.out.print(a[i] + " ");}}public static void bubbleSort(int a[]){int tmp;for (int i = a.length-1; i > 0 ; i–) {
for (int j = 0; j < i; j++) {if(a[j] >a[j+1]){
tmp = a[j];
a[j] = a[j+1];
a[j+1]=tmp;
}
}
}
}


public static void selectSort(int a[]){
for (int i = 0; i < a.length; i++) {int k = i;for (int j = i+1; j < a.length; j++) {if( a[k]>a[j] ) k=j;
}
if (k != i) {
int temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}


public static void insertSort(int a[]){
for (int i = 1; i < a.length; i++) {int temp = a[i];int j;for (j = i-1; j >= 0 && temp < a[j]; j--) {a[j+1] = a[j];}a[j+1] = temp;}}public static void shellSort(int a[]){int gap = 0;while(gap <= a.length){//改进版insert排序,增加gap跨区域排序gap = gap*3 +1;}while(gap >0){
for (int i = gap; i < a.length; i+=gap) {int temp = a[i];int j;for (j = i-gap; j>=0&&temp< a[j]; j-=gap) {a[j+gap] = a[j];}a[j+gap] = temp;}gap = (gap-1)/3;}}public static void quickSort(int[] a,int low,int high){ if(low >=high){
return;
}
int start = low;
int end = high;
int temp = a[low];
while(lowtemp){// 从右向左找第一个小于x的数
high–;
}
a[low] = a[high];
while(low int temp = a[0];
a[0]=a[i];
a[i]=temp;
maxHeap(a,1,i);
}
}
private static void createMaxHeap(int[] a) {
int startNode = a.length/2;
for(int i = startNode;i>0;i–){
maxHeap(a,i,a.length);
}
}
private static void maxHeap(int[] a, int startNode, int length) {
int leftNode = 2*startNode;
int rightNode = 2*startNode+1;
int maxNode = startNode;
if(leftNode<=length&&a[leftNode-1]>a[startNode-1]){//只考虑length以内的家族,数组与节点相差1
maxNode = leftNode;
}
if(rightNode<=length&&a[rightNode-1]>a[maxNode-1]){
maxNode = rightNode;
}
if(maxNode!=startNode){
int temp = a[maxNode-1];
a[maxNode-1]=a[startNode-1];
a[startNode-1]=temp;
maxHeap(a,maxNode,length);//调整交换后的最大值对其孩子的影响
}
}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/263953.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号