1.java中提供接口為多種數(shù)據(jù)庫(kù)提供規(guī)范
- 具體實(shí)現(xiàn)接口類(lèi)由各數(shù)據(jù)廠商提供
2.開(kāi)發(fā)步驟
1.注冊(cè)驅(qū)動(dòng).2.獲得連接.
3.獲得語(yǔ)句執(zhí)行平臺(tái)4.執(zhí)行sql語(yǔ)句
5.處理結(jié)果6.釋放資源.
---------------------
不使用java.sql.DriverManager.registerDriver(new Driver());
是因?yàn)閞egisterDriver同樣有new會(huì)注冊(cè)兩次
Class.forName("com.mysql.jdbc.Driver");
String urlString=
"jdbc:mysql://localhost:3306/myabc",
usename = "root",
password = "0616";
Connection co = DriverManager.getConnection(urlString, usename,password);
String sql = "SELECT * FROM datausers ";
Statement st = co.createStatement();
ResultSet rest = st.executeQuery(sql);
List<Datausers> arr=new ArrayList<Datausers>();
while (rest.next()) {
Datausers use=new Datausers(rest.getString("dser"), rest.getString("dpassword"));
arr.add(use);
}
co.close();
st.close();
rest.close();
使用Statement 會(huì)產(chǎn)生注入攻擊密碼后加入'or'1=1就會(huì)為真
-----------------------使用PreparedStatement 不會(huì)產(chǎn)生注入攻擊并且速度快
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String urlString = "jdbc:mysql://localhost:3306/myabc", usename = "root", password = "0616";
Connection co = DriverManager.getConnection(urlString, usename,
password);
String sql = "SELECT * FROM datausers WHERE dser=?AND dpassword=?or 1=1";
PreparedStatement ps = co.prepareStatement(sql);
Scanner sc = new Scanner(System.in);
String user = sc.nextLine();
String pass = sc.nextLine();
ps.setObject(1, user);
ps.setObject(2, pass);
ResultSet rest = ps.executeQuery();上處使用sql已經(jīng)初始化此處就不用了
while (rest.next()) {
System.out.println(rest.getObject("dser") + " "
+ rest.getObject("dpassword"));
}
rest.close();
ps.close();
co.close();
}
executeQuery()返回ResultSet可以處理結(jié)果
而st.executeUpdate()會(huì)返回int僅可以做insert择吊,delete疙咸,update
3.注冊(cè)浅侨,close重復(fù)代碼多盗扇,寫(xiě)成tool類(lèi)
public class Propertool {
private static Connection co;
static {
try {
Class.forName("com.mysql.jdbc.Driver");
String urlString = "jdbc:mysql://localhost:3306/myabc", usename = "root", password = "0616";
co = DriverManager.getConnection(urlString, usename, password);
} catch (Exception ex) {
throw new RuntimeException(ex + "連接數(shù)據(jù)庫(kù)失敗");
}
}
private Propertool() {
}
public static Connection getConnection() {
return co;
}
public static void close(Connection co, Statement st, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (Exception e) { }
}
if (co != null) {
try {
co.close();
} catch (Exception e) {
}
}
if (st != null) {
try {
st.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
4.將url等寫(xiě)成曾配置文件
- newfile文件,注意空格問(wèn)題(可能在句末)
- 文件放在src下嘹裂,如此會(huì)在創(chuàng)建時(shí)同樣在bin目錄下創(chuàng)建(bin是放編譯后class文件地方)
drivername=com.mysql.jdbc.Driver
urlString=jdbc:mysql://localhost:3306/myabc
usename=root
password=0616
---------------------------用Properties讀取
public class sqlBin {
public static void main(String[] args) throws Exception{
InputStream in= sqlBin.class.getClassLoader().getResourceAsStream("databases.properties");
Properties pro=new Properties();
pro.load(in);
String drivername= pro.getProperty("drivername");
String urlString= pro.getProperty("urlString");
String usename=pro.getProperty("usename");
String password=pro.getProperty("password");
System.out.println(drivername+ " "+password+" "+urlString +" "+usename);
Class.forName(drivername);
Connection con=DriverManager.getConnection(urlString, usename, password);
System.out.println(con);
con.close();
}}
在使用java連接時(shí)候需要打開(kāi)mysql服務(wù)(net start mysql)
-----------------------
實(shí)際使用時(shí)候需要將本類(lèi)寫(xiě)成工具類(lèi),在代碼塊中static{}中調(diào)取方法
public class sqlBin {
private static String drivername;
private static String urlString;
private static String usename;
private static String password;
private static Connection con;
static
{
try {
getConfig();
Class.forName(drivername);
con=DriverManager.getConnection(urlString, usename, password);
} catch (Exception e) {
throw new RuntimeException("連接數(shù)據(jù)庫(kù)失敗"); }
}
private static void getConfig ()throws Exception
{
InputStream in= sqlBin.class.getClassLoader().getResourceAsStream("databases.properties");
Properties pro=new Properties();
pro.load(in);
drivername= pro.getProperty("drivername");
urlString= pro.getProperty("urlString");
usename=pro.getProperty("usename");
password=pro.getProperty("password");
}