how to connect java and mssql?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 2314 Views - Last Post: 17 March 2013 - 05:14 PM Rate Topic: -----

#1 iwanttolearn  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 13-March 12

how to connect java and mssql?

Posted 08 March 2013 - 05:06 AM

here is the error igot

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall


im using java netbeans and ms sql server 2008 r2 and sqljdbc4 jar file

here my codes

try{
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection conn = DriverManager.getConnection ("jdbc:sqlserver://localhost:1433;database=database1;integratedSecurity=true;");
        JOptionPane.showMessageDialog(null, "Connection Established");
        
        return conn;

    }catch(Exception e){
        System.out.print(e);
        return null;
    }


Is This A Good Question/Topic? 0
  • +

Replies To: how to connect java and mssql?

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2556
  • View blog
  • Posts: 10,664
  • Joined: 20-September 08

Re: how to connect java and mssql?

Posted 08 March 2013 - 07:18 AM

Quote

Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall
So - did you?
Was This Post Helpful? 0
  • +
  • -

#3 iwanttolearn  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 13-March 12

Re: how to connect java and mssql?

Posted 08 March 2013 - 07:50 PM

thats my problem ive google it and saw some solution but ive tried them but it didnt work for me.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2556
  • View blog
  • Posts: 10,664
  • Joined: 20-September 08

Re: how to connect java and mssql?

Posted 09 March 2013 - 03:40 AM

Quote

and saw some solution but ive tried them but it didnt work for me.

That tells us absolutely nothing. If you can't be bothered telling us what you've done we can't help
Was This Post Helpful? 0
  • +
  • -

#5 iwanttolearn  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 13-March 12

Re: how to connect java and mssql?

Posted 09 March 2013 - 05:55 AM

ive figured out how to connect java and mssql already.

i have another error sir. can i post it here?

it says
java.lang.NullPointerException

ive tried searching solution but it gives me different scenario

        try{
            
            CallableStatement stored_pro = null;
            stored_pro = conn.prepareCall("{ call selectlogin(?)(?) }");
            stored_pro.setString(1, txtusername.getText());
            stored_pro.setString(2, txtpassword.getText());
            if (rs.next()) {
                
                System.out.println("Successful");
            } else {
                System.out.println("Wrong Username or Password");
            }
            }catch(Exception e){
                
                System.out.println(e);

Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10186
  • View blog
  • Posts: 37,608
  • Joined: 27-December 08

Re: how to connect java and mssql?

Posted 09 March 2013 - 01:17 PM

You set stored_pro to null. Of course you get a NullPointerException when trying to use this object.
Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2556
  • View blog
  • Posts: 10,664
  • Joined: 20-September 08

Re: how to connect java and mssql?

Posted 10 March 2013 - 02:10 AM

View Postiwanttolearn, on 09 March 2013 - 01:55 PM, said:

ive figured out how to connect java and mssql already.

i have another error sir. can i post it here?

it says
java.lang.NullPointerException

ive tried searching solution but it gives me different scenario

That's simply not enough info

http://technojeeves....free/118-errors

mac: con.prepareCall inits the object (if it works ;))
Was This Post Helpful? 0
  • +
  • -

#8 iwanttolearn  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 13-March 12

Re: how to connect java and mssql?

Posted 10 March 2013 - 05:20 AM

using e.printStackTrace(); gives me this message

i think i need to do some research :stupid:

java.lang.NullPointerException
	at NewJFrame.btnloginActionPerformed(NewJFrame.java:131)
	at NewJFrame.access$200(NewJFrame.java:14)
	at NewJFrame$3.actionPerformed(NewJFrame.java:59)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
	at java.awt.Component.processMouseEvent(Component.java:6505)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
	at java.awt.Component.processEvent(Component.java:6270)
	at java.awt.Container.processEvent(Container.java:2229)
	at java.awt.Component.dispatchEventImpl(Component.java:4861)
	at java.awt.Container.dispatchEventImpl(Container.java:2287)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
	at java.awt.Container.dispatchEventImpl(Container.java:2273)
	at java.awt.window.dispatchEventImpl(window.java:2719)
	at java.awt.Component.dispatchEvent(Component.java:4687)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:723)
	at java.awt.EventQueue.access$200(EventQueue.java:103)
	at java.awt.EventQueue$3.run(EventQueue.java:682)
	at java.awt.EventQueue$3.run(EventQueue.java:680)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
	at java.awt.EventQueue$4.run(EventQueue.java:696)
	at java.awt.EventQueue$4.run(EventQueue.java:694)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:693)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2556
  • View blog
  • Posts: 10,664
  • Joined: 20-September 08

Re: how to connect java and mssql?

Posted 10 March 2013 - 05:52 AM

Quote

at NewJFrame.btnloginActionPerformed(NewJFrame.java:131)

So that gives you the source code line number at which something is null
Was This Post Helpful? 0
  • +
  • -

#10 iwanttolearn  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 13-March 12

Re: how to connect java and mssql?

Posted 10 March 2013 - 09:35 PM

oh my i dont understand. im a newbie sir goose
Was This Post Helpful? 0
  • +
  • -

#11 iwanttolearn  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 13-March 12

Re: how to connect java and mssql?

Posted 11 March 2013 - 07:26 AM

another question
i got this error

com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set

BUT it adds the data i inputed from my jtextfield to my database?

why?

try{
    stored_pro = conn.prepareCall("{ call insertpdata(?,?,?,?) }");
    stored_pro.setString(1, this.txtid.getText());
    stored_pro.setString(2, this.txtname.getText());
    stored_pro.setString(3, this.txtaddress.getText());
    stored_pro.setString(4, this.txtage.getText());
stored_pro.executeQuery();    
        System.out.println("Successful");

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

This post has been edited by iwanttolearn: 11 March 2013 - 07:41 AM

Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: how to connect java and mssql?

Posted 11 March 2013 - 09:12 AM

It is a PreparedStament that you want, not a CallableStatement
And if you execute a query, you have to store the ResultSet that it returns somewhere
Was This Post Helpful? 0
  • +
  • -

#13 iwanttolearn  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 84
  • Joined: 13-March 12

Re: how to connect java and mssql?

Posted 12 March 2013 - 04:01 AM

i dont understand sir PBL are you trying to say that my CallableStatement statement is wrong?
and my codes is for PreparedStament? i searched over the internet and got this codes, but modified to suit my needs
i added this

rs= stored_pro.executeQuery();

but still got same error
Was This Post Helpful? 0
  • +
  • -

#14 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2556
  • View blog
  • Posts: 10,664
  • Joined: 20-September 08

Re: how to connect java and mssql?

Posted 12 March 2013 - 08:15 AM

Quote

stored_pro.executeQuery();

Try instead
stored_pro.executeUpdate();

Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: how to connect java and mssql?

Posted 12 March 2013 - 09:20 AM

View Postiwanttolearn, on 12 March 2013 - 06:01 AM, said:

i dont understand sir PBL are you trying to say that my CallableStatement statement is wrong?
and my codes is for PreparedStament? i searched over the internet and got this codes, but modified to suit my needs
i added this

rs= stored_pro.executeQuery();

but still got same error

Sorry my bad.. are sur sure insertpdata is in your Database ?

and that it should return a ResultSet ?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3