【发布时间】:2014-05-23 05:12:47
【问题描述】:
我尝试用 SQL 连接 netbeans,但还是有问题。我想,还是少了点什么。
在这里,我创建了这个类: (这里现在发生错误 - 另一方面,我真的不知道,如果它是完全正确的)
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"); statement.setString(2, "llname");"
由于标签:”找不到符号 符号:方法 setString(int,String) location:variable Statement of type Statement"
然后,我尝试以不同的形式发送 String sql:
String sql = "SELECT Answer FROM Feedback.dbo.Question1 WHERE TrainerFirstName="+ffname+"AND TrainerLastName="+llname
并以这种方式在代码中省略:
statement.setString(1,"ffname");
statement.setString(2, "llname");
所以,代码看起来像这样:
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);
}
这样,一切似乎都OK了,但是当我运行一个项目时,出现错误并指向:“ statement = con.getConnection().createStatement(); ”并且程序当然崩溃了。
我真的不知道该怎么办。
如果有人有想法,请写出来。
非常感谢。
【问题讨论】:
-
你需要改为
conn.prepareStatement()
标签: java sql netbeans connection