2 uioprty uioprty 于 2016.03.23 12:46 提问

贪吃蛇数据库连接问题 5C

package ads.com;

import java.awt.Color;

import java.awt.Frame;
import java.awt.Graphics;
enum State{
RUN,DEAD;

}

public class Board extends Frame{

private static final int width = 400;
private static final int height = 300;
public static final int tilewidth = 16;
public static final int tileheight = 16;
public static final int row = 15;
public static final int column = 20;
public static final int xoffset = ((width-column*tilewidth)/2);
public static final int yoffset = ((height-row*tileheight)/2);



private Snake snake;
private Food food;
private State state;

public int getRow(){
    return row;

}

public int getColumn(){
    return column;

}


public Board(String title){
    super(title);
    setSize(width,height);
    setVisible(true);
    setResizable(false);
    setState(State.RUN);
    snake = new Snake();

    food = produceFood();
    addWindowListener(new BoardHandler());
    addKeyListener(new KeyHandler(this,snake));

}

public Snake getSnake(){
    return snake;

}



public void drawDecoration(Graphics g){
    g.setColor(Color.RED);
    g.drawRect(xoffset,yoffset,column*tilewidth,row*tileheight);

}

public void paint(Graphics g){
    switch(state){
        case RUN:
            drawDecoration(g);
            drawSnake(g);
            drawFood(g);
            break;
        case DEAD:
            drawDeadMessage(g);
            break;

    }
}   

public void drawSnake(Graphics g){
    g.setColor(Color.GREEN);
    for(int i=0;i<snake.getLength();i++){
        Body body = snake.getBody(i);
        g.fillRect(body.col*tilewidth+xoffset,body.row*tileheight+yoffset,tilewidth,tileheight);

    }

}

public void drawFood(Graphics g){
    g.setColor(Color.BLUE);
    g.fillRect(food.col*tilewidth+xoffset,food.row*tileheight+yoffset, tilewidth, tileheight);

}

public void drawDeadMessage(Graphics g){
    g.drawString("GGGGGGGGGGGGGGGGGG,your score:"+ snake.score +"press F2 to retry",xoffset,height/2);

}

public Food produceFood(){
    boolean finished = false;
    Food myfood = null;
    while(!finished){
        myfood = new Food();
        finished = true;
        for(int i=0;i<snake.getLength();i++){
            if(snake.getBody(i).col==myfood.col&&snake.getBody(i).row==myfood.row)
                finished = false;

        }



    }
    food = myfood;
    snake.score++;
    return food;

}

public Food getFood(){
    return food;

}

public void setState(State s){
    state = s;

}

}

package ads.com;

public class Body {

public int row;
public int col;


public Body(int row,int col){
    this.row = row;
    this.col = col;

}

}

package ads.com;

import java.util.Random;

public class Food extends Body{

public Food(){
    super(0,0);
    Random r = new Random();
    this.row = r.nextInt(Board.row);
    this.col = r.nextInt(Board.column);


}

}

package ads.com;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyHandler extends KeyAdapter{

private Snake snake;
private Board board;
public KeyHandler(Board b,Snake s){
    snake = s;
    board = b;

}



public void keyPressed(KeyEvent e){
    switch(e.getKeyCode()){
        case KeyEvent.VK_UP:
            snake.setNextDirection(Direction.UP);
            break;
        case KeyEvent.VK_DOWN:
            snake.setNextDirection(Direction.DOWN);
            break;
        case KeyEvent.VK_LEFT:
            snake.setNextDirection(Direction.LEFT);
            break;
        case KeyEvent.VK_RIGHT:
            snake.setNextDirection(Direction.RIGHT);
            break;
        case KeyEvent.VK_F2:
            snake.reset();
            board.setState(State.RUN);
            break;

    }

}

}

package ads.com;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class BoardHandler extends WindowAdapter{

public void windowClosing(WindowEvent e){
    System.exit(0);

}

}

package ads.com;

import java.util.ArrayList;
import java.util.List;

enum Direction {
UP, DOWN, LEFT, RIGHT
}

