DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2006
    Posts
    16

    Error getting Connection via Java JDBC

    Hi,
    I wrote a Table in Oracle to create a table in Oracle.

    Code:
    import javax.swing.JOptionPane;
    import java.sql.*;
    public class JDBCProgram
    {
    	public static void main(String args[])
    	{
    		JOptionPane.showMessageDialog(null,"Welcome to JDBC Demo");
    		int choice = -1;
    		String userid="scott";
    		String password = "tiger";
     
    		do
    		{
    			choice = getChoice();
    			if (choice != 0)
    			{
    				getSelected(choice, userid, password);
    			}
    		}
    		while ( choice !=  0);
    		System.exit(0);
    	}
     
    	public static int getChoice()
    	{
    		String choice;
    		int ch;
    		choice = JOptionPane.showInputDialog(null,
    			"1. Create Coffees Table\n"+
    			"0. Exit\n\n"+
    			"Enter your choice");
    		ch = Integer.parseInt(choice);
    		return ch;
     
    	}
     
    	public static void getSelected(int choice, String userid, String password)
    	{
    		if(choice==1)
    		{
    			createCoffees(userid, password);
    		}
    	}
     
    	// Create Coffees Table
    	public static void createCoffees(String userid, String password)
    	{
    		String url = "jdbc:odbc:bob";		// String url = "jdbc:mySubprotocol:myDataSource"; ?
    											
    		Connection con;
    		String createString;
    		createString = "create table COFFEES " +
    							"(COF_NAME varchar(32), " +
    							"SUP_ID int, " +
    							"PRICE float, " +
    							"SALES int, " +
    							"TOTAL int)";
    		Statement stmt;
     
    		try {
    			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");	//Class.forName("myDriver.ClassName"); ?
     
    		} catch(java.lang.ClassNotFoundException e) {
    			System.err.print("ClassNotFoundException: ");
    			System.err.println(e.getMessage());
    		}
     
    		try {
    			con = DriverManager.getConnection(url,
    									 "userid", "password");
     
    			stmt = con.createStatement();
    	   		stmt.executeUpdate(createString);
     
    			stmt.close();
    			con.close();
     
    		} catch(SQLException ex) {
    			System.err.println("SQLException: " + ex.getMessage());
    		}
    	}
     
    	
     
     
    }//End of class





    When I run this program I am getting the following error.

    SQLException: [Microsoft][ODBC driver for Oracle][Oracle]ORA-12154: TNS:could not resolve service name

    When I cross checked to see if I am able to log in SQL Plus using
    Username: scott
    Password: Tiger
    Host String: bob


    It works fine

  2. #2
    Join Date
    Oct 2005
    Posts
    4

    Solution

    Hi,
    Please cross check ur code while you are creating Connection object.
    con = DriverManager.getConnection(url, "userid", "password");

    Here you are using username and password as "userid" and "password" respectively. Please use String variables userid and password instead.

    try this one.
    con = DriverManager.getConnection(url, userid, password);

    This should work.

    mcaashish

Similar Threads

  1. Replies: 3
    Last Post: 08-24-2006, 10:15 PM
  2. java jdbc connection on w2k, Oracle8i, JDK1.3
    By frest in forum oracle.general
    Replies: 0
    Last Post: 05-01-2001, 07:33 AM
  3. JDBC - Oracle - Servlet connection
    By Mark in forum Java
    Replies: 1
    Last Post: 09-12-2000, 11:51 AM
  4. JSP and JDBC connection
    By Randy in forum Java
    Replies: 2
    Last Post: 08-28-2000, 04:46 PM
  5. Best Java Development environment
    By H. Wilson in forum Java
    Replies: 6
    Last Post: 04-14-2000, 11:25 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links