使用java实现数据库编程第六章_java数据库编程——第六章,课后作业

1.向表中添加一条或多条记录,然后在控制台输出记录总数

package JDBC;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.Statement;

public class Dog {

public static void main(String[] args) {

Connection con = null;

Statement sta = null;

PreparedStatement pre = null;

ResultSet rs = null;

String name = "欧欧";

int health = 100;

int love = 90;

String strain = "哈士奇";

try {

//加载驱动

Class.forName("com.mysql.jdbc.Driver");

//建立连接

String url = "jdbc:mysql://localhost:3306/zoology?useUnicode=true&characterEncoding=utf-8";

String user = "root";

String password = "123135";

con = DriverManager.getConnection(url,user,password);

sta = con.createStatement();

StringBuffer sbuff = new StringBuffer(

"insert into zoo(name,health,love,strain) values('");

sbuff.append(name + "',");

sbuff.append(health + ",");

sbuff.append(love + ",'");

sbuff.append(strain + "')");

sta.execute(sbuff.toString());

System.out.println("插入数据成功!");

String sql = "select count(*) from zoo";

pre = con.prepareStatement(sql);

rs = pre.executeQuery(sql);

int sum = 0;

while(rs.next()) {

sum = rs.getInt(1);

}

System.out.println("记录总数:"+sum);

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}finally {

try {

if(sta!=null) {

sta.close();

}

if(con!=null) {

con.close();

}

} catch (Exception e2) {

// TODO: handle exception

e2.printStackTrace();

}

}

}

}

2.宠物主人根据控制台提示输入用户名和密码,如果输入正确,输出:用户登录成功!否则输出:登录失败!

package JDBC;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.util.Scanner;

public class Master {

public static void main(String[] args) {

Connection con = null;

PreparedStatement ps = null;

ResultSet rs = null;

Scanner input = new Scanner(System.in);

System.out.println("请输入用户名和密码:");

System.out.print("请输入用户名:");

String name = input.next();

System.out.print("请输入密码:");

String pass = input.next();

try {

Class.forName("com.mysql.jdbc.Driver");

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/zoology?useUnicode=true&characterEncoding=UTF-8","root","123135");

ps = con.prepareStatement("select * from master where name=? and pass=?");

ps.setString(1, name);

ps.setString(2, pass);

rs = ps.executeQuery();

while(!rs.next()) {

System.out.println("对不起本世界只提供小段和廉颇供主人玩耍!请重新选择:");

System.out.print("请输入用户名:");

name = input.next();

System.out.print("请输入密码:");

pass = input.next();

ps.setString(1, name);

ps.setString(2, pass);

rs = ps.executeQuery();

}

if(name.equals("小段")&&pass.equals("123")) {

System.out.println("登录成功!欢迎来到小段世界!");

}else {

System.out.println("登录成功!欢迎来到廉颇世界!");

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} finally {

try {

if(rs!=null) {

rs.close();

}

if(ps!=null) {

ps.close();

}

if(con!=null) {

con.close();

}

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值