Results 1 to 15 of 15
Like Tree2Likes
  • 2 Post By Fubarable

Thread: Connect to SQl

  1. #1
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Connect to SQl

    hi
    i need to a sample code for connect to sql 2008 for fetch data
    please help me
    thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Connect to SQl

    This is not a very good question for a forum since surely Google will find you sample code, and then if you have any problems understanding the concepts a better question for this site would be to ask about your points of confusion or about any errors in your code attempts.

  3. #3
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Connect to SQl

    thanks for your answer
    i try this code :

    import java.sql.*;


    public class ConnectURL {

    public static void main(String[] args)
    {
    DB db = new DB();
    db.dbConnect("jdbc:sqlserver://localhost:1433/test", "sa", "123" );
    }
    }
    class DB{
    public void dbConnect( String db_connect_string,
    String db_userid,
    String db_password)
    {
    try{
    Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
    Connection conn = DriverManager.getConnection(
    db_connect_string,
    db_userid,
    db_password);
    System.out.println( "connected" );
    }
    catch( Exception e ){
    e.printStackTrace();
    }
    }
    };
    but i meet error
    please help me

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: Connect to SQl

    Quote Originally Posted by vahidbakhtiary View Post
    but i meet error
    please help me
    Oh dear; and now you want us to guess which error? I'll start: does the error start with an S?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Connect to SQl

    i have 2 question :
    do my code is correct
    and
    i get this error :
    java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at DB.dbConnect(ConnectURL.java:18)
    at ConnectURL.main(ConnectURL.java:9)

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: Connect to SQl

    Quote Originally Posted by vahidbakhtiary View Post
    i have 2 question :
    do my code is correct
    and
    i get this error :
    java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
    Either you misspelled that class name or that class is not on your classpath.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Connect to SQl

    thanks for your answer

    please if possible show me how to connect to sql and fetch records

    thanks

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default Re: Connect to SQl

    Step 1, see post 6. Step 2: get the driver to your database. Step 3: add the driver to your classpath when your run the program. Step 4: verify the correct name of your driver. Steps 2, 3, and 4 can be greatly aided by the use of your favorite web search engine.

  9. #9
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Connect to SQl

    thanks for your answer
    i'm a .Net programmer
    and i'm a new java programmer

    if possible show me with cod

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Connect to SQl

    No. This is a forum, not a free coding service. As already said, there are tons of code samples to be found on the internet.

    Also go through the following:
    Forum Rules
    Guide For New Members
    BB Code List - Java Programming Forum

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    Join Date
    Jun 2012
    Posts
    14
    Rep Power
    0

    Default Re: Connect to SQl

    i try this code :

    String driverName = "com.sql.jdbc.Driver";
    try
    {
    Class.forName(driverName);
    }
    catch (Exception e)
    {

    System.err.println(e.getMessage());
    }
    and meet this error :
    com.sql.jdbc.Driver
    please help me

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,763
    Blog Entries
    7
    Rep Power
    21

    Default Re: Connect to SQl

    Quote Originally Posted by vahidbakhtiary View Post
    i try this code :

    String driverName = "com.sql.jdbc.Driver";
    ...
    How strange, because first you had:

    Java Code:
    Class.forName( "com.microsoft.sqlserver.jdbc.SQLServerDriver" );
    You do have to make up your mind; which one is it?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Connect to SQl

    I'm guessing that he's just trying to use with random code downloaded from the internet, that he doesn't yet have a database per se.
    JosAH and DarrylBurke like this.

  14. #14
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Connect to SQl

    Quote Originally Posted by vahidbakhtiary View Post
    i try this code :

    String driverName = "com.sql.jdbc.Driver";
    try
    {
    Class.forName(driverName);
    }
    catch (Exception e)
    {

    System.err.println(e.getMessage());
    }
    and meet this error :
    com.sql.jdbc.Driver
    please help me
    I gave you a link to the forum section about using code tags. So, are you being obstinate or just plain lazy?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default Re: Connect to SQl

    Microsoft's MSDN site has plenty of examples of connecting to SQLServer using JDBC, including what you need to do to get it to work.
    In fact the first few links on a decent web search are all MSDN articles on it.
    As a .NET programmer I'm sure you are aware of MSDN, so why you have chosen to come here first is slightly bizarre to say the least.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Connect to wap
    By HoangNgoc in forum CLDC and MIDP
    Replies: 2
    Last Post: 12-23-2010, 10:13 AM
  2. connect through usb
    By taufiqbzz in forum Networking
    Replies: 0
    Last Post: 11-02-2010, 12:04 AM
  3. Connect 4
    By pansylea in forum New To Java
    Replies: 0
    Last Post: 12-11-2009, 04:36 PM
  4. how to connect with my GUI
    By doha786 in forum New To Java
    Replies: 2
    Last Post: 04-01-2009, 05:59 PM
  5. how connect
    By herfnai in forum JDBC
    Replies: 1
    Last Post: 08-18-2008, 07:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •