java电话簿_java小程序电话簿

packagecom_wy;import java.sql.*;importjava.util.ArrayList;importjava.util.List;importjava.util.Scanner;public classFindphone {public staticConnection con;

Statement sta;

ResultSet res;

String name;

String number;public voidconnectsql(){

String URL= "jdbc:sqlserver:// localhost: 1433; DatabaseName = Phone;";

String USER= "admin";

String PASSWORD= "admin";try{

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

}catch(ClassNotFoundException e) {

e.printStackTrace();

}try{

con=(Connection) DriverManager.getConnection(URL,USER,PASSWORD);

System.out.println("连接成功");

}catch(SQLException e) {

e.printStackTrace();

}

}public voidinserts(){try{

sta=con.createStatement();

}catch(SQLException e) {

e.printStackTrace();

}

Scanner sc= newScanner(System.in);

System.out.println("请输入新增name:");

name=sc.nextLine();

System.out.println("请输入新增number:");

number=sc.nextLine();

String str= "insert into phonebook(name,number) values('"+name+"',"+number+")";try{

sta.executeUpdate(str);

System.out.println("添加成功");

}catch(SQLException e) {

System.out.println(e.getMessage());

System.out.println("添加失败");

}

}public voidfinds(){

List list = new ArrayList<>();try{

sta=con.createStatement();

}catch(SQLException e) {

e.printStackTrace();

}

Scanner sc= newScanner(System.in);

System.out.println("请输入查找name:");

name=sc.nextLine();

String str= "select number from phonebook where name='"+name+"'";try{

res=sta.executeQuery(str);

}catch(SQLException e) {

e.printStackTrace();

}try{while(res.next()){

System.out.println(res.getString("number"));

}

}catch(SQLException e) {

e.printStackTrace();

}

}public voidupdates(){try{

sta=con.createStatement();

}catch(SQLException e) {

e.printStackTrace();

}

Scanner sc= newScanner(System.in);

System.out.println("请输入修改name:");

name=sc.nextLine();

System.out.println("请输入修改number:");

number=sc.nextLine();

String str= "update phonebook set number='"+number+"' where name='"+name+"'";try{

sta.executeUpdate(str);

System.out.println("修改成功");

}catch(SQLException e) {

e.printStackTrace();

}

}public voiddeletes(){try{

sta=con.createStatement();

}catch(SQLException e) {

e.printStackTrace();

}

Scanner sc= newScanner(System.in);

System.out.println("请输入删除修改name:");

name=sc.nextLine();

String str= "delete from phonebook where name = '"+name+"'";try{

sta.executeUpdate(str);

System.out.println("删除成功");

}catch(SQLException e) {

e.printStackTrace();

}

}public static voidmain(String[] args) {int num=0;

Findphone findphone= newFindphone();

findphone.connectsql();

System.out.println("1.新增");

System.out.println("2.查找");

System.out.println("3.修改");

System.out.println("4.删除");

Scanner sc= newScanner(System.in);

System.out.println("请选择:");

num=sc.nextInt();switch(num){case 1: findphone.inserts(); break;case 2: findphone.finds(); break;case 3: findphone.updates(); break;case 4: findphone.deletes(); break;

}try{

con.commit();

}catch(SQLException e) {

e.printStackTrace();

}try{

con.close();

}catch(SQLException e) {

e.printStackTrace();

}

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值