- 根据如下图所示数据库信息,完成1-3小题。
(1)使用JDBC数据库连接技术,编写JDBCUtil工具类,在该类中定义getCon方法用于获得与数据库的连接对象,定义closeAll方法用于释放连接对象等资源。
import java.sql.*;
public class JDBCUtil {
private static String user="用户名";
private static String password="密码";
private static String url="jdbc:mysql://localhost:3306/数据库名";
private static String driver="com.mysql.jdbc.Driver";
/**
* 连接数据库, 返回 Connection
*/
public static Connection getCon() {
try {
return DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* 关闭连接
*/
public static void closeAll(ResultSet set, PreparedStatement statement, Connection connection) {
try {
if (set != null) {
set.close();
}
if (statement != null) {
statement.close();
}
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
(2)应用类的封装性,创建book表对应的类,其中book表存储图书id(主键、自增)、书名、价格、作者、库存量等信息。
public class Book {
private Integer id;
private String name;
private Double price;
private String author;
private Integer reserve;
public Book() {
}
public Book(Integer id, String name, Double price, String author, Integer reserve) {
this.id = id;
this.name = name;
this.price = price;
this.author = author;
this.reserve = reserve;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getReserve() {
return reserve;
}
public void setReserve(Integer reserve) {
this.reserve = reserve;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", name='" + name + '\'' +
", price=" + price +
", author='" + author + '\'' +
", reserve=" + reserve +
'}';
}
}
(3)创建BookDao类,完成增加图书信息的方法,并编写Test测试类测试。
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class BookDao {
static Connection con = null;
static ResultSet set=null;
static PreparedStatement ps=null;
public static void addBook(String name,double price,String author,int reserve) {
String sql="insert into book values(null,?,?,?,?);";
try {
con=JDBCUtil.getCon();
ps=con.prepareStatement(sql);
ps.setString(1,name);
ps.setDouble(2,price);
ps.setString(3,author);
ps.setInt(4,reserve);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.closeAll(null,ps,con);
}
}
public static void select(){
String sql="select *from book";
con=JDBCUtil.getCon();
try {
ps=con.prepareStatement(sql);
set= ps.executeQuery();
while (set.next()){
System.out.println("Book{" +
"id=" + set.getInt("id") +
", name='" + set.getString("name") + '\'' +
", price=" + set.getDouble("price") +
", author='" + set.getString("author") + '\'' +
", reserve=" + set.getInt("reserve") +
'}');
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.closeAll(set,ps,con);
}
}
}
Test
public class Test {
public static void main(String[] args) {
BookDao.addBook("老人与海",32.1,"海明威",4);
BookDao.select();
}
}
2、使用循环和break、continue等流程控制语句,计算1-n全部偶数的和,n的值在控制台输入。
import java.util.Scanner;
/**
* @author whlie(true){learn}
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int sum = 0;
for (int index = 1; index < n; index++) {
if (index % 2 != 0) {
continue;
}
sum += index;
}
System.out.println(sum);
}
}
3、一张纸的厚度大约是0.08mm,对折多少次之后能达到或超过珠穆朗玛峰的高度(8848.13米)。
public class Main {
public static void main(String[] args) {
//为了便于计算将原值扩大100倍
long start=8;
//1mm=0.001m
long end=884813000;
long count=0;
while (start<end){
start*=2;
count++;
}
System.out.println(count);
}
}
4、创建一个存储13张牌面值的集合,如“A,1,2…Q,K”,采用高级for循环或者迭代器方法遍历集合数据输出。
import java.util.*;
/**
* @author whlie(true){learn}
*/
public class Main {
public static void main(String[] args) {
String[] card=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
List<String> list = Arrays.asList(card);
//高级for循环
for(String s:list){
System.out.print(s+" ");
}
//迭代器
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next()+" ");
}
}
}
5、计算控制台输入的任意字符串中每个字符出现的次数。如abbbcccdabc,输出如:
a出现次数为:2
b出现次数为:4
c出现次数为:4
d出现次数为:1
这道题的题意不明,控制台输入字符串的范围没给,从样例推测字符串只包含小写字符。
import java.util.*;
/**
* @author whlie(true){learn}
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s= scanner.nextLine();
char[] chars = s.toCharArray();
int[] array=new int[26];
for(char c:chars){
array[c-'a']++;
}
for (int i = 0; i < array.length; i++) {
if (array[i]>0){
System.out.println((char)(i+'a')+"出现次数为:"+array[i]);
}
}
}
}
6、一辆卡车违反交通规则,撞人后逃跑。现场有三人目击事件,但都没有记住车号,只记下车号的一些特征。
甲说:牌照的前两位数字是相同的;
乙说:牌照的后两位数字是相同的,但与前两位不同;
丙是数学家,他说:四位的车号刚好是一个整数的平方。请根据以上线索求出车号。
public class Main {
public static void main(String[] args) {
for (int i = 1000; i < 10000; i++) {
if (judgeSame(i)&&judgeSqrt(i)){
System.out.println(i);
}
}
}
/**
* 判断甲和乙的条件
* 1.前两位相同
* 2.后两位相同
* 3.前两位和后两位不能相同
*/
public static boolean judgeSame(int i){
char[] chars = (i + "").toCharArray();
return chars[0]==chars[1]&&chars[2]==chars[3]&&chars[0]!=chars[3];
}
/**
* 判断丙的条件
* 1.是否为完全平方数
*/
public static boolean judgeSqrt(int i){
double a=Math.sqrt(i);
return a==(int)a;
}
}
7、马克思手稿中有一道趣味数学问题:有30个人,其中有男人、女人和小孩,在一家饭馆吃饭花了50先令;每个男人花3先令,每个女人花2先令,每个小孩花1先令;问男人、女人和小孩各有几人?
public class Main {
public static void main(String[] args) {
int count=1;
for (int i = 1; i < 16; i++) {
for (int j = 1; j < 25; j++) {
for (int k = 1; k < 50; k++) {
if (i+j+k==30&&3*i+2*j+1*k==50){
System.out.println("第"+(count++)+"种可能:");
System.out.print("男人:"+i+"个 " +
"女人:"+j+"个 " +
"小孩:"+k+"个\n");
}
}
}
}
}
}
8、中国有句俗语叫“三天打鱼两天晒网”。某人从1990年1月1日起开始“三天打鱼两天晒网”,问这个人在以后的某一天中是“打鱼”还是“晒网”?
个人理解:三天打鱼两天晒网是指先连续打三天鱼再连续晒两天网,5天一次循环
1,2,3天打鱼,4,5天晒网,第0天相当于上次循环的第5天
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.*;
/**
* @author whlie(true){learn}
*/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请依次输入年月日,以空格间隔:");
int year=scanner.nextInt();
int month=scanner.nextInt();
int day = scanner.nextInt();
long between = ChronoUnit.DAYS.between(LocalDate.of(1990, 1, 1),
LocalDate.of(year, month, day));
long result=(between+1)%5;
System.out.println(result<4?result==0?"晒网":"打鱼":"晒网");
}
}
9、创建一个名为Shape的接口,接口内有一个计算面积的抽象方法。定义一个Circle类,实现Shape接口,并实现接口里的抽象方法。
public interface Shape {
public double area(double r);
}
public class Circle implements Shape{
@Override
public double area(double r) {
return r*r*Math.PI;
}
}
10、创建如图所示的窗口。
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class TextMain {
public static String Signin;
public static String register;
JFrame jFrame;
public static void main(String[] args) {
init();
}
public static void init()
{
JFrame jFrame=new JFrame("hello");
jFrame.setBounds(200, 200, 500, 400);
jFrame.setLayout(null);
JLabel jLabel1=new JLabel();
jLabel1.setBounds(130, 100, 100, 100);
jLabel1.setText("姓名:");
jFrame.add(jLabel1);
JLabel jLabel2=new JLabel();
jLabel2.setBounds(130, 150, 100, 100);
jLabel2.setText("密码:");
jFrame.add(jLabel2);
final JTextField jTextField=new JTextField();
jTextField.setBounds(165, 135, 200, 30);
jFrame.add(jTextField);
final JTextField jTextField1=new JTextField();
jTextField1.setBounds(165, 185, 200, 30);
jFrame.add(jTextField1);
JButton jButton=new JButton();
jButton.setBounds(130, 250, 85, 50);
jButton.setText("登录");
jFrame.add(jButton);
JButton jButton1=new JButton();
jButton1.setBounds(280, 250, 85, 50);
jButton1.setText("注册");
jFrame.add(jButton1);
jFrame.setVisible(true);
}
}
11、给上述窗口的登录按钮绑定点击事件,点击后获取用户在文本框输入的姓名和密码,打印输出到控制台上。(在10的基础上增加键盘监听事件)
jTextField.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO 自动生成的方法存根
if(e.getKeyChar()==KeyEvent.VK_ENTER&&!jTextField.getText().equals(""))
{
Signin=jTextField.getText();
System.out.println("姓名为:"+Signin);
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {}
});
jTextField1.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyChar()==KeyEvent.VK_ENTER&&!jTextField1.getText().equals(""))
{
register=jTextField1.getText();
System.out.println("密码为:"+register);
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyPressed(KeyEvent e) {}
});
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Signin=jTextField.getText();
System.out.println("姓名为:"+Signin);
register=jTextField1.getText();
System.out.println("密码为:"+register);
}
});
12、使用Canvas类,绘制红色字符串“hello”,显示在JFrame窗口中。
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
public class TextMain {
public static String Signin;
public static String register;
JFrame jFrame;
public static void main(String[] args) {
JFrame jFrame1=new JFrame("Canvas");
jFrame1.setBounds(200, 200, 500, 400);
jFrame1.setLayout(null);
Can can=new Can();
can.setBounds(20, 20, 400, 400);
jFrame1.add(can);
jFrame1.setVisible(true);
}
}
class Can extends Canvas{
@Override
public void paint(Graphics g) {
super.paint(g);
g.setFont(new Font("黑体", Font.BOLD, 50));
g.setColor(new Color(255,0,0));
g.drawString("hello", 100, 100);
}
}