package com.jzscce.Test;
//导包
import java.sql.DriverManager;//类
import java.sql.Connection;//接口
import java.sql.Statement;//接口
import java.sql.ResultSet;//接口
import java.util.Scanner;
public class TestLianJie {
private Connection connection=null;
private Statement statement=null;
private ResultSet resultset=null;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("是否启用(y/n)");
char ch1 = input.next().charAt(0);
if (ch1 == 'y' || ch1 == 'Y') {
int i = 0;
while (i <= 5) {
new TestLianJie().mnun();
System.out.println("是否继续使用(y/n)");
ch1 = input.next().charAt(0);
if (ch1 == 'y' || ch1 == 'Y') {
} else {
System.out.println("谢谢使用!");
}
}
}
}
public void mnun()
{
System.out.println("1、查询全部信息");
System.out.println("2、按姓氏查询");
System.out.println("3、按性别查询");
System.out.println("4、按年龄段查询");
System.out.println("5、退出");
System.out.println("请选择(1/2/3/4/5)");
Scanner input=new Scanner(System.in);
int num=input.nextInt();
this.menuChose(num);
}
/**
* 查询全部信息
*/
public void searchAll()
{
String sql="select * from stus";
this.search(sql);
}
//按姓氏查询
public void searchByName()
{
Scanner input=new Scanner(System.in);
System.out.println("请输入姓氏:");
String stuName=input.next();
String sql="select stuName,stuPwd,stuSex,stuAge,stuAddress from stus where stuName like'" + stuName + "%'";
this.search(sql);
}
//按年龄查询
public void searchByAge()
{
Scanner input=new Scanner(System.in);
System.out.println("请输入开始年龄段:");
String age=input.next();
System.out.println("请输入结束年龄段:");
String age1=input.next();
String sql="select * from stus where stuAge between '"+ age +"' and '"+ age1 +"'" ;
this.search(sql);
}
//按性别查询
public void searchBySex()
{
Scanner input=new Scanner(System.in);
System.out.println("请输入性别:");
String sex=input.next();
String sql="select * from stus where stuSex='"+ sex +"'";
this.search(sql);
}
public void search(String sql) {
try {
// 加载驱动
Class.forName("net.sourceforge.jtds.jdbc.Driver");
// 创建连接
this.connection = DriverManager.getConnection(
"jdbc:jtds:sqlserver://localhost:1433/students", "sa", "123456");
// System.out.println("Success");
this.statement = this.connection.createStatement();
this.resultset = this.statement.executeQuery(sql);
while (this.resultset.next()) {
System.out.println(this.resultset.getString("stuName") + " "+ this.resultset.getString("stuPwd") + " "+ this.resultset.getString("stuSex") + " "+ this.resultset.getInt("stuAge") + " "+ this.resultset.getString("stuAddress"));
}
} catch (Exception e) {
System.out.println(e.toString());
} finally {
try {
this.resultset.close();
this.statement.close();
this.connection.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
}
/**
* 菜单选择
*
* @param flage
*
*
*/
public void menuChose(int flage) {
switch (flage) {
case 1:
// 查询全部信息
this.searchAll();
break;
case 2:
// 按姓氏查询
this.searchByName();
break;
case 3:
// 按性别查询
this.searchBySex();
break;
case 4:
// 按年龄段查询
this.searchByAge();
break;
default:
System.out.println("谢谢使用!");
System.exit(0);// 退出程序
}
}
}
java连接数据库操作进行查询
4星 · 超过85%的资源 需积分: 9 78 浏览量
2010-04-25
12:17:23
上传
评论
收藏 274KB ZIP 举报
peichengya
- 粉丝: 23
- 资源: 22