1. 程式人生 > 其它 >javaweb22/4/11

javaweb22/4/11

JDBC

JDBC基本用法

1.普通編譯,connection.createStatement

public class JDBCTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置資訊
        String url = "jdbc:mysql://localhost:3306/hotel?useUnicode=true&charachterEncoding=utf-8";
        String username = "root";
        String password = "297999";
        //載入驅動
        Class.forName("com.mysql.jdbc.Driver");
        //連線資料庫,connection代表資料可庫
        Connection connection = DriverManager.getConnection(url, username, password);
        //向資料庫傳送SQL物件statement:做增刪改查
        Statement statement = connection.createStatement();
        String sql= "select * from account";
        ResultSet rs = statement.executeQuery(sql);
        while (rs.next()){
            System.out.println("id:"+rs.getInt("id"));
            System.out.println("name:"+rs.getString("name"));
            System.out.println("pwd:"+rs.getString("pwd"));
            System.out.println("mobile:"+rs.getString("mobile"));
            System.out.println("email:"+rs.getString("email"));
            System.out.println("type:"+rs.getString("type"));
        }
        //一定要關閉連線!!
        rs.close();
        statement.close();
        connection.close();
    }

}

2.預編譯,connection.prepareStatement()

public class JDBCTest {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //配置資訊
        String url = "jdbc:mysql://localhost:3306/hotel?useUnicode=true&charachterEncoding=utf-8";
        String username = "root";
        String password = "297999";
        //載入驅動
        Class.forName("com.mysql.jdbc.Driver");
        //連線資料庫,connection代表資料可庫
        Connection connection = DriverManager.getConnection(url, username, password);
        //向資料庫傳送SQL物件statement:做增刪改查

        String sql= "insert into account(id, name, pwd, mobile, email, type) values (?,?,?,?,?,?)";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setInt(1,3);
        preparedStatement.setString(2,"張三");
        preparedStatement.setString(3,"123456");
        preparedStatement.setString(4,"987654321");
        preparedStatement.setString(5,"[email protected]");
        preparedStatement.setInt(6,1);

        //i是影響的行數
        int i = preparedStatement.executeUpdate();
        //一定要關閉連線!!
        if (i>0){
            System.out.println("新增成功");
        }
        preparedStatement.close();
        connection.close();
    }

}

事務

public class JDBCTest3 {
    @Test
   public void test() throws SQLException, ClassNotFoundException {
        //配置資訊
        String url = "jdbc:mysql://localhost:3306/hotel?useUnicode=true&charachterEncoding=utf-8";
        String username = "root";
        String password = "297999";
        Connection connection = null;
        try {
            //載入驅動
            Class.forName("com.mysql.jdbc.Driver");
            //連線資料庫,connection代表資料可庫
            connection = DriverManager.getConnection(url, username, password);

            //開啟事務,false是開啟
            connection.setAutoCommit(false);
            //事務的第一條語句
            String sql = "update account set name='李四' where id=1";
            connection.prepareStatement(sql).executeUpdate();
            //錯誤程式碼
//            int i=1/0;
            //事務的第二條語句
            String sql2 = "update account set name='王五' where id=2";
            connection.prepareStatement(sql2).executeUpdate();
            //若以上兩條語句都執行成功則提交事務
            connection.commit();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            //事務回滾
            connection.rollback();
            e.printStackTrace();
        }finally {
            connection.close();
        }


    }
}