[英]Access JDBC methods from another class within the same package
I have a database
class which imports java.sql.*
and have following variables 我有一个导入java.sql.*
的database
类,并且具有以下变量
Connection connect;
PreparedStatement statement;
ResultSet result;
public database() {} //constructor to connect to db [successful connection]
Now I create its instance in another class database db = new database();
现在,我在另一个类database db = new database();
创建它的实例database db = new database();
, and I have to do something like this: ,并且我必须执行以下操作:
statement = connect.statement("sql query");
But to do this in another class, I have to do it like this: 但是要在另一堂课中做到这一点,我必须这样做:
db.statement = db.connect.db.statement("sql query");
It is showing error as well as it looks weird. 它显示错误以及看起来很奇怪。
I think the possible solution could be to create local variables in another class 我认为可能的解决方案可能是在另一个类中创建局部变量
Connection connect = db.connect;
PreparedStatement statement = db.statement;
Which makes the database
a useless class. 这使database
成为无用的类。
There was a misunderstanding in my approach 我的做法有误会
statement = connect.statement("sql query"); <--wrong statement
statement = connect.prepareStatement("sql query"); <--correct statement
so in another class it should be 所以在另一堂课中
db.statement = db.connect.prepareStatement("sql query"); <--worked perfectly
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.