DBUtil工具类实现!

package com.zhiyou100.video.utils;


import java.sql.Connection;




import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;


import com.mysql.jdbc.PreparedStatement;
/**
 * 
 * @author Yuliujun
 * mail:[email protected]
 *
 */




public class DBUtil {


String driver = "com.mysql.jdbc.Driver";
String url ="jdbc:mysql://localhost:3307/zy_video";

Connection con = null;
ResultSet res = null;

public void DataBase() {
try {
Class.forName(driver);
try {
con = DriverManager.getConnection(url,"bin","123456");

} catch (SQLException e) {
System.out.println("无法连接数据库");
e.printStackTrace();
}

} catch (ClassNotFoundException e) {
System.out.println("装载 JDBC/ODBC 驱动程序失败");
e.printStackTrace();
}

}

//查询
public ResultSet Search(String sql,String str[]) {
DataBase();
try {
PreparedStatement pst =  (PreparedStatement) con.prepareStatement(sql);
    if(str!=null) {
    for(int i =0;i<str.length;i++) {
    pst.setString(i+1, str[i]);
    }
    }
    
    res = pst.executeQuery();
} catch (Exception e) {

}
System.out.println("ressql数据库查找"+res.toString());
return res;
}
//
public int AddU(String sql,String str[]) {

int a = 0;
DataBase();
try {
PreparedStatement pst = (PreparedStatement) con.prepareStatement(sql);
if(str!=null) {
    for(int i =0;i<str.length;i++) {
    pst.setString(i+1, str[i]);
    }
    }
a = pst.executeUpdate();
 
} catch (Exception e) {

}

return a;

}




}

猜你喜欢

转载自blog.csdn.net/qq_41338740/article/details/80943579
今日推荐