工具类:
import java.sql.*;
import java.util.Vector;
public class Util {
//链接数据库工具包
public static Connection getConnection(){
Connection cn=null;
try {
Class.forName("org.gjt.mm.mysql.Driver");
try {
cn= DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/grame_snack_greed","root","121788");
} catch (SQLException throwables) {
throwables.printStackTrace();
}
} catch (ClassNotFoundException classNotFoundException) {
classNotFoundException.printStackTrace();
}
return cn;
}
//搜素数据库信息
public static ResultSet Select(String sql, Vector v){
ResultSet rs=null;
try {
Connection cn=getConnection();
PreparedStatement ps=cn.prepareStatement(sql);
for(int i=0;i<v.size();i++){
ps.setString(i+1,v.get(i).toString());
}
rs=ps.executeQuery();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return rs;
}
//增删改 工具包
public static int Updata(String sql,Vector v){
int n=0;
try {
PreparedStatement ps=getConnection().prepareStatement(sql);
for(int i=0;i<v.size();i++){
ps.setString(i+1,v.get(i).toString());
}
n=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return n;
}//进行改查删的操作
}
JAVABEAN类
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.*;
import java.util.Random;
import java.util.Vector;
public class panel extends Panel implements KeyListener {
//设置角色坐标 以及 大小
int index_x;
int index_y;
int width;
int height;
//设置食物坐标 以及 大小
int food_x;
int food_y;
int food_width;
int food_height;
//利用构造函数获取数据库 角色 以及 食物 的初始化 数据
public panel(){
try {
Connection cn=Util.getConnection();
Statement st=cn.createStatement();
ResultSet rs=st.executeQuery("select *from information");
while (rs.next()){
index_y=rs.getInt(1);
index_y=rs.getInt(2);
width=rs.getInt(3);
height=rs.getInt(4);
food_x=rs.getInt(5);
food_y=rs.getInt(6);
food_width=rs.getInt(7);
food_height=rs.getInt(8);
}
} catch (Exception e) {
e.printStackTrace();
}
}
//引用画笔 函数,画出角色和食物
public void paint(Graphics g){
g.fillOval(index_x,index_y,width,height);
g.fillOval(food_x,food_y,food_width,food_height);
}
//键盘事件
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
//移动操作的键盘
if(e.getKeyChar()=='a'||e.getKeyCode()==37){
index_x=index_x-10;
}if(e.getKeyChar()=='w'||e.getKeyCode()==38){
index_y=index_y-10;
}if(e.getKeyChar()=='d'||e.getKeyCode()==39){
index_x=index_x+10;
}if(e.getKeyChar()=='s'||e.getKeyCode()==40){
index_y=index_y+10;
//假如吃到食物,角色和食物 进行转变
}if(index_x<=food_x&&index_x+width>=food_x&&index_y<=food_y&&index_y+height>=food_y){
Random r=new Random();
width=width+10;
height=height+10;
food_x=r.nextInt(700)+1;
food_y=r.nextInt(700)+1;
}
//更新数据库的数据
String sql="update information set index_x=?,index_y=?,width=?,height=?,food_x=?,food_y=?,food_width=?,food_height=?";
Vector v=new Vector();
v.add(index_x);
v.add(index_y);
v.add(width);
v.add(height);
v.add(food_x);
v.add(food_y);
v.add(food_width);
v.add(food_height);
int n= Util.Updata(sql,v);
// System.out.println(n);
repaint();
}
@Override
public void keyReleased(KeyEvent e) {
}
}
测试类:
import java.awt.*;
public class test {
public static void main(String[] args) {
Frame f=new Frame("键盘事件");
f.setSize(800,800);
f.setLocationRelativeTo(null);
panel p=new panel();
f.add(p);
f.addKeyListener(p);
p.addKeyListener(p);
f.setVisible(true);
}
}
数据库初始化信息
create table Information(
index_x int not null,
index_y int not null,
width int not null,
height int not null,
food_x int not null,
food_y int not null,
food_width int not null,
food_height int not null
);
insert into information values(20,20,50,50,30,30,20,20);