class将java程序连接到数据库(接受参数)

时间:2015-04-08 10:37:13

标签: java sql database phpmyadmin

我正在尝试编写一个类,它将接受来自一个quizgame的两个字符串和一个int(用户名和密码以及分数),最终将来自GUI,我刚刚从它传递它们main,并将它们传递到要插入的数据库中。

我添加了JConnector jar文件并在Eclipse中工作。

这是我的代码

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

//public 
class DbConnect {

    private java.sql.Connection con;
    private java.sql.Statement st;
    private ResultSet rs;

    public DbConnect() {
        try {
            Class.forName("com.mysql.jdbc.Driver");

            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3307/nostalgic", "root", "usbw");

            st = con.createStatement();
        } catch (Exception ex) {
            System.out.println("Error is " + ex);
        }

    }

    public void setData(String n, String p, int x) {
        try {

            String query = "select * from nostalgic";
            String query1 = "INSERT INTO nostalgic values (n,p,x)";

            PreparedStatement statement3 = con.prepareStatement(query1);
            statement3.executeUpdate();

            rs = st.executeQuery(query);
            System.out.println("records from database");

            while (rs.next()) {
                String name1 = rs.getString("name");
                String pw = rs.getString("password");
                int score = rs.getInt("score");
                System.out.println("Name : " + name1);
                System.out.println("Password : " + pw);
                System.out.println("Score : " + score);
            }
        } catch (Exception ex) {
            System.out.println(ex);

        }

    }
}

我得到的错误是

  

'字段列表'中的未知列'n'

我可以像'john'那样直接输入一个字符串,但在这种情况下对我来说没用。

5 个答案:

答案 0 :(得分:2)

而不是INSERT INTO nostalgic values (n,p,x)你应该拥有 INSERT INTO nostalgic values (?,?,?)然后:

PreparedStatement statement3 = con.prepareStatement(query1);
statement3.setString(1,n);
statement3.setString(2,p);
statement3.setString(3,x);
statement3.executeUpdate();

答案 1 :(得分:1)

in

String query1 = "INSERT INTO nostalgic values (n,p,x)";

n,p,x没有被这些值替换,它们只被视为一些char,这就是你得到这个错误的原因

编辑:statement3.executeUpdate();之前

        String query1 = "INSERT INTO nostalgic values (?,?,?)";

        PreparedStatement statement3 = con.prepareStatement(query1);
        statement3.setString(1,n);
        statement3.setString(2,p);
        statement3.setInt(3,x);

更新:您可能想知道为什么Unknown column 'n' in 'field list'? 因为sql中的insert查询可以有这个结构

  INSERT INTO table (col1,col2,...) values (`val1`,`val2`,....)

但是如果硬编码,则值必须在``内;如果没有``符号则将其视为列名称。

答案 2 :(得分:0)

米克 问题是,在执行statement3.executeUpdate();时,您不会将实际参数传递给查询。请查看此链接以了解如何操作:http://docs.oracle.com/javase/tutorial/jdbc/basics/prepared.html

答案 3 :(得分:0)

你不能这样做试试这个:

            Class.forName("com.mysql.jdbc.Driver");
            try (Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/nostalgic","root","usbw") {
                PreparedStatement pr = null;
                String query = "INSERT INTO nostalgic VALUES ((?), (?), (?))";
                pr = con.prepareStatement(query);
                pr.setString(1, n);
                pr.setString(2, p);
                pr.setString(3, x);
                int status = pr.executeUpdate();
           }catch (ClassNotFoundException | SQLException e) {
            System.out.println(e.getMessage());
           }

答案 4 :(得分:0)

您的代码中存在一些错误......

  • 正确导入
  • 插入声明

您的代码将是这样的:

import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.jdbc.Statement;
// you imported sql innecessary statements...    


//public 
class DbConnect {

    // dont need complete path, you already imported it  
    private Connection con;
    private Statement st;
    private ResultSet rs;

    public DbConnect() {
        try {
            Class.forName("com.mysql.jdbc.Driver");

            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3307/nostalgic", "root", "usbw");

            st = con.createStatement();
        } catch (Exception ex) {
            System.out.println("Error is " + ex);
        }

    }

    public void setData(String n, String p, int x) {
        try {

            String query = "select * from nostalgic";

            String query1 = "INSERT INTO nostalgic values (?,?,?)";
            // no need to define vars here, 
            // just number of places to be inserted here ----^ 

            PreparedStatement statement = con.prepareStatement(query1);
            // prepare statement
            statement = con.prepareStatement(query);
            // insert the variables in places you prepared before
            // and matching the types you need!!!
            statement.setString(1,n);
            statement.setString(2,p);
            statement.setInt(3,x);    
            rs = st.executeQuery(query);
            System.out.println("records from database");

            while (rs.next()) {
                String name1 = rs.getString("name");
                String pw = rs.getString("password");
                int score = rs.getInt("score");
                System.out.println("Name : " + name1);
                System.out.println("Password : " + pw);
                System.out.println("Score : " + score);
            }
        } catch (Exception ex) {
            System.out.println(ex);

        }

    }
}