私はデータベースとしてSQLiteを使用するJavaアプリケーションを作成しています。私はデータベース接続のために別のクラスファイルを使用し、DB接続のために他のクラスでそれを使用しようとしています。は、Javaの別のクラスの1つのクラスにアクセスできない
private void insertActionPerformed(ActionEvent evt) {
//Statement statement= null;
// PreparedStatement ps = null;
//Connection con = null;
try {
Connect_db con = new Connect_db();
//con = conn.getConnection();
String query = "insert into emp_info (emp_id,emp_pwd,emp_name,emp_addres,emp_salary) values (?,?,?,?,?)";
Connection conn = con.getConnection();
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, Integer.parseInt(emp_id.getText()));
ps.setString(2, emp_pwd.getText());
ps.setString(3, emp_name.getText());
ps.setString(4, emp_addres.getText());
ps.setInt(5, Integer.parseInt(emp_salary.getText()));
ps.executeUpdate(query);
ps.close();
conn.close();
} catch (SQLException ex) {
Logger.getLogger(admin_emp_insert.class.getName()).log(Level.SEVERE, null, ex);
}
}
これは、接続クラスの呼び出しクラスです。
public class Connect_db {
public Connection getConnection()
{
Connection con=null;
try
{
Class.forName("org.sqlite.JDBC");
String dbURL = "jdbc:sqlite:C:/Users/Hasan/Desktop/SQLiteStudio/central.db";
con = DriverManager.getConnection(dbURL);
}
catch(Exception e)
{
e.printStackTrace();
}
return con;
}}
これはSQLiteに接続するための接続クラスです。 しかし、呼び出し側のクラスをコンパイルした後、エラーが出ます。
C:\Users\Hasan\Documents\NetBeansProjects\JavaProject\src\javaproject\admin_emp_insert.java:181: error: cannot access Connect_db
Connect_db con = new Connect_db();
bad source file: C:\Users\Hasan\Documents\NetBeansProjects\JavaProject\src\javaproject\Connect_db.java
file does not contain class javaproject.Connect_db
しかし、両方のクラスは同じフォルダにあります。私はベストを尽くしましたが、解決できませんでした。重複していると思われる場合は、重複して記載してください。そして、それは私の最初の記事であるので、いくつかの間違いがそこにあることができます。
あなたは同じパッケージに入れましたか? – muzzlator
はい、私はそれらを同じパッケージに入れました。 –
そしてそのパッケージの名前は何ですか? – fvu