public class Snake {

private List<Body> mybody;
private Direction mydirection;
private Body head;
private Direction nextdirection;
private ScoreTrans st;
public int score;
private boolean flag;

public Snake() {

    mybody = new ArrayList<>();
    st = new ScoreTrans(score);
    reset();

}

public void move(Board b) {

    mydirection = nextdirection;

    switch (mydirection) {
    case UP:
        head = new Body(head.row - 1, head.col);
        break;
    case DOWN:
        head = new Body(head.row + 1, head.col);
        break;
    case LEFT:
        head = new Body(head.row, head.col - 1);
        break;
    case RIGHT:
        head = new Body(head.row, head.col + 1);
        break;
    default:
        head = new Body(0, 0);
        break;

    }
    Food f = b.getFood();
    if(f.row==head.row&&f.col==head.col){

        mybody.add(0,f);
        b.produceFood();


    }
    else{
        mybody.add(0, head);
        mybody.remove(mybody.size() - 1);
    }
    if(checkBodyCollsion()||checkBoundCollsion(b)){
        b.setState(State.DEAD);
        if(!flag)
        {
            st.trans();
            flag = true;
        }
    }
}

public void setNextDirection(Direction d) {
    switch (d) {
    case UP:
        if (mydirection == Direction.DOWN) {
            break;

        } else
            nextdirection = d;
    case DOWN:
        if (mydirection == Direction.UP) {

            break;
        } else
            nextdirection = d;

    case LEFT:
        if (mydirection == Direction.RIGHT) {
            break;
        }
        else nextdirection = d;
    case RIGHT:
        if (mydirection == Direction.LEFT) {
            break;
        }
        else nextdirection = d;

    }
}

public void reset() {
    mybody.clear();
    mybody.add(new Body(7, 4));
    mybody.add(new Body(7, 3));
    mybody.add(new Body(7, 2));
    mybody.add(new Body(7, 1));
    head = mybody.get(0);
    mydirection = Direction.RIGHT;
    nextdirection = Direction.RIGHT;
    score = 0;
    flag = false;

}

public Body getBody(int index) {
    return mybody.get(index);

}

public int getLength() {
    return mybody.size();

}

public boolean checkBodyCollsion(){
    for(int i=1;i<mybody.size()-1;i++){
        if((mybody.get(i).col==head.col)&&(mybody.get(i).row==head.row))
            return true;

    }
    return false;

}

public boolean checkBoundCollsion(Board b){
    if(head.col<0||head.col>=b.column||head.row<0||head.row>=b.row)
        return true;
    return false;
}

public int getScore(){
    return score;
}

}

package ads.com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class OracleOpen {

private static String driver;
private static String url;
private static String username;
private static String password;

static {
    driver="oracle.jdbc.driver.OracleDriver";
    url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
    username="scott";
    password="zhushaowei12";

}

public static Connection open(){

    try {
        Class.forName(driver);
        return DriverManager.getConnection(url,username,password);
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

public static void close(Connection conn){
    if(conn!=null){
        try {
            conn.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

}

package ads.com;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ScoreTrans {
private Snake snake;
private String name = "ads";
private int score;

public ScoreTrans(int score){
    this.score = score;

}

public void trans(){

Connection conn = OracleOpen.open();
String sql = "insert into snake values(?,?)";
try {
    PreparedStatement pstmt = conn.prepareStatement(sql);
    pstmt.setString(1,name);
    pstmt.setInt(2,snake.getScore());
    pstmt.executeUpdate();
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
OracleOpen.close(conn);

}

}

package ads.com;

public class Updater extends Thread{

private Board myboard;
private Snake snake;

public Updater(Board b,Snake s){
    myboard = b;
    snake = s;

}

public void run(){

    while(true){
        snake.move(myboard);
        myboard.repaint();
        try {
            sleep(100);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

}

package ads.com;

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Board board = new Board("snake");


    Updater myupdater = new Updater(board,board.getSnake());

    myupdater.start();

}

}

没加数据库前能正常运行,加了以后发现数据无法存入数据库

7个回答

qq_34396834
qq_34396834   2016.03.23 12:50

dfhfggggjj

qq_34396834
qq_34396834   2016.03.23 12:51

dfhfggggjj

Royal_lr
Royal_lr   Ds   Rxr 2016.03.23 15:25

看下报的什么错,,贴出关键代码,都贴出来,,没人愿意看的

uioprty
uioprty 可以看下吗, 主要是数据库存报错
一年多之前 回复
uioprty
uioprty   2016.03.23 19:09

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at ads.com.OracleOpen.open(OracleOpen.java:25)
at ads.com.ScoreTrans.trans(ScoreTrans.java:19)
at ads.com.Snake.move(Snake.java:66)
at ads.com.Updater.run(Updater.java:17)
Exception in thread "Thread-2" java.lang.NullPointerException
at ads.com.ScoreTrans.trans(ScoreTrans.java:22)
at ads.com.Snake.move(Snake.java:66)
at ads.com.Updater.run(Updater.java:17)

uioprty
uioprty   2016.03.23 19:10

package ads.com;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class OracleOpen {
private static String driver;
private static String url;
private static String username;
private static String password;

static {
driver="oracle.jdbc.driver.OracleDriver";
url="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
username="scott";
password="zhushaowei12";

}

public static Connection open(){

try {
    Class.forName(driver);
    return DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

return null;

}

public static void close(Connection conn){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
}
package ads.com;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ScoreTrans {
private Snake snake;
private String name = "ads";
private int score;
public ScoreTrans(int score){
this.score = score;

}

public void trans(){

Connection conn = OracleOpen.open();
String sql = "insert into snake values(?,?)";
try {
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,name);
pstmt.setInt(2,snake.getScore());
pstmt.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
OracleOpen.close(conn);

}

uioprty
uioprty 这是数据库的代码
一年多之前 回复
qinjisheng_11
qinjisheng_11   2016.03.26 00:46

你那个oracle的jar包没有导入吧!

fredrickhu
fredrickhu   Ds   Rxr 2016.04.08 09:32

这是想做什么?代码看起来是没什么问题的

得把报错信息贴上来

Csdn user default icon
上传中...
上传图片
插入图片