2014/08/27

[Java] 連線至sql資料庫


有點久沒更新了,果然放暑假會放縱自己啊ˊˋ



前置作業:

1. 下載,mysql-connetor.jar

2. 加入至你的java project(右鍵 -> properties -> java build path -> add external jar)


/*******以下是code**********/

1. IPaddress:統一數值方便取用,建一個interface

?
1
2
3
4
5
6
    public interface IPaddress {
    final String ip = "IP地址";
    final String sqldbaccount = "使用者名稱";
    final String sqldbpass = "使用者密碼";
    final String dbName = "資料庫名稱";
}

2. 要取用的class implements IPaddress即可

3. 宣告4個等一下會用到的變數

?
1
2
3
4
private Connection connection = null;
private Statement statement = null;
private ResultSet resultSet = null;
private PreparedStatement pStatement = null;

4. 與資料庫取得連線

?
1
2
3
4
5
6
7
8
9
10
try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://"+ip+"/"+dbName+"?useUnicode=true&characterEncoding=Big5",
        sqldbaccount,sqldbpass);
    } catch (ClassNotFoundException e) {
        //System.out.println(e);
    }catch (SQLException e) {
        //System.out.println(e);
    }

5. closedb(在每個access db過後,要關閉連線資料)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public void closedb(){
    try {
        if (resultSet!=null){
            resultSet.close();
            resultSet = null;
        }
        if (statement != null) {
            statement.close();
            statement = null;
        }
        if (pStatement != null) {
            pStatement.close();
            pStatement = null;
        }
    } catch (SQLException e) {
        //System.out.println(e);
    }
}

基本上到這就差不多了,剩下的就是pStatment, resultSet, statement 互相的搭配使用而已

例如:
(1)我只需要簡單地執行一段sql而已,我就用statement

?
1
2
3
4
5
6
7
8
9
try {
        statement = connection.createStatement();
        statement.executeUpdate(你想執行的sql);
    } catch (SQLException e) {
        //System.out.println(e);
    }finally{
        closedb();
    }
}

(2)我想要取用一些當地變數,使用pStatment

?
1
2
3
4
5
6
7
8
9
10
11
12
13
String 你的SQL = "insert into User(id,name,nickName, passwd) " +
      "select ifNULL(max(id),0)+1,?,?,? FROM User";
try {
    pStatement = connection.prepareStatement(你的SQL);
    pStatement.setString(1, 當地變數);//第一個問號
    pStatement.setString(2, 當地變數);//第二個問號
    pStatement.setString(3, 當地變數);//第三個問號
    pStatement.executeUpdate();
    } catch (SQLException e) {
        //System.out.println(e);
    }finally{
        closedb();
    }

(3)我想要取回資料表裡的資料,使用statement+resultSet 或 pStatement+resultSet

?
1
2
3
4
5
6
7
8
9
10
11
12
try {
    statement = connection.createStatement();
    resultSet = statement.executeQuery("select * from User ");
    while (resultSet.next()) {
         System.out.println(resultSet.getInt("id")+"\t\t"+
                    resultSet.getString("name")+"\t\t"+resultSet.getString("passwd"));
    }
} catch (SQLException e) {
    //System.out.println("selectTable:"+e);
}finally{
    closedb();
}

6. 建立資料表table(可用可不用)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void creatTable_friend(){//創建資料表
    String createdbSQL_friend = "CREATE TABLE "+name+"_friend (" +
            "    id     INTEGER " +
            "  , name    VARCHAR(20)"+
            "  , state VARCHAR(8))";
    try {
        statement = connection.createStatement();
        statement.executeUpdate(createdbSQL_friend);
    } catch (SQLException e) {
        //System.out.println(e);
    }finally{
        closedb();//最後一定要關閉
    }
}

註冊登入範例: