java连接Oracle数据库
基本步骤我知道了,但是各个步骤中的参数都是什么意思啊?比如url中的数据?望给出详细的注释~~...
基本步骤我知道了,但是各个步骤中的参数都是什么意思啊?比如url中的数据?望给出详细的注释~~
展开
展开全部
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver");//加入oracle的驱动,“”里面是驱动的路径
String url = "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL";// 数据库连接,oracle代表链接的是oracle数据库;thin:@MyDbComputerNameOrIP代表的是数据库所在的IP地址(可以保留thin:);1521代表链接数据库的端口号;ORCL代表的是数据库名称
String UserName = "root";// 数据库用户登陆名 ( 也有说是 schema 名字的 )
String Password = "2006";// 密码
conn = DriverManager.getConnection(url, UserName, Password);
Class.forName("oracle.jdbc.driver.OracleDriver");//加入oracle的驱动,“”里面是驱动的路径
String url = "jdbc:oracle:thin:@MyDbComputerNameOrIP:1521:ORCL";// 数据库连接,oracle代表链接的是oracle数据库;thin:@MyDbComputerNameOrIP代表的是数据库所在的IP地址(可以保留thin:);1521代表链接数据库的端口号;ORCL代表的是数据库名称
String UserName = "root";// 数据库用户登陆名 ( 也有说是 schema 名字的 )
String Password = "2006";// 密码
conn = DriverManager.getConnection(url, UserName, Password);
展开全部
8.Oracle8/8i/9i数据库(thin模式)
//import java.sql.*;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmtNew=conn.createStatement();
1.提取单条记录
//import java.sql.*;
Connection con=null;
Statement stmt=null;
ResultSet %%6=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:"+%%1;
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement();
stmt.executeUpdate(%%4);
%%6=stmt.executeQuery(%%5);
%%7
}catch(Exception e){
e.printStackTrace();
}finally{
try {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
2.单值比较
//import java.sql.*;
ResultSet rs;
String strurl="jdbc:odbc:%%1";
Connection con=NULL;
Statement stmt=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(%%2);
rs.next();
int %%3=rs.getInt("%%3");
if(%%3==%%4)
{
%%5
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(rs!=null)
{
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3.显示表格
//import java.awt.*;
//import javax.swing.*;
//import java.sql.*;
//import javax.swing.table.*;
String[] colHeads=%%4;
Connection con=null;
Statement stmt=null;
ResultSet %%6=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:"+%%1;
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement();
%%6=stmt.executeQuery("SELECT count(*) as au_count from "+%%5);
%%6.next();
int iCount=rs.getInt("au_count");
Object[][] data=new Object[iCount][];
int i=0;
%%6=stmt.executeQuery("SELECT * from "+%%5);
while(%%6.next()){
data[i]=new Object[iCount];
data[i][0]=%%6.getString("au_fname");
data[i][1]=%%6.getString("Phone");
data[i][2]=%%6.getString("City");
i++;
}
JTable table=new JTable(data,colHeads);
JScrollPane jsp=new JScrollPane(table);
getContentPane().add(jsp);
}catch(Exception e){
e.printStackTrace();
}
finally{
if(con!=null){
try {
%%6.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
7.执行命令
//import java.sql.*;
Connection conn;
PreparedStatement pst=null;
try {
conn=DriverManager.getConnection(url);
pst=conn.prepareStatement("Insert Into grade(%%1) Values (?)");
pst.setInt(1,%%2);
//pst.setInt(2,%%2);
pst.addBatch();
pst.executeBatch();
} catch (SQLException e){
e.printStackTrace();
}
finally{
pst.close();
conn.close();
}
//import java.sql.*;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@localhost:1521:orcl"; //orcl为数据库的SID
String user="test";
String password="test";
Connection conn= DriverManager.getConnection(url,user,password);
Statement stmtNew=conn.createStatement();
1.提取单条记录
//import java.sql.*;
Connection con=null;
Statement stmt=null;
ResultSet %%6=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:"+%%1;
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement();
stmt.executeUpdate(%%4);
%%6=stmt.executeQuery(%%5);
%%7
}catch(Exception e){
e.printStackTrace();
}finally{
try {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
2.单值比较
//import java.sql.*;
ResultSet rs;
String strurl="jdbc:odbc:%%1";
Connection con=NULL;
Statement stmt=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(%%2);
rs.next();
int %%3=rs.getInt("%%3");
if(%%3==%%4)
{
%%5
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
try {
if(rs!=null)
{
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
3.显示表格
//import java.awt.*;
//import javax.swing.*;
//import java.sql.*;
//import javax.swing.table.*;
String[] colHeads=%%4;
Connection con=null;
Statement stmt=null;
ResultSet %%6=null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url="jdbc:odbc:"+%%1;
con=DriverManager.getConnection(url,%%2,%%3);
stmt=con.createStatement();
%%6=stmt.executeQuery("SELECT count(*) as au_count from "+%%5);
%%6.next();
int iCount=rs.getInt("au_count");
Object[][] data=new Object[iCount][];
int i=0;
%%6=stmt.executeQuery("SELECT * from "+%%5);
while(%%6.next()){
data[i]=new Object[iCount];
data[i][0]=%%6.getString("au_fname");
data[i][1]=%%6.getString("Phone");
data[i][2]=%%6.getString("City");
i++;
}
JTable table=new JTable(data,colHeads);
JScrollPane jsp=new JScrollPane(table);
getContentPane().add(jsp);
}catch(Exception e){
e.printStackTrace();
}
finally{
if(con!=null){
try {
%%6.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
7.执行命令
//import java.sql.*;
Connection conn;
PreparedStatement pst=null;
try {
conn=DriverManager.getConnection(url);
pst=conn.prepareStatement("Insert Into grade(%%1) Values (?)");
pst.setInt(1,%%2);
//pst.setInt(2,%%2);
pst.addBatch();
pst.executeBatch();
} catch (SQLException e){
e.printStackTrace();
}
finally{
pst.close();
conn.close();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Oracle数据库连接URL格式:
jdbc:oracle:thin:@<host>:<port1521>:<全局数据库名>
thin是纯java实现tcp/ip的c/s通讯
<host>IP地址
<port1521>端口号,默认1521
<全局数据库名>一般是orcl
jdbc:oracle:thin:@<host>:<port1521>:<全局数据库名>
thin是纯java实现tcp/ip的c/s通讯
<host>IP地址
<port1521>端口号,默认1521
<全局数据库名>一般是orcl
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询