Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Java
  4. Connection to SQL using JDBC in Java

Connection to SQL using JDBC in Java

Scheduled Pinned Locked Moved Java
csharpc++javadatabasecom
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chdboy
    wrote on last edited by
    #1

    static Connection Connectionstring()
    {
    Connection con = null;
    try {
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    } catch (ClassNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String conUrl = "jdbc:sqlserver://localhost;databaseName=paytest;user=PC;password=;integratedSecurity=true";

    	try 
    	{
    			 con = DriverManager.getConnection(conUrl); 				 
    			 JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING\_MESSAGE);
    			 
    			 }
    	catch (SQLException e)
    	{
    		
    		e.printStackTrace();
    	}		
    	return con;
    	
    }//connection string method ends 
    

    I have got this error

    Exception in thread "main" java.lang.NoSuchFieldError: address
    at java.net.InetAddress.init(Native Method)
    at java.net.InetAddress.(Unknown Source)
    at java.net.InetSocketAddress.(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SocketFinder.getDefaultSocket(IOBuffer.java:2430)
    at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2094)
    at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at SpringSample.Connectionstring(SpringSample.java:290)
    at SpringSample.main(SpringSample.java:120)

    How to connect ?

    T 1 Reply Last reply
    0
    • C chdboy

      static Connection Connectionstring()
      {
      Connection con = null;
      try {
      Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
      } catch (ClassNotFoundException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      }
      String conUrl = "jdbc:sqlserver://localhost;databaseName=paytest;user=PC;password=;integratedSecurity=true";

      	try 
      	{
      			 con = DriverManager.getConnection(conUrl); 				 
      			 JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING\_MESSAGE);
      			 
      			 }
      	catch (SQLException e)
      	{
      		
      		e.printStackTrace();
      	}		
      	return con;
      	
      }//connection string method ends 
      

      I have got this error

      Exception in thread "main" java.lang.NoSuchFieldError: address
      at java.net.InetAddress.init(Native Method)
      at java.net.InetAddress.(Unknown Source)
      at java.net.InetSocketAddress.(Unknown Source)
      at com.microsoft.sqlserver.jdbc.SocketFinder.getDefaultSocket(IOBuffer.java:2430)
      at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2094)
      at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
      at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
      at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at SpringSample.Connectionstring(SpringSample.java:290)
      at SpringSample.main(SpringSample.java:120)

      How to connect ?

      T Offline
      T Offline
      thanh_bkhn
      wrote on last edited by
      #2

      You should check the port where sqlserver is installed and specify the port number. Ex jdbc:sqlserver://localhost:1433;databaseName=paytest;user=PC;password=;integratedSecurity=true

      C 1 Reply Last reply
      0
      • T thanh_bkhn

        You should check the port where sqlserver is installed and specify the port number. Ex jdbc:sqlserver://localhost:1433;databaseName=paytest;user=PC;password=;integratedSecurity=true

        C Offline
        C Offline
        chdboy
        wrote on last edited by
        #3

        If I use this code in a different class(Just the connection code)it works fine with

        jdbc:sqlserver://localhost:1433;databaseName=paytest;user=PC;password=;integratedSecurity=true

        this code but when I'm using it with GUI it does not work ?I hope connection string is not related to GUI I'm using ?

        L 1 Reply Last reply
        0
        • C chdboy

          If I use this code in a different class(Just the connection code)it works fine with

          jdbc:sqlserver://localhost:1433;databaseName=paytest;user=PC;password=;integratedSecurity=true

          this code but when I'm using it with GUI it does not work ?I hope connection string is not related to GUI I'm using ?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          chdboy wrote:

          I hope connection string is not related to GUI I'm using ?

          How do you think it could be? It is much more likely that you have made a mistake in one application that is not in the other one. Try stepping through the code with your debugger to trace the exact sequence of events when you try to connect.

          Use the best guess

          C 1 Reply Last reply
          0
          • L Lost User

            chdboy wrote:

            I hope connection string is not related to GUI I'm using ?

            How do you think it could be? It is much more likely that you have made a mistake in one application that is not in the other one. Try stepping through the code with your debugger to trace the exact sequence of events when you try to connect.

            Use the best guess

            C Offline
            C Offline
            chdboy
            wrote on last edited by
            #5

            import java.sql.Connection;
            import java.sql.DriverManager;
            import java.sql.ResultSet;
            import java.sql.SQLException;
            import java.sql.Statement;
            import javax.swing.JOptionPane;

            public abstract class ConnectionDB
            {

            /\*\*
             \* @param args
             \*/
            public static void main(String\[\] args)
            {	
            	
            	try {						 
            		insertRecordIntoDbUserTable();
            		selectfromdb();
            	} 
            	catch (SQLException e)
            	{
            
            		System.out.println(e.getMessage());
            
            	}
            

            }

            private static void insertRecordIntoDbUserTable() throws SQLException
            {
            	Connection con = null;
            	Statement statement = null;
            	String insertTableSQL = "INSERT INTO LoginDetails" + "(Username, Password) " + "VALUES" + "('user1','user1')";
            	try 
            	{
            		con = DBConnectionstring();
            		statement = con.createStatement(); 
            		System.out.println(insertTableSQL);			
            		statement.executeUpdate(insertTableSQL);			
            		
            		JOptionPane.showMessageDialog(null,"Your Data has been Inserted","Data Inserted",JOptionPane.WARNING\_MESSAGE);
            	} 
            	catch (SQLException e)
            	{
            
            		System.out.println(e.getMessage()); 
            	} 
            	finally 
            	{
            
            		if (statement != null)
            		{
            			statement.close();
            			
            			JOptionPane.showMessageDialog(null,"Statement is closed","Statement",JOptionPane.WARNING\_MESSAGE);
            		}
            
            		if (con != null)
            		{
            			con.close();
            			
            			JOptionPane.showMessageDialog(null,"Connection is closed!","Connection",JOptionPane.WARNING\_MESSAGE);
            		}
            
            	}
            	
            	
            }
            
            private static void selectfromdb() throws SQLException
            {		
                Statement stmt = DBConnectionstring().createStatement();
            	ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails");
            	while (rs.next())
            	{
            		  String lastName = rs.getString("Username");
            		  String Pass = rs.getString("Password");
            		  System.out.println(lastName + "" + Pass + "\\n");
            		 
            		}		
            }
            
             static Connection DBConnectionstring()
            {		
            	Connection con = null;
            	String conUrl = "jdbc:sqlserver://localhost:1433; databaseName=paytest; user=PC; password=; integratedSecurity=true;";
            	
            	try 
            	{
            			 con = DriverManager.getConnection(conUrl);
            			
            			 JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING\_MESSAGE);
            			 
            			 }
            	catch (SQLException e)
            	{
            		
            		e.printStackTrace();
            	}		
            	return con;
            	
            }
            
            
            }
            

            The only difference is that this is an Abstract class. And this is the code I'm using now with copy for connection string to the above code

            L 1 Reply Last reply
            0
            • C chdboy

              import java.sql.Connection;
              import java.sql.DriverManager;
              import java.sql.ResultSet;
              import java.sql.SQLException;
              import java.sql.Statement;
              import javax.swing.JOptionPane;

              public abstract class ConnectionDB
              {

              /\*\*
               \* @param args
               \*/
              public static void main(String\[\] args)
              {	
              	
              	try {						 
              		insertRecordIntoDbUserTable();
              		selectfromdb();
              	} 
              	catch (SQLException e)
              	{
              
              		System.out.println(e.getMessage());
              
              	}
              

              }

              private static void insertRecordIntoDbUserTable() throws SQLException
              {
              	Connection con = null;
              	Statement statement = null;
              	String insertTableSQL = "INSERT INTO LoginDetails" + "(Username, Password) " + "VALUES" + "('user1','user1')";
              	try 
              	{
              		con = DBConnectionstring();
              		statement = con.createStatement(); 
              		System.out.println(insertTableSQL);			
              		statement.executeUpdate(insertTableSQL);			
              		
              		JOptionPane.showMessageDialog(null,"Your Data has been Inserted","Data Inserted",JOptionPane.WARNING\_MESSAGE);
              	} 
              	catch (SQLException e)
              	{
              
              		System.out.println(e.getMessage()); 
              	} 
              	finally 
              	{
              
              		if (statement != null)
              		{
              			statement.close();
              			
              			JOptionPane.showMessageDialog(null,"Statement is closed","Statement",JOptionPane.WARNING\_MESSAGE);
              		}
              
              		if (con != null)
              		{
              			con.close();
              			
              			JOptionPane.showMessageDialog(null,"Connection is closed!","Connection",JOptionPane.WARNING\_MESSAGE);
              		}
              
              	}
              	
              	
              }
              
              private static void selectfromdb() throws SQLException
              {		
                  Statement stmt = DBConnectionstring().createStatement();
              	ResultSet rs = stmt.executeQuery("SELECT Username,Password FROM LoginDetails");
              	while (rs.next())
              	{
              		  String lastName = rs.getString("Username");
              		  String Pass = rs.getString("Password");
              		  System.out.println(lastName + "" + Pass + "\\n");
              		 
              		}		
              }
              
               static Connection DBConnectionstring()
              {		
              	Connection con = null;
              	String conUrl = "jdbc:sqlserver://localhost:1433; databaseName=paytest; user=PC; password=; integratedSecurity=true;";
              	
              	try 
              	{
              			 con = DriverManager.getConnection(conUrl);
              			
              			 JOptionPane.showMessageDialog(null,"Connection is open!","Connection",JOptionPane.WARNING\_MESSAGE);
              			 
              			 }
              	catch (SQLException e)
              	{
              		
              		e.printStackTrace();
              	}		
              	return con;
              	
              }
              
              
              }
              

              The only difference is that this is an Abstract class. And this is the code I'm using now with copy for connection string to the above code

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              And what happens when you debug it?

              Use the best guess

              C 1 Reply Last reply
              0
              • L Lost User

                And what happens when you debug it?

                Use the best guess

                C Offline
                C Offline
                chdboy
                wrote on last edited by
                #7

                Exception in thread "main" java.lang.NoSuchFieldError: address
                at java.net.InetAddress.init(Native Method)
                at java.net.InetAddress.(Unknown Source)
                at java.net.InetSocketAddress.(Unknown Source)
                at com.microsoft.sqlserver.jdbc.SocketFinder.getDefaultSocket(IOBuffer.java:2430)
                at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2094)
                at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
                at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
                at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
                at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
                at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
                at java.sql.DriverManager.getConnection(Unknown Source)
                at java.sql.DriverManager.getConnection(Unknown Source)
                at SpringSample.Connectionstring(SpringSample.java:130)
                at SpringSample.main(SpringSample.java:101)

                this error happens As above code is being run.

                L 1 Reply Last reply
                0
                • C chdboy

                  Exception in thread "main" java.lang.NoSuchFieldError: address
                  at java.net.InetAddress.init(Native Method)
                  at java.net.InetAddress.(Unknown Source)
                  at java.net.InetSocketAddress.(Unknown Source)
                  at com.microsoft.sqlserver.jdbc.SocketFinder.getDefaultSocket(IOBuffer.java:2430)
                  at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2094)
                  at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
                  at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
                  at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
                  at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
                  at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
                  at java.sql.DriverManager.getConnection(Unknown Source)
                  at java.sql.DriverManager.getConnection(Unknown Source)
                  at SpringSample.Connectionstring(SpringSample.java:130)
                  at SpringSample.main(SpringSample.java:101)

                  this error happens As above code is being run.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  See http://docs.oracle.com/javase/7/docs/api/java/lang/NoSuchFieldError.html[^]. Also, looking at the top four messages above, I am tempted to suspect that the version of the Microsoft library you are using is either out of date or has been corrupted somehow. Check the documentation to see if it is the latest version.

                  Use the best guess

                  C 1 Reply Last reply
                  0
                  • L Lost User

                    See http://docs.oracle.com/javase/7/docs/api/java/lang/NoSuchFieldError.html[^]. Also, looking at the top four messages above, I am tempted to suspect that the version of the Microsoft library you are using is either out of date or has been corrupted somehow. Check the documentation to see if it is the latest version.

                    Use the best guess

                    C Offline
                    C Offline
                    chdboy
                    wrote on last edited by
                    #9

                    I'm using sqljdbc4.jar if you are pointing towards it.I'm not sure how to check if it is latest version?

                    L 1 Reply Last reply
                    0
                    • C chdboy

                      I'm using sqljdbc4.jar if you are pointing towards it.I'm not sure how to check if it is latest version?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Sorry, nor do I. I would guess you go to wherever the library comes from and ask there.

                      Use the best guess

                      C 1 Reply Last reply
                      0
                      • L Lost User

                        Sorry, nor do I. I would guess you go to wherever the library comes from and ask there.

                        Use the best guess

                        C Offline
                        C Offline
                        chdboy
                        wrote on last edited by
                        #11

                        ok thanks a lot :)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups