Problems with connecting Oracle databases using Java

Source: Internet
Author: User

I encountered some problems when I first learned how to connect to the Oracle database using java the day before. I posted a post on the Internet for help, searched for similar problems, and explored with my classmates. Finally, I solved the problem, I can't say it's a perfect solution, but at least I have learned a lot.

I posted the same post in the Java and Oracle communities respectively. The following will reference the original post:

Subject:   Problems with connecting Oracle databases using Java
Author:   Add the blog as a friend (only love child cold)
Level 1:  
Reputation value:   100
Community:   Java j2se/basic class
Problem points:   50
Replies:   17
Posting time:   2007-4-10 20:30:41

I used Java to connect to the Oracle database, but an error occurred. The prompt is:
Exception in thread "Main" Java. Lang. noclassdeffounderror: jdbcconn
My source program is as follows:
Import java. SQL .*;

Public class jdbcconn
{
Private Static string url = "";
Private Static string username = "";
Private Static string Password = "";

Private connection conn ()
{
Try
{
Class. forname ("oracle. JDBC. Driver. oracledriver ");
Connection con = drivermanager. getconnection (URL, username, password );
Return con;
}
Catch (classnotfoundexception CNF)
{
System. Out. println ("driver not find:" + CNF );
Return NULL;
}
Catch (sqlexception sqle)
{
System. Out. println ("can't connection DB:" + sqle );
Return NULL;
}
Catch (exception E)
{
System. Out. println ("failed to load JDBC Driver .");
Return NULL;
}
}

Public void query (connection con, string SQL)
{
Try
{
If (con = NULL) throw new exception ("datebase connection can't use! ");
If (SQL = NULL) throw new exception ("check your parameter: 'SQL '! Don't input null! ");
Statement stmt = con. createstatement ();
Resultset rs1_stmt.exe cutequery (SQL );
Resultsetmetadata rmeta = Rs. getmetadata ();
Int numcolumns = rmeta. getcolumncount ();
While (Rs. Next ())
{
For (INT I = 0; I <numcolumns; I ++)
{
String stemp = Rs. getstring (I + 1 );
System. Out. println (stemp + "");
}
System. Out. println ("");
}
}
Catch (exception E)
{
System. Out. println ("query error:" + E );
}
}

Public void execute (connection con, string SQL)
{
Try
{
If (con = NULL) return;
Statement stmt = con. createstatement ();
Stmt.exe cuteupdate (SQL );
}
Catch (exception E)
{
System. Out. println ("execute error: SQL =" + SQL );
System. Out. println (E );
}
}

Public void demo ()
{
Try
{
Jdbcconn OC = new jdbcconn ();
Connection conn = OC. Conn ();
String SQL = "insert into tbl_user (ID, name, password) values (seq_user.nextval, 'switch', 'haorenpingan ')";
Oc.exe cute (Conn, SQL );
SQL = "select * From tbl_user ";
OC. Query (Conn, SQL );
Conn. Close ();
}
Catch (sqlexception SE)
{
System. Out. println (SE );
}
Catch (exception E)
{
System. Out. println (E );
}
}

Public static void main (string [] Arg)
{
If (Arg. length! = 3)
{
System. Out. println ("Use: Java jdbcconn URL Username Password ");
Return;
}
Jdbcconn OC = new jdbcconn ();
OC. url = Arg [0];
OC. Username = Arg [1];
OC. Password = Arg [2];
OC. Demo ();
}
}

