麻烦与连接SQL数据库到JAVA在NetBeans



我尝试连接netbeans与SQL,但我仍然有一个问题。我想,还是少了点什么。

在这里,我创建了这个类:(这里现在出现错误-另一方面,我不知道,如果它是完全正确的)
public class connection {
   public Connection con;
   public void dbConnect(){
     try {  
       Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
       String connectionUrl = "jdbc:sqlserver://FIO\SQLEXPRESS;" +
           "databaseName=Feedback;integratedSecurity=true";
       con = DriverManager.getConnection(connectionUrl);
    } catch (SQLException e) {
        System.out.println("SQL Exception: "+ e.toString());
    } catch (ClassNotFoundException cE) {
        System.out.println("Class Not Found Exception: "+ cE.toString());   
    }
    }
    public Connection getConnection() {
        return con;
    }
    static PreparedStatement prepareStatement(String sql) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}

然后,在另一个名为Questions的类中,我尝试准备语句并从数据库加载信息:

public class Questions {
public void doQuestion()
{...
 ...
 ...
 ...
 connection con = new connection(); 
 con.dbConnect();
 PreparedStatement preparedStatement;

    try{

      **preparedStatement = con.getConnection().createStatement();**

    String sql = "SELECT Answer FROM Feedback.dbo.Question1 WHERE TrainerFirstName=? AND TrainerLastName=?"

    preparedStatement.setString(1,"ffname");
    preparedStatement.setString(2, "llname");
    ResultSet result = preparedStatement.executeQuery(sql);

     while(result.next()) 
     {
     System.out.println(result.getString(1) + result.getString(2));
      }

      }catch (SQLException ex) {
          Logger.getLogger(Questions.class.getName()).log(Level.SEVERE, null, ex);
       }
         ...
          ...
           ...
             ...
              ...
          }
          }

Netbeans下划线:"preparedStatement = con.getConnection().createStatement(); ", due to "incompatible types: Statement cannot be converted to PreparedStatement."

我克服不了。

然后我试着做这样的事情来解决问题,但问题仍然存在,即使以不同的方式。:

       public class Questions {
         public void doQuestion()
       {...
        ...
         ...
           ...
          connection con = new connection(); 
          con.dbConnect();
          Statement statement;
    try {
        statement = con.getConnection().createStatement();
     String sql = "SELECT Answer FROM Feedback.dbo.Question1 WHERE TrainerFirstName=?      AND TrainerLastName=?"

        **statement.setString(1,"ffname");
        statement.setString(2, "llname");**
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1) + rs.getString(2));
        }
    } catch (SQLException ex) {
        Logger.getLogger(Questions.class.getName()).log(Level.SEVERE, null, ex);
    }

这里,Netbeans下划线"statement.setString(1,"ffname");声明。llname"setString (2);"

由于标签:"无法找到符号象征:方法setString (int,字符串)

语句类型

然后,我尝试以不同的形式发送字符串sql:

           String sql = "SELECT Answer FROM Feedback.dbo.Question1 WHERE TrainerFirstName="+ffname+"AND TrainerLastName="+llname
以这种方式在代码中省略:
statement.setString(1、"ffname");声明。llname"setString (2); 所以,代码看起来像这样:
          public class Questions {
         public void doQuestion()
       {...
        ...
         ...
           ...
          connection con = new connection(); 
          con.dbConnect();
          Statement statement;
    try {
        statement = con.getConnection().createStatement();
         String sql = "SELECT Answer FROM Feedback.dbo.Question1 WHERE TrainerFirstName="+ffname+"AND TrainerLastName="+llname

        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()) {
            System.out.println(rs.getString(1) + rs.getString(2));
        }
    } catch (SQLException ex) {
        Logger.getLogger(Questions.class.getName()).log(Level.SEVERE, null, ex);
    }

这样,一切似乎都很好,但是当我运行一个项目时,出现错误并指向:" statement = cont . getconnection (). createstatement ();当然,程序也会崩溃。

我真不知道该怎么办。

如果有人有想法,请写下来。

非常感谢。

在这个简单的例子中,我连接到一个数据库并创建一个语句,但我不使用cont . getconnection()。createStatement,但只是cont。createStatement()尝试这样做可能是问题所在。我在这里留下了我已经学过的使用I数据库的整个sql演示,如果你将来有其他问题,它可以帮助你。如果您的问题仍然存在,请告诉我。

public static void main(String[] args) {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "pc_control";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "root";
    String password = "";
    try {
        Class.forName(driver).newInstance();
        Connection conn = DriverManager.getConnection(url + dbName, userName, password);
        Statement st = conn.createStatement();
        ResultSet res = st.executeQuery("SELECT * FROM computers");
        while (res.next()) {
            int comp_id = res.getInt("computer_id");
            int emp_id = res.getInt("employee_id");
            String image = res.getString("image");
            String brand = res.getString("brand");
            String model = res.getString("model");
            String m_board = res.getString("m_board");
            String processor = res.getString("processor");
            int ram = res.getInt("ram");
            String hdd = res.getString("hdd");
            String os = res.getString("os");
            String ip_add = res.getString("ip_add");

            System.out.println(comp_id + "t" + emp_id + "t" + image + "t" + brand + "t" + 
                    model + "t" + m_board + "t" + processor + "t" + ram + "t" + hdd + "t" 
                    + os + "t" + ip_add + "t");
        }//            
        conn.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

更新

我们将尝试以这种方式或类似的方式重新定义您的连接类。

我希望这对你有帮助。我很抱歉我不能指出你的错误,但是你对连接的实现对我来说有点奇怪。

public class MySqlConnection {
static String url = "jdbc:mysql://localhost:3306/";
static String dbName = "pc_control";
static String driver = "com.mysql.jdbc.Driver";
static String userName = "root";
static String password = "";
static Statement st;
static Connection conn;
public MySqlConnection() {
}
public static boolean connect() {
    try {
        Class.forName(driver).newInstance();
        conn = DriverManager.getConnection(url + dbName, userName, password);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}
public static ResultSet read(String query) 
{
    ResultSet res = null;
    try {
        st = conn.createStatement();
        res = st.executeQuery(query);
    } catch (SQLException ex) {
        Logger.getLogger(MySqlConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
    return res;
}
}

要执行查询,只需从任何其他类调用该方法:

String query = "SELECT * FROM computers WHERE computer_id= " + id; //or whatever
ResultSet res = MySqlConnection.read(query);

这是一个netbeans上的准备语句示例

Connection con = null;
PreparedStatement ptmt = null;
public void add(Archivos archivos) {
    try {
        String query = "INSERT INTO archivosemisor "
                + "(rfcEmisor, cer, llave, password, idError, resultado)"
                + " VALUES (?,?,?,?,?,?)";
        con = getConnection();
        ptmt = con.prepareStatement(query);
        ptmt.setString(1, archivos.getRfcEmisor());
        ptmt.setString(2, archivos.getCer());
        ptmt.setString(3, archivos.getLlave());
        ptmt.setString(4, archivos.getPassword());
        ptmt.setString(5, archivos.getIdError());
        ptmt.setString(6, archivos.getResultado());
        ptmt.executeUpdate();
    } catch (SQLException e) {
         .......
    } finally {
            .....
    }
}

希望对你有所帮助

相关内容

  • 没有找到相关文章

最新更新