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();
}
}
}
}