I have a requirement where in I have to access the database and place those values in a list and then print those values using multiple threads.currently i use 2 threads and my output is such that each thread establishes separate connection to database and iterates the entire list and hence in out put i get every value twice.pl help in solving this
-
2Will you please place some Code with this ?user284291– user2842912010-12-29 06:18:01 +00:00Commented Dec 29, 2010 at 6:18
-
We have no idea what your problem is. Why do should you not print the values twice?Falmarri– Falmarri2010-12-29 06:20:34 +00:00Commented Dec 29, 2010 at 6:20
Add a comment
|
4 Answers
//this s my code
class Conn
{ Connection getDBConnection(String serverName,String url,String userid,String password) {
Connection conObj = null;
try {Class.forName("oracle.jdbc.OracleDriver");
conObj = DriverManager.getConnectionurl,userId,password);
return conObj;}
catch(Exception e){
}}}
class listObj extends Conn
{ List lst;
Connection con;
String query;
public List resultList() {
try{//connection code
query = "select * from test";
ResultSet rs = st.executeQuery(query);
lst=new ArrayList();
while(rs.next())
{lst.add(rs.getInt(1);}return lst;
} catch(Exception e)
{ }
}}
class Thread1 extends listObj implements Runnable
{ public void run()
{List ls=null;
try {ls=(ArrayList)resultList();
} catch (Exception e) {
e.printStackTrace();
}
ListIterator it= ls.listIterator();
while (it.hasNext()){
System.out.println(it.next());
} }}
class TestMain extends listObj {
p.s.v.m(String a[]) throws Exception {
Thread t1=new Thread(new Thread1());
Thread t2=new Thread(new Thread1());
t1.start();t2.start();}}