Accessing JDBC connection variable from one class to another class in java

时间:2016-11-09 08:25:01

标签: java sql oracle jdbc

I am beginner in java.

I want to access variable JDBC database connection variable conn from class test.java to Test2.java in jdk 1.8 . Both classes are in Tool package. Database name is UOHStudents and table is usertest

My question is : can I access the connection variable by making object in another class? If yes then please check the code where am I doing mistake?

test.java

mport java.sql.*;
public class test 
{
    static String USERNAME= "root";
    static String CONN_STRING= "jdbc:mysql://localhost:3306/UOHStudents";
    public static void main(String args[])
    {
        try
        {
            Connection conn;
            Statement st;
            ResultSet rs;
            //get connection
           conn = DriverManager.getConnection(CONN_STRING,USERNAME,"");
           st=conn.createStatement();
                     String sql1="CREATE TABLE if not exists usertest(name varchar(10), surname varchar(10),FatherName varchar(25))";
                     st.executeUpdate(sql1);

             System.out.println("table created");

                    String sql2="INSERT INTO usertest values('preeti', 'singh','Mr shiv kumar singh'),('neha', 'singh','Mr shiv kumar singh'),('anil', 'kumar','Mr shiv kumar singh'),('rahul', 'kumar','Mr shiv kumar singh'),('ankita',      'kumari','Mr shiv kumar singh')";

                     st.executeUpdate(sql2);
             System.out.println("record inserted");

                    String sql3= "SELECT * FROM usertest where surname='kumar'";
                     st.executeQuery(sql3);
             rs=st.getResultSet();    
             while(rs.next())
             {
                    System.out.println(rs.getString("name"));
                    System.out.println(rs.getString("surname"));
                    System.out.println(rs.getString("FatherName"));
             }
        }

        catch(Exception e)
        {   
             System.out.println(e);
        }


    }
}

Test2.java

package Tool;
import java.sql.*;
public class Test2
{
    public static void main (String [] args) throws SQLException 
    {
        Connection conn1;
        Statement st1;
        ResultSet rs1;
         Tool.test ts = new Tool.test();
         public  Test2()
                 {
                     this.conn1 = ts.conn;
                     this.rs1 = ts.rs;
                     this.st1 =ts.st;

                 }
         String p = "Select * from usertest where name = 'preeti'";
         st1.executeQuery(p);
         rs1 = st1.getResultSet();
         while(rs1.next())
             {
                    System.out.println(rs1.getString("name"));
                    System.out.println(rs1.getString("surname"));
                    System.out.println(rs1.getString("FatherName"));
             }    
    }
}

1 个答案:

答案 0 :(得分:0)

与注册驱动程序和连接数据库相关的代码存在于main类的test方法中。您需要将构造函数声明为类test并在构造函数中调用main方法。尝试维护以下类Connection, Statement, ResultSet;

的类级变量
public class test
{
    Connection conn;
    Statement st;
    ResultSet rs;
    public test()
    {
        main()
    }
    ....
}