資源簡介
http://blog.csdn.net/deathislikethewind/article/details/71122647
代碼片段和文件信息
package?stu;
import?java.sql.Connection;??
import?java.sql.DriverManager;??
import?java.sql.PreparedStatement;??
import?java.sql.SQLException;??
public?class?ConnectMysql{
private?static?final?String?url?=?“jdbc:mysql://127.0.0.1:3306/sql?useUnicode=true&characterEncoding=utf-8&useSSL=false“;?
//MySQL?表示連接的數據庫?是?MySQL?;??127.0.0.1表示你的MySQL的遠程端地址(127.0.0.1表示的是本地地址)?3306表示是這個端口
//后邊的一大串?useUnicode=true&characterEncoding=utf-8&useSSL=false?設置數據庫屬性如果不添加被曝↓↓↓↓
//WARN:Establishing?SSL?connection?without?server‘s?identity?verification?is?not?recommended.KILL?WARN??useUnicode=true&characterEncoding=utf-8&useSSL=false?↑
private?static?final?String?name?=?“com.mysql.jdbc.Driver“;
public?static?final?String?user?=?“root“; //數據庫的用戶名
public?static?final?String?password?=?“ABCabc123“; //數據庫的用戶密碼
public?Connection?connect?=?null; //連接類這就是那個鏈接啊
????public?PreparedStatement?pStatement?=?null;? //準備聲明
????
????
????public?ConnectMysql(String?SqlStatement){
???? try{
???? Class.forName(name); //指定連接類型?
???? connect?=?DriverManager.getConnection(url?user?password); //獲取連接?
???? pStatement?=?connect.prepareStatement(SqlStatement); //準備執行語句
???? }
???? catch?(Exception?e)?{
???? e.printStackTrace();
}
????}
????
????
????public?void?close()?{??
????????try?{??
????????????this.connect.close();?? //關閉鏈接
????????????this.pStatement.close();?? //關閉準備語句
????????}?catch?(SQLException?e)?{??
????????????e.printStackTrace();
????????}??
????}??
}
評論
共有 條評論