java我在getconnection的参数方面有错误吗?
您好,我有一个编译问题,我无法在getconnection()中输入什么来连接我的数据库强> Netbeans表示找不到关于getconnection()的符号。我不知道我该怎么解决我的问题。代码必须在Java1.5中编译
public DeleteOutil(String outil, JButton toEnable) {
this.outil = outil;
this.toEnable = toEnable;
}
@Override
public Void doInBackground() {
PreparedStatement stmt=null;
String wql = "DELETE FROM outil WHERE id_outil=?";
try {
Connexion con = Connexion.getConnection();
stmt = con.prepareStatement(wql);
stmt.setString(1, "outil");
stmt.executeUpdate();
}
catch (Exception e)
}
finally {
if ( stmt!=null ) {
// fermer/libérer la ressource
try {
stmt.close();
}
catch (Exception e) {
}
}
} return null;
}
**this my connect code:**
public class Connexion {
String urlPilote="com.mysql.jdbc.Driver";//Direction pour charger le pilote
String urlBasedonnees="jdbc:mysql://localhost:3306/bdboiteoutil";// Direction pour la connexion à la base de données
Connection conn;
public Connexion () {
//On charge notre pilote
try{
Class.forName(urlPilote);
System.out.println("Le pilote est chargé");
}
catch(ClassNotFoundException ex){
System.out.println(ex);
}
// On se connecte à la base de donnée
try{
conn=DriverManager.getConnection(urlBasedonnees,"root","");
System.out.println("La Base de données est chargé");
}
catch(SQLException ex){
System.out.println(ex);
}
}
Connection ObtenirConnexion(){
return conn;
}
}
# 1 楼答案
注意:答案针对Java 5进行了更新
您不需要注册JDBC驱动程序,因为这应该由JDBC驱动程序自动完成。jar文件
假设
Connexion
的主要目的是封装数据库连接字符串和凭据,那么您的代码如下所示:该代码将在每次调用
doInBackground()
时创建到数据库的新连接。由于连接到数据库是一个缓慢的操作,您可能希望在调用doInBackground()之间保持数据库连接的活动状态,在这种情况下,您需要决定在何处维护该连接,以及该连接应保持多长时间# 2 楼答案
请检查你的密码。因为在执行
stmt.setString
时stmt
为空,所以它在NPE中会失败