如何连接java和Oracle? [英] How to connect java and Oracle?
问题描述
大家好,
我正在使用netbeans开发Java项目。我完成了我想要连接到数据库的所有部分(GUI和java代码)在数据库中保留一些信息,例如我想通过文本框将新用户添加到数据库。但是我无法连接它。
这是我的代码伙伴,请帮助我,这个项目对我来说非常重要。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
try {
Connection con = null;
Class.forName( oracle.jdbc.driver.OracleDriver);
con = DriverManager.getConnection(
jdbc:oracle:thin:@localhost:1521: orcl,
plsql_staj,
plsql_staj1);
Statement s = con.createStatement();
s.execute( INSERT INTO E_USER VALUES(((u_name,u_pass,u_role)('+txtAddUserName.getText()+ ',' + txtAddUserPassWord.getText()+ ',' + cmbAddUserRole.getSelectedItem()。toString()+ '));
s.close();
con.close();
}
catch (ClassNotFoundException | SQLException e){}
}
netbeans没有错误。但是按钮不起作用。我正在添加
import Oracle.jdbc.pool.OracleDataSource;
这个导入但这次我拿包oracle.jdbc.pool不存在错误。
请拜托。这是我的毕业典礼roject。如果你无法解决,请宣布。
谢谢。
你注册了司机吗?
< pre lang =c#> DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver());
尝试按照以下步骤操作(检查是否遗漏了任何内容):
1)在您的java文件中导入以下软件包:***********
import java.sql。*;
import oracle.jdbc.driver。*;
import oracle.sql。*;
2)加载并注册JDBC驱动程序:* **********
DriverManager.registerDriver( new oracle.jdbc.driver .OracleDriver());
或者你可以使用Class.forName( oracle.jdbc.driver.OracleDriver);
3)连接数据库:*** ********
a)如果您使用的是oracle oci驱动程序,则必须使用:
连接conn = DriverManager .getConnection( jdbc:oracle:oci8:@ oracle.world, root, root);
其中oracle.world是TNSNAMES条目,root是用户名和密码。
b)如果你正在使用oracl瘦驱动程序,你必须使用:
连接conn = DriverManager.getConnection( jdbc:oracle:thin:@localhost:3306:mydatabase, root, root);
其中localhost是主机,3306是端口,mydatabase是数据库,root是用户名和密码。
4)查询数据库:******** **
a)创建声明:
语句st = conn.createStatement();
b)编写查询并执行查询:
ResultSet rs = st.executeQuery( SELECT * from student);
5)关闭语句,结果集和连接:**** ******
rs.close();
st.close();
conn.close();
Aditaya我不知道该怎么感谢你。那是最好的。谢谢,非常感谢你。
我又遇到了一些问题。我怎么能理解我的司机是oci还是瘦?我试过两个,但我正在采取java.sql.SQLException:Io异常:网络适配器无法建立连接错误。
尝试 {
DriverManager.registerDriver( new oracle.jdbc.driver.OracleDriver() );
连接conn = DriverManager.getConnection( jdbc:oracle:thin:@localhost:3306:O10G, plsql_staj, plsql_staj1);
语句st = conn.createStatement();
ResultSet rs = st.executeQuery( SELECT * from E_USER);
rs.close();
st.close();
conn.close();
} catch (SQLException ex){
Logger.getLogger(AddNewUser。 class .getName())。log(Level.SEVERE,null,ex);
}
当我使用其他选项时
连接conn = DriverManager.getConnection( jdbc:oracle:oci8:@ oracle.world, plsql_staj, plsql_staj1);
我正在接受另一个错误:
线程中的异常AWT-EventQueue-0java.lang.UnsatisfiedLinkError:java.library.path中没有ocijdbc9
这是我的PLSQL连接屏幕。
http://www.imagetoo.com/images/untitlzhz.jpg [ ^ ]
Hello everyone,
I am developing Java project with netbeans. I completed all the parts(GUI and java code) I want to connect to database keep some information in database for example I want to add new user via textbox to database. However I couldnt Connect it.
Here is my code guys please help me that project is so important for me.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
Connection con=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(
"jdbc:oracle:thin:@localhost:1521:orcl",
"plsql_staj",
"plsql_staj1");
Statement s=con.createStatement();
s.execute("INSERT INTO E_USER VALUES(((u_name,u_pass,u_role)('"+txtAddUserName.getText()+"','"+txtAddUserPassWord.getText()+"','"+cmbAddUserRole.getSelectedItem().toString()+"')");
s.close();
con.close();
}
catch(ClassNotFoundException | SQLException e){}
}
There is no error on netbeans. However button does not work. I am adding
import Oracle.jdbc.pool.OracleDataSource;
this import but this time I am taking "package oracle.jdbc.pool does not exist" error.
Please please please. That is my graduation project. If you cannot solve please announce it.
Thanks.
Have you registered the driver?
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
Try Following these steps (Check if you missed anything):
1) Import the following packages in your java file:***********
import java.sql.*; import oracle.jdbc.driver.*; import oracle.sql.*;
2) Load and Register the JDBC driver:***********
DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
or you can useClass.forName("oracle.jdbc.driver.OracleDriver");
3) Connect to database:***********
a) If you are using oracle oci driver,you have to use:
Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@oracle.world", "root", "root");
where oracle.world is the TNSNAMES entry and root is the username and password.
b) If you are using oracle thin driver,you have to use:
Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:3306:mydatabase","root", "root");
where localhost is the host,3306 is the port, mydatabase is the database and root is the username and password.
4) Querying the database:**********
a)create statement:
Statement st = conn.createStatement();
b)write query and execute the query:
ResultSet rs = st.executeQuery("SELECT * from student");
5) Close the statement,resultset and connection:**********
rs.close(); st.close(); conn.close();
Aditaya I don't know how to thank you. That was the best. Thank you thank you so much.
I have some problems again. How could I understan my driver is oci or thin ? I tried both but I am taking "java.sql.SQLException: Io exception: The Network Adapter could not establish the connection" error.
try { DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver()); Connection conn = DriverManager.getConnection ("jdbc:oracle:thin:@localhost:3306:O10G","plsql_staj", "plsql_staj1"); Statement st = conn.createStatement(); ResultSet rs = st.executeQuery("SELECT * from E_USER"); rs.close(); st.close(); conn.close(); } catch (SQLException ex) { Logger.getLogger(AddNewUser.class.getName()).log(Level.SEVERE, null, ex); }
When I used the other option which is
Connection conn = DriverManager.getConnection("jdbc:oracle:oci8:@oracle.world", "plsql_staj", "plsql_staj1");
I am taking another error which is:
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no ocijdbc9 in java.library.path
Here is my PLSQL connection screen.
http://www.imagetoo.com/images/untitlzhz.jpg[^]
这篇关于如何连接java和Oracle?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!