Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #22354 > unrolled thread
| Started by | xodepp shrestha <xodepp@gmail.com> |
|---|---|
| First post | 2013-02-19 00:18 -0800 |
| Last post | 2013-03-01 00:20 -0800 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | xodepp shrestha <xodepp@gmail.com> |
|---|---|
| Date | 2013-02-19 00:18 -0800 |
| Subject | how to access connection object in another class? |
| Message-ID | <9345656c-d843-4e20-a49a-df3b3fd7dfe5@googlegroups.com> |
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);
}
}
[toc] | [next] | [standalone]
| From | Arved Sandstrom <asandstrom2@eastlink.ca> |
|---|---|
| Date | 2013-02-19 04:48 -0400 |
| Message-ID | <u%GUs.78313$_U.46814@newsfe20.iad> |
| In reply to | #22354 |
On 02/19/2013 04:18 AM, xodepp shrestha wrote:
> 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);
> }
> }
>
Where is that second set of code, what class and package? Is it in the
same package, likely not. Do you have an import statement that refers to
the "dbconnect" class? If not, fully qualify it when you reference it.
Other notes:
1. I realize your 1st language is not English; nevertheless
professionalism (and respect for any language that you use) demands that
I point out a few typos - StudentRecord -> StudentRecord, conect() ->
connect().
2. CamelCase your class names, e.g. DbConnect.
3. Your 'url' + 'db' concatenation, *everything* joined is potentially
the JDBC URL, including username and password. Since you're hard-coding
the DB name anyway, just add it to the 'url' variable.
4. Your username and password validation: do you think that's sufficient
to see if either is null? Is an empty username OK?
5. If your code got multiple rows back from the 'userlogin' table it
wouldn't think that was a problem. Also, don't just copy code withoput
understanding it - just because you get a ResultSet doesn't mean you
have to also run a while() loop on it; if you expect one row and must
have one row, a while() loop would be clumsy and obscure that logic.
6. Don't - even in learning code - do that absolute noop with an
exception. Please.
AHS
[toc] | [prev] | [next] | [standalone]
| From | jlp <jlp@jlp.com> |
|---|---|
| Date | 2013-02-19 09:50 +0100 |
| Message-ID | <51233cc3$0$9019$ba4acef3@reader.news.orange.fr> |
| In reply to | #22354 |
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
[toc] | [prev] | [next] | [standalone]
| From | lipska the kat <"nospam at neversurrender dot co dot uk"> |
|---|---|
| Date | 2013-02-19 09:18 +0000 |
| Message-ID | <CvadnZQc_J_i3r7MnZ2dnUVZ8j-dnZ2d@bt.com> |
| In reply to | #22354 |
On 19/02/13 08:18, xodepp shrestha wrote:
> Here is the source code.
> This is the code to connect the database.
>
> package stundentrecord;
>
> import java.sql.Connection;
> import java.sql.DriverManager;
>
I think you probably need something like this
public class ConnectionManager{
public Connection getNewConnection() throws ...{
Connection conn = null;
//get your connection then return it
...
return conn;
}
public void closeConnection(connection conn) throws ...{
//manage the close connection process
...
}
}
now you can do as you wish e.g
class SomeClass{
public SomeType someMethod(){
//should really be a singleton but what the heck
ConnectionManager connections = new Connectionmanager();
Connection conn = connections.getNewConnection();
...
//use the connection
...
//close it
...
connections.closeConnection(conn);
...
// etc
}
}
HTH
lipska
--
Lipska the Kat©: Troll hunter, sandbox destroyer
and farscape dreamer of Aeryn Sun
[toc] | [prev] | [next] | [standalone]
| From | markspace <markspace@nospam.nospam> |
|---|---|
| Date | 2013-02-19 09:43 -0800 |
| Message-ID | <kg0dj6$rrc$1@dont-email.me> |
| In reply to | #22354 |
On 2/19/2013 12:18 AM, xodepp shrestha wrote: > AND HERE I WANT TO USE connection OBJECT BUT IT IS SHOWING ERROR.WHAT TO DO ?? > What we really need is the actual error message it is showing. Please copy and paste the whole thing into your email and send it to this group. The problem from our end is your code is incomplete, so we can't guess what error you are seeing. I have an idea, but as Arved pointed out there are actually a lot of questions that still need to be answered. When your error message refers to line numbers, please point out in your source code which lines those are. Since you have edited the code for the email message, we can't guess which line numbers correspond to your error message.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2013-02-19 11:39 -0800 |
| Message-ID | <edb6caae-d531-49b8-85a3-5c26dd4b9a25@googlegroups.com> |
| In reply to | #22366 |
markspace wrote: > xodepp shrestha wrote: >> AND HERE I WANT TO USE connection OBJECT BUT IT IS SHOWING ERROR.WHAT TO DO ?? No need to shout. > What we really need is the actual error message it is showing. Please > copy and paste the whole thing into your email and send it to this group. > > The problem from our end is your code is incomplete, so we can't guess > what error you are seeing. I have an idea, but as Arved pointed out > there are actually a lot of questions that still need to be answered. > > When your error message refers to line numbers, please point out in your > source code which lines those are. Since you have edited the code for > the email message, we can't guess which line numbers correspond to your > error message. Better yet, http://sscce.org/ -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2013-03-01 00:20 -0800 |
| Message-ID | <q4p0j85u4ctmd29goe7a7clhqtost7e2t5@4ax.com> |
| In reply to | #22354 |
There is nothing magic about a Connection object. See http://mindprod.com/jgloss/scope.html -- Roedy Green Canadian Mind Products http://mindprod.com One thing I love about having a website, is that when I complain about something, I only have to do it once. It saves me endless hours of grumbling.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web