package rpgGenerateor;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Scanner;
/**
*
* @author Administrator
*
*/
public class RPG {
PreparedStatement psql;
boolean flag=false;
Conn conn = new Conn();
Connection connection = null;
public void printout(String type,String name) throws Exception{
boolean flag=false;
Conn conn = new Conn();
Connection connection = null;
connection = conn.getCon();
connection = conn.getCon();
String sql="select * from "+type+" where name=?";
psql=connection.prepareStatement(sql);
psql.setString(1, name);
ResultSet rs = psql.executeQuery();
while(rs.next()) {
String names=rs.getString("name");
int speed=rs.getInt("speed");
int power=rs.getInt("power");
int stamina=rs.getInt("stamina");
int wit=rs.getInt("wit");
System.out.println(" 速度:"+speed+" 力量:"+power+" 耐力:"+stamina+" 智力:"+wit);
}
psql.close();
connection.close();
}
public void people() throws Exception {
System.out.println("请选择人物:刘备/关羽/张飞/诸葛亮");
Scanner scan2=new Scanner(System.in);
String people;
while(flag==false) {
people=scan2.next();
if(people.equals("刘备")) {
flag=true;
System.out.print("姓名:刘备");
printout("people","liubei");
}
else if(people.equals("关羽")) {
flag=true;
System.out.print("姓名:关羽");
printout("people","guanyu");
}
else if(people.equals("张飞")) {
flag=true;
System.out.print("姓名,张飞");
printout("people","zhangfei");
}
else if(people.equals("诸葛亮")) {
flag=true;
System.out.print("姓名:诸葛亮");
printout("people","zhugeliang");
}
else {
System.out.println("人物不存在,请正确输入");
}
}
scan2.close();
}
public void animal() throws Exception{
boolean flag=false;
while(flag==false) {
System.out.println("请选择人物:梦奇/孙悟空/妲己");
Scanner scan2=new Scanner(System.in);
String people=scan2.next();
if(people.equals("梦奇")) {
flag=true;
System.out.print("姓名:梦奇");
printout("animal","mengqi");
}
else if(people.equals("孙悟空")) {
flag=true;
System.out.print("姓名:孙悟空");
printout("animal","sunwukong");
}
else if(people.equals("妲己")) {
flag=true;
System.out.print("姓名:妲己");
printout("animal","daji");
}
else {
System.out.println("人物不存在,请正确输入");
}
}
}
public static void main(String[] args) throws Exception {
boolean flag=false;
RPG rpg=new RPG();
Scanner scan2=new Scanner(System.in);
System.out.println("请选择种族:人族/兽族");
while(flag==false) {
String type=scan2.next();
if(type.equals("人族")) {
flag=true;
try {
rpg.people();
} catch (Exception e) {
e.printStackTrace();
}
}
else if(type.equals("兽族")) {
flag=true;
rpg.animal();
}
else {
System.out.println("种族不存在,请正确输入");
}
}
scan2.close();
}
}
代码与数据库相连接,通过读取数据库信息,来实现选择人物,输出人物相应的信息
- 执行数据库语句有两种方式
1) 通过createStatement()成statement语句,调用statement对象的executeQuery(sql)执行mysql语句,一般用来执行查询语句。
Statement statement=connection.createStatement();
statement.execureQuery(sql);
2)通过preparedStatement(sql)生成preparedStatement语句,调用对象的executeQuery()执行,可以调用对象的SetString()为preparedStatement语句赋值,多用于insert, update, alter等语句。
String sql="select * from people where name=?";
psql=connection.prepareStatement(sql);
psql.setString(1, name);
ResultSet rs = psql.executeQuery();
两者的区别:
用st,在动态组装sql时,执行动态的sql语句;
用ps时,可以更好的进行维护,避免sql语句的注入,效率更高
- Scanner过多生成会造成nullpointerExection异常,一般主函数生成一个,主函数关闭一次,类函数生成,不用关闭
- 数据库连接包括4个部分 加载驱动,获取连接对象,穿件执行语句并执行SQL,关闭jdbc并释放资源