Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #22356

Re: how to access connection object in another class?

Path csiph.com!usenet.pasdenom.info!gegeweb.org!feeder.news.orange.fr!not-for-mail
Date Tue, 19 Feb 2013 09:50:12 +0100
From jlp <jlp@jlp.com>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2
MIME-Version 1.0
Newsgroups comp.lang.java.programmer
Subject Re: how to access connection object in another class?
References <9345656c-d843-4e20-a49a-df3b3fd7dfe5@googlegroups.com>
In-Reply-To <9345656c-d843-4e20-a49a-df3b3fd7dfe5@googlegroups.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 8bit
Lines 72
Message-ID <51233cc3$0$9019$ba4acef3@reader.news.orange.fr> (permalink)
Organization les newsgroups par Orange
NNTP-Posting-Date 19 Feb 2013 09:50:11 CET
NNTP-Posting-Host 109.214.119.33
X-Trace 1361263811 reader.news.orange.fr 9019 109.214.119.33:15874
X-Complaints-To abuse@orange.fr
Xref csiph.com comp.lang.java.programmer:22356

Show key headers only | View raw


Le 19/02/2013 09:18, xodepp shrestha a écrit :
> Here is the source code.
> This is the code to connect the database.
>
> package stundentrecord;
>
> import java.sql.Connection;
> import java.sql.DriverManager;
>
> public class dbconnect {
>
>      public void conect(){
>    Connection con = null;
>    String url = "jdbc:mysql://localhost:3306/";
>    String db = "studentRecord";
>    String driver = "com.mysql.jdbc.Driver";
>    String user = "root";
>    String pass = "";
>    try {
>    Class.forName(driver);
>    con = DriverManager.getConnection(url + db, user, pass);
>    if(con==null){
>    System.out.println("Connection cannot be established");
>    }
>
>   // con.close();
>    } catch (Exception e) {
>    System.out.println(e);
>    }
>        }
> }
>
> AND HERE I WANT TO USE connection OBJECT BUT IT IS SHOWING ERROR.WHAT TO DO ??
>
> if(source==login){
> if(username!=null && password!=null){
>
>      Connection conn= null;
>      Statement stmt = null;
>      dbconnect db = new dbconnect();
>
>
>   String query = "SELECT * from userlogin";
>   try{
>   stmt=(Statement) conn.createStatement();
>   ResultSet rs = stmt.executeQuery(query);
>   while (rs.next())
>        {
>        String user = rs.getString("username");
>        String pass=rs.getString("password");
>        System.out.println("Welcome "+user);
>        }
>   }catch(SQLException ex){
>   ex.getMessage();
>   }
>     StundentRecord SR = new StundentRecord();
>
>      }else{
>      JOptionPane.showMessageDialog(null,"Username or password field is empty","error !!",JOptionPane.ERROR_MESSAGE);
>      }
> }
>
your method "conect" must return a Connection not void and you must call 
Connection con=new new dbconnect().conect();

Note : To make your code more readable, you have also to learn how to 
name Classes ( First letter in upper case),  methods, attributes (First 
letter in lower case) ...

-- 
Cordialement
Jean-Louis Pasturel

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

how to access connection object in another class? xodepp shrestha <xodepp@gmail.com> - 2013-02-19 00:18 -0800
  Re: how to access connection object in another class? Arved Sandstrom <asandstrom2@eastlink.ca> - 2013-02-19 04:48 -0400
  Re: how to access connection object in another class? jlp <jlp@jlp.com> - 2013-02-19 09:50 +0100
  Re: how to access connection object in another class? lipska the kat <"nospam at neversurrender dot co dot uk"> - 2013-02-19 09:18 +0000
  Re: how to access connection object in another class? markspace <markspace@nospam.nospam> - 2013-02-19 09:43 -0800
    Re: how to access connection object in another class? Lew <lewbloch@gmail.com> - 2013-02-19 11:39 -0800
  Re: how to access connection object in another class? Roedy Green <see_website@mindprod.com.invalid> - 2013-03-01 00:20 -0800

csiph-web