写了一个连接数据库的BEAN.不知道怎么在主页面调用它?
代码:
package mybean;
import java.io.*;
import java.sql.*;
import com.mysql.jdbc.Driver;
public class connbean {
public static void main(String [] args){
String url = "jdbc:mysql://localhost/study";//连接地址
String username = "root";//用户名
String password = "x7u7w2e1i4";
Connection conn = null;
String sql = null;
Statement stmt = null;
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e){
System.out.println("加载驱动器类出现异常");
}
try{
//数据库连接
conn = DriverManager.getConnection(url,username,password);
stmt = conn.createStatement();
sql = "select * from  product";
ResultSet rs = stmt.executeQuery(sql);//执行SQL语句
while(rs.next()){
//处理查询结果
String pid = rs.getString(1);
                 String pdm = new String(rs.getString(2));
System.out.println(pid + "," + pdm);
                                                                               }
rs.close();//关闭连接
stmt.close();
                                                     conn.close();
}catch(SQLException e){
System.out.println("连接出现异常");
}
}
}
就是想请教一下,没有定义公共变量的BEAN,怎么调用,如何设置属性值?
谢谢

解决方案 »

  1.   

    mybean.connbean b = new mybean.connbean();
    b.main(null);或者 
    mybean.connbean.main(null);
      

  2.   

      你这样些永远也不要想在主页上调用到,原因是你把连接数据库的语句写在了main方法中去了,你到别的地方不肯能调用的到好吧,你应该把连接数据库语句写在一个自己定义的方法中,然后在主页上得到new出这个类的实例以后用该类的对象去点你写连接语句的方法,这样就可以调用的到了..~
      

  3.   

    3楼,4楼你们确认main函数不能被其他位置调用?