making connection to java program using sql

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2254 Views - Last Post: 21 August 2012 - 02:31 AM Rate Topic: -----

#1 mickyy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-August 12

making connection to java program using sql

Posted 15 August 2012 - 09:34 PM

Hello ,
My name is Monika Bhatnagar.I am trying to connect my java program (in netBEans 7.0 beta version) to SQL server 2005 which is installed using windows authentication mode.So, i have not given any username / password while installing it. The database in SQL express management studio is created by running it as administrator .So, here i am getting confused about what username and password should be given while making the connection in the program. I have tried to connect it by leaving the spaces but is still not working. Can anyone make suggestions about it. Thank you in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: making connection to java program using sql

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,181
  • Joined: 20-September 08

Re: making connection to java program using sql

Posted 16 August 2012 - 04:35 AM

Generally speaking, the correct procedure would be to create a regular user account, having logged into your admin app as administrator. You would create a username and password for that user, and you would then connect as that user in your Java app.
Was This Post Helpful? 0
  • +
  • -

#3 mickyy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-August 12

Re: making connection to java program using sql

Posted 16 August 2012 - 10:59 PM

it is still not working. :(
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,181
  • Joined: 20-September 08

Re: making connection to java program using sql

Posted 17 August 2012 - 02:58 AM

http://technojeeves..../116-doesntwork
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: making connection to java program using sql

Posted 17 August 2012 - 06:05 AM

"I am sick, please Doctor prescribe me pills"

What seriously do you think the doctor can do ?
Was This Post Helpful? 0
  • +
  • -

#6 mickyy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-August 12

Re: making connection to java program using sql

Posted 17 August 2012 - 08:38 PM

View Postpbl, on 17 August 2012 - 06:05 AM, said:

"I am sick, please Doctor prescribe me pills"

What seriously do you think the doctor can do ?




What you talking about? Sorry,I did not got your meaning.?
Was This Post Helpful? 0
  • +
  • -

#7 mickyy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-August 12

Re: making connection to java program using sql

Posted 17 August 2012 - 08:46 PM

Ohh Sorry , now i got your meaning. Here is the java code in which problem is occurring. When I run this program ,only the exception blockis running giving the output "Error Occurred" although I have created the database named "school" having the table "students".The table contains three values also.I have created DSN named as "mm".I have installed SQL server2005 using windows authentication mode. Now please suggest me the pills.
package javaapplication2;

import java.sql.*;
public class JavaApplication2
{
    public static void main(String args[])
    {
        int i=0;
        try
        {
            String str="Select fname from students";
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con=DriverManager.getConnection("jdbc:odbc:mm","","");
            System.out.println("Connection open");
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery(str);
            System.out.println("fname\t");
            while(i<3)
            {
                rs.next();
                String lname=rs.getString("lname");
                System.out.println(lname);
                i++;
                
            }
                con.close();    
        }
        catch(Exception ex)
        {
            System.out.println("error occured");
            
      
        }
    }
}






Was This Post Helpful? 0
  • +
  • -

#8 RCharles  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 27
  • Joined: 12-August 12

Re: making connection to java program using sql

Posted 17 August 2012 - 11:48 PM

When I was doing my project to connect with mysql, im using this code.

import java.sql.*;
import javax.swing.*;
public class mysqlconnect {
    Connection conn = null;
    public static Connection ConnectDB(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/databasename","root","mysql");
            //JOptionPane.showMessageDialog(null, "Connection Established");
            return conn;
            
        }catch (Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }
    
}


Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,181
  • Joined: 20-September 08

Re: making connection to java program using sql

Posted 18 August 2012 - 03:36 AM

Quote

catch(Exception ex)
{
    System.out.println("error occured");
}


is almost useless. What you need is

catch(Exception ex)
{
    ex.printStackTrace();
}


Then tell us what the output is. I'm getting strong deja vu on this ...
Was This Post Helpful? 0
  • +
  • -

#10 mickyy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-August 12

Re: making connection to java program using sql

Posted 18 August 2012 - 08:11 AM

After altering my code in exception block I get the following error :

run:
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user 'monika-PC\monika'.
	at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6957)
	at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7114)
	at sun.jdbc.odbc.JdbcOdbc.SQLDriverConnect(JdbcOdbc.java:3073)
	at sun.jdbc.odbc.JdbcOdbcConnection.initialize(JdbcOdbcConnection.java:323)
	at sun.jdbc.odbc.JdbcOdbcDriver.connect(JdbcOdbcDriver.java:174)
	at java.sql.DriverManager.getConnection(DriverManager.java:582)
	at java.sql.DriverManager.getConnection(DriverManager.java:185)
	at javaapplication2.JavaApplication2.main(JavaApplication2.java:13)
BUILD SUCCESSFUL (total time: 0 seconds)





Here "monika-pc\monika" is the name of SQL server 2005
.
Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,181
  • Joined: 20-September 08

Re: making connection to java program using sql

Posted 18 August 2012 - 08:27 AM

Looks like an authentication problem
Was This Post Helpful? 0
  • +
  • -

#12 mickyy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-August 12

Re: making connection to java program using sql

Posted 18 August 2012 - 08:54 AM

How can I solve this?
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,181
  • Joined: 20-September 08

Re: making connection to java program using sql

Posted 18 August 2012 - 09:57 AM

Firstly, use a proper jdbc driver for SQL Server, not the jdbc/odbc bridge that you're currently using
Was This Post Helpful? 1
  • +
  • -

#14 mickyy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 15-August 12

Re: making connection to java program using sql

Posted 18 August 2012 - 10:14 AM

I only know jdbc/odbc driver to connect with SQL server.Can you please tell which SQL driver?
What should I write in Java Program?
Was This Post Helpful? 0
  • +
  • -

#15 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,181
  • Joined: 20-September 08

Re: making connection to java program using sql

Posted 18 August 2012 - 10:30 AM

You should probably have one with your SQL Server distro. If you haven't, try the http://jtds.sourceforge.net/
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2