> 데이터 베이스 > MySQL 튜토리얼 > jdbc操作数据库语句

jdbc操作数据库语句

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
풀어 주다: 2016-06-07 15:47:20
원래의
992명이 탐색했습니다.

非常有用的jdbc操作数据库语句,记录下来,以方便以后的查询。 public class PersonDao { // 增加操作 public void insert(Person person) throws Exception; // 修改操作 public void update(Person person) throws Exception; // 删除操作 public void del

非常有用的jdbc操作数据库语句,记录下来,以方便以后的查询。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public class PersonDao {

 

    // 增加操作 

    public void insert(Person person) throws Exception;

     

    // 修改操作 

    public void update(Person person) throws Exception;

    // 删除操作 

    public void delete(String id) throws Exception ;

    // 按ID查询操作 

    public Person queryById(String id) throws Exception;

    // 查询全部 

    public List queryAll() throws Exception;

    // 模糊查询 

    public List queryByLike(String cond) throws Exception;

}

로그인 후 복사

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

// 此类需要完成具体的数据库操作,需要JDB代码 

public class PersonDAOImpl implements PersonDAO 

    // 增加操作 

    public void insert(Person person) throws Exception 

    

        String sql = "INSERT INTO person (id,name,password,age,email) VALUES (?,?,?,?,?)"

        PreparedStatement pstmt = null ; 

        DataBaseConnection dbc = null ; 

   

        // 下面是针对数据库的具体操作 

        try 

        

            // 连接数据库 

            dbc = new DataBaseConnection() ; 

            pstmt = dbc.getConnection().prepareStatement(sql) ; 

            pstmt.setString(1,person.getId()) ; 

            pstmt.setString(2,person.getName()) ; 

            pstmt.setString(3,person.getPassword()) ; 

            pstmt.setInt(4,person.getAge()) ; 

            pstmt.setString(5,person.getEmail()) ; 

            // 进行数据库更新操作 

            pstmt.executeUpdate() ; 

            pstmt.close() ; 

        

        catch (Exception e) 

        

            throw new Exception("操作出现异常") ; 

        

        finally 

        

            // 关闭数据库连接 

            dbc.close() ; 

        

    

    // 修改操作 

    public void update(Person person) throws Exception 

    

        String sql = "UPDATE person SET name=?,password=?,age=?,email=? WHERE id=?"

        PreparedStatement pstmt = null ; 

        DataBaseConnection dbc = null ; 

   

        // 下面是针对数据库的具体操作 

        try 

        

            // 连接数据库 

            dbc = new DataBaseConnection() ; 

            pstmt = dbc.getConnection().prepareStatement(sql) ;          

            pstmt.setString(1,person.getName()) ; 

            pstmt.setString(2,person.getPassword()) ; 

            pstmt.setInt(3,person.getAge()) ; 

            pstmt.setString(4,person.getEmail()) ; 

            pstmt.setString(5,person.getId()) ; 

            // 进行数据库更新操作 

            pstmt.executeUpdate() ; 

            pstmt.close() ; 

        

        catch (Exception e) 

        

            throw new Exception("操作出现异常") ; 

        

        finally 

        

            // 关闭数据库连接 

            dbc.close() ; 

        

    

    // 删除操作 

    public void delete(String id) throws Exception 

    

        String sql = "DELETE FROM person WHERE id=?"

        PreparedStatement pstmt = null ; 

        DataBaseConnection dbc = null ; 

   

        // 下面是针对数据库的具体操作 

        try 

        

            // 连接数据库 

            dbc = new DataBaseConnection() ; 

            pstmt = dbc.getConnection().prepareStatement(sql) ;          

            pstmt.setString(1,id) ; 

            // 进行数据库更新操作 

            pstmt.executeUpdate() ; 

            pstmt.close() ; 

        

        catch (Exception e) 

        

            throw new Exception("操作出现异常") ; 

        

        finally 

        

            // 关闭数据库连接 

            dbc.close() ; 

        

    

    // 按ID查询操作 

    public Person queryById(String id) throws Exception 

    

        Person person = null ; 

        String sql = "SELECT id,name,password,age,email FROM person WHERE id=?"

        PreparedStatement pstmt = null ; 

        DataBaseConnection dbc = null ; 

   

        // 下面是针对数据库的具体操作 

        try 

        

            // 连接数据库 

            dbc = new DataBaseConnection() ; 

            pstmt = dbc.getConnection().prepareStatement(sql) ;          

            pstmt.setString(1,id) ; 

            // 进行数据库查询操作 

            ResultSet rs = pstmt.executeQuery() ; 

            if(rs.next()) 

            

                // 查询出内容,之后将查询出的内容赋值给person对象 

                person = new Person() ; 

                person.setId(rs.getString(1)) ; 

                person.setName(rs.getString(2)) ; 

                person.setPassword(rs.getString(3)) ; 

                person.setAge(rs.getInt(4)) ; 

                person.setEmail(rs.getString(5)) ; 

            

            rs.close() ; 

            pstmt.close() ; 

        

        catch (Exception e) 

        

            throw new Exception("操作出现异常") ; 

        

        finally 

        

            // 关闭数据库连接 

            dbc.close() ; 

        

        return person ; 

    

    // 查询全部 

    public List queryAll() throws Exception 

    

        List all = new ArrayList() ; 

        String sql = "SELECT id,name,password,age,email FROM person"

        PreparedStatement pstmt = null ; 

        DataBaseConnection dbc = null ; 

   

        // 下面是针对数据库的具体操作 

        try 

        

            // 连接数据库 

            dbc = new DataBaseConnection() ; 

            pstmt = dbc.getConnection().prepareStatement(sql) ;          

            // 进行数据库查询操作 

            ResultSet rs = pstmt.executeQuery() ; 

            while(rs.next()) 

            

                // 查询出内容,之后将查询出的内容赋值给person对象 

                Person person = new Person() ; 

                person.setId(rs.getString(1)) ; 

                person.setName(rs.getString(2)) ; 

                person.setPassword(rs.getString(3)) ; 

                person.setAge(rs.getInt(4)) ; 

                person.setEmail(rs.getString(5)) ; 

   

                // 将查询出来的数据加入到List对象之中 

                all.add(person) ; 

            

            rs.close() ; 

            pstmt.close() ; 

        

        catch (Exception e) 

        

            throw new Exception("操作出现异常") ; 

        

        finally 

        

            // 关闭数据库连接 

            dbc.close() ; 

        

        return all ; 

    

    // 模糊查询 

    public List queryByLike(String cond) throws Exception 

    

        List all = new ArrayList() ; 

        String sql = "SELECT id,name,password,age,email FROM person WHERE name LIKE ? or email LIKE ?"

        PreparedStatement pstmt = null ; 

        DataBaseConnection dbc = null ; 

   

        // 下面是针对数据库的具体操作 

        try 

        

            // 连接数据库 

            dbc = new DataBaseConnection() ; 

            pstmt = dbc.getConnection().prepareStatement(sql) ;  

            // 设置模糊查询条件 

            pstmt.setString(1,"%"+cond+"%") ; 

            pstmt.setString(2,"%"+cond+"%") ; 

            // 进行数据库查询操作 

            ResultSet rs = pstmt.executeQuery() ; 

            while(rs.next()) 

            

                // 查询出内容,之后将查询出的内容赋值给person对象 

                Person person = new Person() ; 

                person.setId(rs.getString(1)) ; 

                person.setName(rs.getString(2)) ; 

                person.setPassword(rs.getString(3)) ; 

                person.setAge(rs.getInt(4)) ; 

                person.setEmail(rs.getString(5)) ; 

   

                // 将查询出来的数据加入到List对象之中 

                all.add(person) ; 

            

            rs.close() ; 

            pstmt.close() ; 

        

        catch (Exception e) 

        

            throw new Exception("操作出现异常") ; 

        

        finally 

        

            // 关闭数据库连接 

            dbc.close() ; 

        

        return all ; 

    

}; 

로그인 후 복사
PS:

1.pstmt操作时,第一个空格处,应该填写1,而不是0,否则会报错误的列索引;

2.查询完resultSet以后,先要对其进行非空判断,如果存在,再进行下一步的赋值;

3.数据库每次连接时,都要记住关闭,当然完全可以用数据库连接池来取代它,后面会对此做改进。


본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