Java:理解預(yù)處理完善DBUtils,F(xiàn)ile,在D盤等處創(chuàng)建文件夾和文件,JDBC查詢【詩(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)建完畢");
}
}





