最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

Java:理解預(yù)處理完善DBUtils,F(xiàn)ile,在D盤等處創(chuàng)建文件夾和文件,JDBC查詢【詩(shī)書畫唱

2020-07-12 01:35 作者:詩(shī)書畫唱  | 我要投稿



package liZi;


import java.io.IOException;

import java.io.InputStream;

import java.sql.*;

import java.util.Properties;


public class DBUtilsShiWu {


public static void main(String[] args) throws Exception{

// //通過(guò)獲取統(tǒng)一的用上預(yù)處理的DBUtils中的Connection時(shí),

// 用的就都是同一個(gè)Connection

Connection con1=DBUtilsPreparedStatement.getCon();

con1.setAutoCommit(false);

//setAutoCommit(false)表示提交方式為手動(dòng)提交

String sql1="update shangDian set shangDianMoney+="

+ "20 where shangDianId=1";

DBUtilsPreparedStatement.ZSG(sql1,con1);

String sql2="update yongHu set yongHuMoney-="

+ "20 where yongHuId=1";

DBUtilsPreparedStatement.ZSG(sql2,con1);

// //雖然兩個(gè)SQL語(yǔ)句執(zhí)行了兩次修改,但是沒(méi)有往數(shù)據(jù)庫(kù)提交信息,什么時(shí)候提交,

// //但當(dāng)調(diào)用了commit()的時(shí)候,會(huì)一次性進(jìn)行提交

String selectSql1="select * from shangDian where shangDianId=1";

ResultSet res1=DBUtilsPreparedStatement.Select(selectSql1,con1);

String selectSql2="select * from yongHu where yongHuId=1";

ResultSet res2=DBUtilsPreparedStatement.Select(selectSql2,con1);

while(res1.next()){

System.out.println("商店編號(hào):"+res1.getObject(1)+"\t商店名稱:"

+res1.getObject(2)+"\t商店擁有的金額:"+res1.getObject(3)+"元"

);

}

while(res2.next()){

System.out.println("用戶編號(hào):"+res2.getObject(1)+"\t用戶名:"

+res2.getObject(2)+"\t用戶擁有的金額:"+res2.getObject(3)+"元"

);

}

con1.commit();//commit表示全部執(zhí)行完后批量提交

}

//框架就是簡(jiǎn)化開發(fā),框架給我們生成好了,只需要修改相應(yīng)的配置即可

}





常用的預(yù)處理完善DBUtils,有些別人封裝的更完善的DBUtils可以去找后直接調(diào)用等:








package liZi;

import java.io.InputStream;

import java.sql.*;

import java.util.Properties;


class DBUtilsPreparedStatement{

static String url,root,uname,pwd=null;

static PreparedStatement ps=null;

static Connection con=null;

static ResultSet res=null;

static{

InputStream is=DBUtilsPreparedStatement.class.

getResourceAsStream("./database.properties");

Properties p=new Properties();

try {

p.load(is);

url=p.getProperty("url");

root=p.getProperty("root");

uname=p.getProperty("uname");

pwd=p.getProperty("pwd");


Class.forName(root);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static Connection getCon(){

if(con==null){

try {

con=DriverManager.getConnection(url,uname,pwd);

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return con;

}

public static ResultSet Select(String sql,Connection con1,Object... o){

con=con1;

System.out.println(sql);

try {

ps=con.prepareStatement(sql);

for(int i=0;i<o.length;i++){

ps.setObject(i+1,o[i]);

}

res=ps.executeQuery();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return res;

}

public static boolean ZSG(String sql,Connection con1,Object... o){

con=con1;

boolean b=false;

try {

ps=con.prepareStatement(sql);

for(int i=0;i<o.length;i++){

ps.setObject(i+1,o[i]);

}

int num=ps.executeUpdate();

if(num>0){

b=true;

}

} catch (Exception e) {

e.printStackTrace();

}

return b;

}

}




url=jdbc:sqlserver://localhost;databaseName=yonghu

uname=qqq

pwd=123

root=com.microsoft.sqlserver.jdbc.SQLServerDriver


在D盤等處創(chuàng)建文件夾和text文件



package createFile;


import java.io.File;


public class file {

public static void main(String[] args) throws Exception{

File f=new File("D://集成開發(fā)工具/NewFile");

File f1=new File(f,"NewFile.text");

f.mkdir();


System.out.println("文件夾創(chuàng)建完畢");


f1.createNewFile();


System.out.println("文件創(chuàng)建完畢");

}

}






Java:理解預(yù)處理完善DBUtils,F(xiàn)ile,在D盤等處創(chuàng)建文件夾和文件,JDBC查詢【詩(shī)書畫唱的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
布尔津县| 东乡| 益阳市| 济源市| 望谟县| 宁强县| 申扎县| 封开县| 许昌市| 托克托县| 泗阳县| 新巴尔虎左旗| 栖霞市| 扎赉特旗| 农安县| 霞浦县| 沁源县| 芮城县| 北票市| 突泉县| 长葛市| 江津市| 乌拉特中旗| 玉门市| 南陵县| 石屏县| 建平县| 清水河县| 平昌县| 廊坊市| 祁连县| 安达市| 防城港市| 原平市| 马边| 绩溪县| 石渠县| 香港| 内乡县| 紫阳县| 印江|