RPG人物生成器dos視窗
阿新 • • 發佈:2018-12-13
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並釋放資源