先学Oracle还是Java,事前学习过的java和Oracle笔记没删除的都带过来
一個client
import java.net.*;
import java.io.*;
public class client {
public static void main(String[] args)throws Exception
{
Socket ss=new Socket("127.0.0.1",6666);
OutputStream os=ss.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeUTF("33333333333333333");
dos.flush();
dos.close();
ss.close();
} }
server
ServerSocket ss=new ServerSocket(6666);
while(true){
Socket s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
System.out.println(dis.readUTF());
dis.close();
s.close();
Socket ss=new Socket("127.0.0.1",6666);
OutputStream os=ss.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
dos.writeUTF("33333333333333333");
dos.flush();
dos.close();
ss.close();
==================================================================================
java連接存儲過程
首先 ?建一張表
book表
create table book (bookId number,bookName varchar2(50) ,publicshHouse varchar2(50));
desc book; ?查看表結構--下面那個in可以不寫 是默認的in,表示輸入參數還有 out表示輸出
create or replace procedu sp_pro7(spBookId in number,sp in bookName,
sppublishHouse in varchar2) is
begin
insert into book values(spBookId,spbookName,sppublishHouse);
end;
在java中調用
--###################################################################
package com.sp;
import java.sql.*
//--調用一個沒有返回值的過程
public class Test1 {
public static void main(String[] args) {
try {//--加載驅動
Class.forName("oracle.jdbc.driver.OracleDriver"); ? ? --下面的MYORA1從服務里面的OracleServerMYORA1得到下面的127.0可以寫成localhost,1521是端口
Connection ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:MYORA1","scott","m123");
//--創建CallableStatement
CallableStatement cs=.ct.prepareCall("{call sp_pro7(?,?,?)}");//--幾個參數寫幾個?
//給?付給值
cs.setInt(1,10);
cs.setString(2,"笑傲江湖");
cs.setString(3,"人民出版社");
cs.execute();
} catch (Exception e) {
e.printStackTrace();
}
finally{
//關閉各個資源
}
}
}
--//執行錯誤 java.lang.ClassNotFoundException錯誤
驅動沒有找到 沒有引入jar包
在項目右鍵 最后一個 ?java Build Path +Add External JARS
選擇classes12.jar
然后okokok;
然后 select * from book;就有記錄了
--##############################################################
編寫一個 有輸入輸出的存儲過程
create ?or replace procedure sp_pro8
(spno in number,spName out varchar2) is
begin
select ename into spName from emp where empno=spno;
end;
Class.forName("oracle.jdbc.driver.OracleDriver"); ? ? --下面的MYORA1從服務里面的OracleServerMYORA1得到下面的127.0可以寫成localhost,1521是端口
Connection ct=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:MYORA1","scott","m123");
//--看看如何調用返回值的過程
CallablesStatement cs=ct.prepareCall("{call sp_pro8(?,?)}");//--一個輸入一個輸出
cs.setInt(1,7788);
//第二個注冊一個值;
cs.registerOutParameter(2,oracle.jdbc.OracleTypes.VARCHAR);
cs.execute();
//取出返回值
String name=cs.getString(2);
System.out.println("77888name"+name);//--上面也有一個7788
==================================================================================
MYSQL
package mybag;
import java.sql.*;
public class ConnectionManager {
private static final String DRIVER="com.mysql.jdbc.Driver";
private static final String URL="jdbc:mysql://localhost:3306/aa?useUnicode=true&characterEncoding=utf8";
private static final String USERNAME= "root";
private static final String PASSWORD= "wang";
//conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8", "root", "de");
public static Connection getConn(){
Connection conn=null;
try{
Class.forName(DRIVER);
conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
//System.out.println("數據庫連接成功!");
} catch(Exception e){
e.printStackTrace();
}
return conn;
}
public static void closeAll(Connection conn,PreparedStatement ps,ResultSet rs){
try{
if(ps!=null){
ps.close();
ps=null;
}
if(rs!=null){
rs.close();
rs=null;
}
if(conn!=null){
conn.close();
conn=null;
}
} catch(SQLException e){
e.printStackTrace();
}
}
public static void main(String [] args){
Connection conn=null;
conn=ConnectionManager.getConn();
try{
Statement a=conn.createStatement();
ResultSet rs=a.executeQuery("select * from de");
while(rs.next())
{
System.out.println(rs.getInt("id"));
System.out.println(rs.getString("name"));
}
}
catch(Exception e){}
}
}
===================================================================================
注冊驅動
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
System.setProperty("jdbc.drivers","com.mysql.jdbc.Driver:你還可以加上更多的驅動");
建立連接
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/dd","root","wang");
創建語句
Statement st=conn.createStatement();
執行語句
ResultSet re=st.executeQuery("select * from emp");
處理結果
while(rs.next())
{
System.out.println(rs.getObject(1)+"\t"+rs.getString("name")+"\t"+rs.getInt(3));
}
rs.close();
st.close();
conn.close();
st.executeUpdate(insert into user(name,birthday,money) values('wangwu','1988-01-01',100));
上面這句會有一個返回直,你可以 int i=接受 ?i是改變了多少就是多少
刪除的操作和上面的更新是一樣
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/dd","root","wang");
創建語句
PreparedStatement ps=conn.PrepareStatement("select * from emp");
執行語句
ResultSet ps=st.executeQuery();
處理結果
while(rs.next())
{
System.out.println(rs.getObject(1)+"\t"+rs.getString("name")+"\t"+rs.getInt(3));
}
rs.close();
st.close();
conn.close();
==============================================================
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/dd","root","wang");
創建語句
String sql="select id,name from user where name=?";
PreparedStatement ps=conn.PrepareStatement(sql);
ps.setString(1,"wangwu");
執行語句
ResultSet ps=st.executeQuery();
處理結果
while(rs.next())
{
System.out.println(rs.getObject(1)+"\t"+rs.getString("name")+"\t"+rs.getInt(3));
}
rs.close();
st.close();
conn.close();
==========================================================
long start=System.currentTimeMillis();
一些代碼
long end=System.currentTimeMillis();
System.out.println("read"+(end-start));
==================================================================================
java連接存儲過程
有返回值的存儲過程(列表【結果集】)
案例:編寫一個過程,輸入部門號,返回該部門所有的雇員信息。對該題分析如下:
由于oracle存儲過程沒有返回值,他的所有返回值都是通過out參數來替代的,列表同樣也不例外,但是由于是集合,所以不能用一般的參數,必須用藥用pagkage,所以分兩部分
一:建立一個包 ,在包中定義一個類型test_cursor
create or replace package tespackage ?as
TYPE test_cursor is ref cursor;--名字叫做test的游標,標準時ref cursor
end tespackage;--同上面
二,創建過程
create or replace procedure sp_pro9(spNo in number,p_cursor out tespackage.test_cursor)
is
begin
open p_cursor for select * from emp where deptno=spNo;
end;
三 在java調用
創建CallableStatement
CallableStatement cs=ct.prepareCall("{call sp_pro9(?,?)}");
cs.setInt(1,10);
//注冊
cs.registerOutParameter(2,oracle.jdbc.OracleTypes.CURSOR);
cs.execute();
//關鍵
//得到結果集
ResultSet rs=(ResultSet)cs.getObject(2);
while(rs.next()){
System.out.println(rs.getInt(1)+""+rs.getString(2));
}
===================================================================================
過程
編寫一個過程,可以輸入雇員名字,如果雇員的工資低于 2000 就給雇員工資增加白分之十
create or replace procedure sp_pro6(spName varchar2)--這里不能寫成varchar2(10)
--定義
v_sal emp.sal%type
begin
select sal into v_sal from emp where ename=spName;
--執行
--判斷
if v_sal<2000 then
update emp set=set*1.1 where ename=spName
end if;
end;
select * from emp;
exec/call sp_pro6('SCOTT');
select * from emp; ?--發現 SCOTT工資變化了
還有 二重條件分支 ?if-then-else
--編寫一個過程,可以輸入雇員名字,如果雇員的補助不是0就在原來的基礎上增加100,如果為0,就把補助設置為200
create or replace procedure sp_pro6(spName varchar2)--這里不能寫成varchar2(10)
--定義
v_comm emp.xomm%type
begin
select comm into v_comm from emp where ename=spName;
--執行
--判斷
if v_comm<>0 then ? --不能!=
update emp set comm=comm+100 where ename=spName;
else
update emp set comm=comm+200 where ename=spName;
end if;
end;
--@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
編寫一個過程,可以輸入一個雇員的編號,如果這個雇員的職位是 ??就給他的工資增加1000如果這個
職員是??就給他的工資增加500,其他的工資增加200
create or replace procedure sp_proc6(spNo number) is
v_job emo.job%type;
begin
select job int v_job from emp where empno=spNo;
if v_job='PREDIENT' then
update ?emp set sal=sal+10000 where empno=spNo;
elsif v_job='MAX' then --記住著這個寫法elsif
update emp set sal=sal+500 where empno=spNo;
else
update emp set sal=sal+200 where empno=spNo;
end if; ?--有空格
end;
--執行
exec/call sp_proc6(7839);
--%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
--編寫一個過程,輸入用戶名字,并且循環填加10個用戶 到users表 ?用戶編號從1開始
create or replace procedure sp_proc6(spName varchar2) is
v_num number:=1;
begin
loop
insert into users1 values(v_name,spName);
exit when v_num=10
v_num:=v_num+1;
end loop;
end;
--發生錯誤,沒有表
create tables users1(userNO number,userName varchar2(40));
--現在好了
desc user1--看看表的結構 不是內容
執行 exec sp_proc6('你好');
--##############################################################################
編寫一個過程 ?,可以輸入擁護名字 并且 填加10個擁護到users表
擁護編號從11開始
create or replace procedure sp_proc6(spName varchar2) is
v_num number:=11;--改這里
begin
while v_num<=200 loop
insert into users1 values(v_name,spName);
v_num:=v_num+1;
end loop;
end;
=============================================================================
一個 簡單的goto
declare
i int :=1; ? --定義一個int 的變量1
begin
loop
dbms_out.put_line('輸出'||i); --輸出一句話
if i=10 then
goto end_loop; --跳轉到下面的 <>
end if;
i:=i+1;
end loop;
<>
dbms_out.put_line('循環結束');
end;
如果沒有執行是 因為這個開關沒有打開
set serveroutput on; 現在執行就有效果
--#####################
if 條件 then
執行;
else
null;
================================================================================
mysql ?java
Class.forname("com.mysql.jdbc.Driver");
Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/aa","root","wang");
Statement st=conn.createStatement();
ResultSet re=st.executeQuery("select * from de");
while(re.next()){
System.out.println(re.getInt("id"));
System.out.println(re.getString(2));
}
=================================================================================
約束 五種: not null 不能為空
unique ?唯一約束
primary 主鍵=不能重復且不能為空
foreign key 外鍵
check
單列索引
create index nameIndex on customer(name);
符合索引
====================================================================================
package cm.a;
public class a {
static{
new c("555");
new c("444").ju();
}
public static void main(String[] args) {
System.out.println(new b(){
public intprint(){
System.out.println("hhhhhhhhhh");
return 4;
};public void b(){};
}.print());
System.out.println("++++++++++++++");
}
}
interface b{
void b();
public int print() ;
}
class c implements b{
String a;
public c(){};
public c(String a){
this.a=a;
}
public String ju()
{
return a;
}
public int print(){
System.out.println("ssssssssssssssssss");
return 777;
};public void b(){};
}
總結
以上是生活随笔為你收集整理的先学Oracle还是Java,事前学习过的java和Oracle笔记没删除的都带过来的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ajax用证书调用,跨域的jQuery
- 下一篇: python调用系统命令_linux里面