Java-简单试题

Java的基础知识

1.用java做一个两个数相加求和的程序(命令行的即可)

import java.util.Scanner;
//方法一
public class Demo2 {
public static void main(String[] args) {
  Scanner sc =new Scanner(System.in);
  System.out.println("请输入第一个整数:");
  int a=sc.nextInt();
  System.out.println("请输入第二个整数:");
  int b =sc.nextInt();
  int sum =a+b;
  System.out.println("它们的和为:"+sum);
}
}
import java.util.Scanner;
//方法二
public class Demo1 {

  public static void main(String[] args) {
    // TODO Auto-generated method stub
             Scanner sc =new Scanner(System.in);
             System.out.println("请输入第一个数");
             int m =sc.nextInt();
             System.out.println("请输入第二个数");
             int n=sc.nextInt();
             System.out.println("两个数的和是"+ (m+n));
  }

}

2.然后写SQL语句新建一个表,插入一笔数据,修改一笔数据,删除一笔数据

CREATE DATABASE db0504; 

 USE db0504; 
 CREATE TABLE student (
     sno VARCHAR (10) NOT NULL UNIQUE PRIMARY KEY,
      sname VARCHAR (20) NOT NULL,
      ssex enum ('男', '女') NOT NULL DEFAULT '男',
      sbirth date,
     sclass VARCHAR (10),
     zno CHAR (4)
 ) DEFAULT charset = gb2312;
 #添加操作
 INSERT INTO student
 VALUES('2012010713','丁暖','女','1993-9-9','物联网12-1','z002');
 
 INSERT INTO student (sno,sname,ssex,sbirth,sclass,zno)
 VALUES('2011010503','孔超','男','1993-9-19','自动化11-1','z006');
 
 INSERT INTO student (sno, sname, ssex, zno)
 VALUES('2011010521','韩梅','女','z006');
 
 INSERT INTO student (sname, zno, sno)
 VALUES('韩梅2','z006','2011010522');
 
 INSERT INTO student VALUES
 ('2011010501','孔超1','男','1993-9-19','自动化11-1','z006');
 ('2011010505','孔超2','男','1993-9-19','自动化11-1','z006');
 ('2011010504','孔超3','男','1993-9-19','自动化11-1','z006');
 ('2011010505','孔超4','男','1993-9-19','自动化11-1','z006');
 #修改操作
 UPDATE student
 SET sname = '张三',sbirth = '1993-6-19'
 WHERE sname = '丁暖';

 UPDATE student SET zno = LEFT (zno, 3);
 #删除操作
 delete from student
 where sname='张三';
 
 #查询操作
 SELECT * FROM student;#查询student表的所有记录
 SELECT sno,sname,ssex,sbirth FROM student;#查询部分字段信息
 SELECT * FROM student WHERE age BETWEEN 25 AND 30;

 #清空表操作
 truncate student;#高效
 delete from student;

3.用java做一个查询数据库的程序(命令行的即可,数据库随便一种都可以)

class SqlConnectionUtil {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        String sql;
        String url = "jdbc:MySQL://localhost:3306/test?user=root&password=root&useUnicode=true&characterEncoding=UTF8";
        try {
            Class.forName("com.MySQL.jdbc.Driver");
            conn = DriverManager.getConnection(url);
            stmt = conn.createStatement();
            sql = "select * from user";
            rs = stmt.executeQuery(sql);
            while (rs.next()) {
                String name = rs.getString("name");
                System.out.println("姓名是:" + name);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

编程学习

MySQL数据库的入门基本操作

2020-9-22 10:11:44

编程学习

Java-获取子字符串的索引位置

2020-9-4 15:07:45

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索