一个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(){};
}