At runtime, I enter:
Java jdbcconn JDBC: oracle: thin: @ 127.0.0.1: 1521: misdb Scott Tiger
After receiving the prompt, I also want to ask, my JDBC driver is under the Oracle10g installer... /orahome_1/jdbc/lib/classes12.zip environment variable classpath is set in the command line window (I don't know why, directly in my computer-> properties-> advanced-> environment variables cannot be set), is this okay?

I am a newbie. I am very grateful to you for your understanding of the environment variable settings!

  Richard 2010 (Love child cold only)() Reputation: 100 add blog as a friend 2007-4-10 20:40:44 Score: 0

I found this example on the Internet:
Import java. SQL .*;
Public class TT
{
Public static void main (string [] ARGs)
{
Try
{
Class. forname ("oracle. JDBC. Driver. oracledriver ");
Connection conn = drivermanager. getconnection ("JDBC: oracle: thin: @ localhost: 1521: misdb ",
"Scott", "Tiger ");
Statement stmt = conn. createstatement ();
Resultset rs1_stmt.exe cutequery ("select * from EMP ");
While (Rs. Next ())
{
// Int I = Rs. getint ("ID ");
// System. Out. println (I );
// System. Out. println (Rs. getstring ("userid "));
System. Out. println (Rs. getstring (2 ));
}
Conn. Close ();
}
Catch (exception ex)
{

}
}
}
The running result is the same as that of my program:
Exception in thread "Main" Java. Lang. noclassdeffounderror: classname
Online, etc. Thank you!

There isZzxiaoma (pony)The Oracle driver package cannot be found.

In the absence of set classpath =... when setting environment variables in/orahome_1/jdbc/lib/classes12.zip, the following prompt appears: Can't connection DB: Java. SQL. sqlexception: Io exception: the network adapter cocould not establish the connection
Query error: Java. Lang. Exception: datebase connection can't use!
Java. Lang. nullpointerexception
After the setting is completed, exception in thread "Main" Java. Lang. noclassdeffounderror: jdbcconn

I think it should be because the environment variables are not set, because % pathclass % is not used before I set them with set; the environment variables of Java classes may be overwritten. Therefore, the system prompts that the driver cannot be found before setting, and prompts that the Java class cannot be found after setting. But how should we set the environment variables.

Exactly oneRichboylf ()He enthusiastically posted his own environment variables: I set the environment variables in this way. You can refer to them.
Classpath:
.; C:/j2sdk1.4.2 _ 02/lib/tools. jar; C:/j2sdk1.4.2 _ 02/lib/dt. jar; C:/j2sdk1.4.2 _ 02/lib/classes12.jar
Java_home:
C:/j2sdk1.4.2 _ 02
Path:
D:/Oracle/ora92/bin; C:/j2sdk1.4.2 _ 02/bin; C:/program files/Oracle/JRE/1.3.1/bin; C: /program files/Oracle/JRE/1.1.8/bin; % SystemRoot %/system32; % SystemRoot %/system32/WBEM; % PATH %; % ant_home %/bin

However, the setting still does not work. During the discussion, he said that he had never input the URL, username, and password in the command line, so he switched to another program to test the connection:

Import java. SQL .*;
Import java. util .*;
Import java. AWT .*;
Public class jdbcfile
{
Public static void main (string [] ARGs)
{

Connection conn;
Try
{
Class. forname ("oracle. JDBC. Driver. oracledriver ");
}
Catch (exception E)
{
System. Out. println ("33333333333333333333333333 ");
}
Try
{Conn = drivermanager. getconnection ("JDBC: oracle: thin: @ 127.0.0.1: 1521: misdb", "system", "123456789 ");
System. Out. println ("11111111111111111 ");
}
Catch (sqlexception E)
{
System. Out. println ("2222222222222222222222222222 ");
}

}
}

The output is 222222222222, indicating that the database cannot be connected, so I deleted the database and created a new one. The output is 1111111111, but my original program still does not work. The prompt is changed: can't connection DB: Java. SQL. sqlexception: no suitable driver
Query error: Java. Lang. Exception: datebase connection can't use!
Java. Lang. nullpointerexception

I have summarized this step and I think there are some reasons why the previous environment variables have not been properly set.

Then a friendZhaochunhui (days without smoke)Reply to me: Can't connection DB: Java. SQL. sqlexception: no suitable driver
Query error: Java. Lang. Exception: datebase connection can't use!
Java. Lang. nullpointerexception
No proper driver
Databases cannot be used.
Null Pointer Error

There may be problems with the creation of your database.

Indeed, the EM configuration error was prompted at the last step when I created it. I never encountered it before, but after reboot computer, my original program can finally work properly. However, the EM still cannot be opened, so the database was deleted and re-built. This time the sample solution was not selected (I do not know whether it is related to this). I chose it when I had a problem with the last creation, after being built, we can test all the programs we designed above, and work normally. Em can also be opened. The problem is that it has no problem, but it has brought me a lot of thinking. For example, if you want to solve a problem, you have a friend.Ahu201 ()Reply: http://forum.java.sun.com/thread.jspa? Threadid = 571464
Http://www.jguru.com/faq/view.jsp? Eid = 455768,
There are solutions for Google queries. I read the two posts he provided, and I was a little tired, and the problems I encountered were all inexplicable. I had no problem doing this before, but this time I could not do it. I think this problem occurs in details. Sometimes, the details are not well done, and it may be different from the previous ones. So when we encounter problems, first, let's think about how we did it before, what is the difference with the present, how we solved it in the past, how we solved it on the Internet, and how others solved it. You can also post and ask, discussing with others is a good way.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.