日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

java 从数据库取值_JAVA操作数据库--从一张表中取值,经过判断,然后插入另一张表中。...

發(fā)布時間:2024/9/3 数据库 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 从数据库取值_JAVA操作数据库--从一张表中取值,经过判断,然后插入另一张表中。... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

JAVA操作數(shù)據(jù)庫-->從一張表中取值,經(jīng)過判斷,然后插入另一張表中。

SQL語句如下:兩張表 weather 和 weather_process.

id均為自動增長。Oracle中采用序列 Sequence

寫的不對的地方,歡迎大家指出。您的建議,也是我的進步。謝謝!

DROP SEQUENCE weather_id;

CREATE SEQUENCE weather_id //創(chuàng)建序列

INCREMENT BY 1?//每次增加1

START WITH 1 //從1開始,即id從1開始 每次增1 如:1,2,3...

NOMAXVALUE???????//不設(shè)置最大值

NOCYCLE;?????????????? //不用循環(huán)

插入值的時候,id 字段寫成weather_id.nextval 便完成了自動增加的功能!

INSERT INTO WEATHER(id, weather) values (weather_id.nextval,'晴');

DROPTABLEWEATHER;

CREATETABLEWEATHER?(

IDINTPRIMARYKEYNOTNULL,

weatherVARCHAR(20)NOTNULL

);

DROPWEATHER_PROCESS;

CREATETABLEWEATHER_PROCESS(

IDINTPRIMARYKEYNOTNULL,

陰intNOTNULLdefault0,

晴intNOTNULLdefault0,

雨intNOTNULLdefault0

);

DROPSEQUENCEweather_id;

CREATESEQUENCEweather_id

INCREMENTBY1

STARTWITH1

NOMAXVALUE

NOCYCLE;

DROPSEQUENCEweather_process_id?;

CREATESEQUENCEweather_process_id

MINVALUE?1

STARTWITH1

INCREMENTBY1;

INSERTINTOWEATHER(id,?weather)values(weather_id.nextval,'晴');

INSERTINTOweather(id,?weather)values(weather_id.nextval,'雨');

INSERTINTOweather(id,?weather)values(weather_id.nextval,'陰');

INSERTINTOweather(id,?weather)values(weather_id.nextval,'雨');

INSERTINTOweather(id,?weather)values(weather_id.nextval,'陰');

INSERTINTOweather(id,?weather)values(weather_id.nextval,'晴');

INSERTINTOweather(id,?weather)values(weather_id.nextval,'雨');

INSERTINTOweather(id,?weather)values(weather_id.nextval,'晴');

下面來說說這次的主題:根據(jù)weather 表中的weather字段 的值(晴,陰,雨),來決定表weather_process 中 陰 晴 雨 的值。

當weather 為 晴時,weather_process 中的值 設(shè)置為:0 1 0 (陰,晴,雨)。

當weather 為 陰時,weather_process 中的值 設(shè)置為:1?0 0 (陰,晴,雨)。

當weather 為 雨時,weather_process 中的值 設(shè)置為:0?0?1 (陰,晴,雨)。

上述采用JAVA 實現(xiàn)。

連接ORACLE 的代碼:

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.SQLException;

/**

*?A?JDBC?test?application?for?Oracle

*

*?@author?Wang

*?@version?1.0.0

*?@since?JDK1.6

*/

publicclassOracleConnect?{

//?定義ORACLE的數(shù)據(jù)庫驅(qū)動程序

publicstaticfinalString?DBDRIVER?="oracle.jdbc.driver.OracleDriver";

//?定義ORACLE數(shù)據(jù)庫的連接地址

publicstaticfinalString?DBURL?="jdbc:oracle:thin:@localhost:1521:ORCL";

//ORACLE數(shù)據(jù)庫的連接用戶名

publicstaticfinalString?DBUSER?="system";

//?ORACLE數(shù)據(jù)庫的連接密碼

publicstaticfinalString?DBPASS?="www.google.com";

publicConnection?getConnection()?{

Connection?conn?=null;//?數(shù)據(jù)庫連接

try{

Class.forName(DBDRIVER)?;

}catch(ClassNotFoundException?e)?{

//?TODO?Auto-generated?catch?block

e.printStackTrace();

}//?加載驅(qū)動程序

System.out.println("加載驅(qū)動成功!");

try{

conn?=?DriverManager.getConnection(DBURL,DBUSER,DBPASS)?;

}catch(SQLException?e)?{

//?TODO?Auto-generated?catch?block

e.printStackTrace();

}

System.out.println("連接成功!");

returnconn;

}

}

處理表的代碼:

importjava.sql.Connection;

importjava.sql.ResultSet;

importjava.sql.SQLException;

importjava.sql.Statement;

publicclassOracleProcess?{

Connection?conn?=null;

Statement?stmt?=null;

publicOracleProcess(){

OracleConnect?oraconn?=newOracleConnect();

conn?=?oraconn.getConnection();

}

publicConnection?getWeather()?{

try{

stmt?=?conn.createStatement();

}catch(SQLException?e)?{

//?TODO?Auto-generated?catch?block

e.printStackTrace();

}

String?sql?="SELECT?weather?FROM?WEATHER";

//String?sql1?=?"CREATE?TABLE?WEATHER_PROCESS(ID?INT?PRIMARY?KEY?NOT?NULL,陰?int?NOT?NULL?,晴?int?NOT?NULL,雨?int?NOT?NULL)";

ResultSet?result?=null;

try{

stmt.executeUpdate(sql);

result?=?stmt.executeQuery(sql);

}catch(SQLException?e)?{

//?TODO?Auto-generated?catch?block

e.printStackTrace();

}

try{

intflag?=0;

while(result.next())?{

//System.out.print(result.getString("weather")+"、");

String?str?=?result.getString("weather"); ?//獲取表中weather的值

if("陰".equals(str))?{

stmt?=?conn.createStatement();

/*

*如果不重新stmt一個 則出現(xiàn)的是表中只會插入一條記錄,

*然后整個循環(huán)就退出了。

*/

String?sql2?="INSERT?INTO?WEATHER_PROCESS(id,?晴,陰,雨)?values?(weather_process_id.nextval,0,1,0)";

stmt.executeUpdate(sql2);

System.out.print(str+"、");

}elseif("晴".equals(str))?{

stmt?=?conn.createStatement();

String?sql2?="INSERT?INTO?WEATHER_PROCESS(id,?晴,陰,雨)?values?(weather_process_id.nextval,1,0,0)";

stmt.executeUpdate(sql2);

System.out.print(str+"、");

}else{

stmt?=?conn.createStatement();

String?sql2?="INSERT?INTO?WEATHER_PROCESS(id,?晴,陰,雨)?values?(weather_process_id.nextval,0,0,1)";

stmt.executeUpdate(sql2);

System.out.print(str+"、");

}

flag?++;

}

System.out.println("\n此次一共更新了"+flag+"條語句");

}catch(SQLException?e)?{

//?TODO?Auto-generated?catch?block

e.printStackTrace();

}

try{

result.close();

stmt.close();

conn.close();

}catch(SQLException?e)?{

//?TODO?Auto-generated?catch?block

e.printStackTrace();

}

returnconn;

}

}

歡迎共同交流,寫的不對的地方還望大家諒解,呵呵。

總結(jié)

以上是生活随笔為你收集整理的java 从数据库取值_JAVA操作数据库--从一张表中取值,经过判断,然后插入另一张表中。...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。