区别:
prepareStatement:(为Statement的子类)
conn = DBFactory.getInstance().getImpl().getConnection();
***pstmt = conn.prepareStatement(" update "+tb+" set WDNR=? where "+bh+"=?");
pstmt.setBinaryStream(1, doc.getWDNR(), (int) doc.getStreamSize());
pstmt.setString(2, doc.getWDMC());
***returnVal = pstmt.executeUpdate();
______________________________________
Statement:
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
Class.forName("org.mariadb.jdbc.Driver").newInstance(); //用于加载Driver类(jdbc驱动器),,,registerDriver注册java.sql.DriverManager.registerDriver
String url = "jdbc:mariadb://192.168.1.100:3306/test?user=root&password=pdcss";
conn = DriverManager.getConnection(url);
***stmt = conn.createStatement();
***rs = stmt.executeQuery(sql);
相关推荐
PreparedStatement pstmt = con.prepareStatement("INSERT INTO Table1 (a, b) VALUES (?, ?)"); pstmt.setInt(1, 1); pstmt.setString(2, "value"); pstmt.executeUpdate(); ``` ### CallableStatement `...
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO mytable VALUES (?, ?)"); pstmt.setString(1, "value1"); pstmt.setInt(2, 10); pstmt.addBatch(); pstmt.setString(1, "value2"); pstmt....
PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, value1); pstmt.setInt(2, value2); pstmt.executeUpdate(); ``` 5. **事务处理** JDBC支持事务管理,通过Connection对象的...
- 使用`Connection.prepareStatement()`创建`PreparedStatement`,然后设置参数(用`?`占位符表示),最后执行SQL。 4. **CallableStatement与存储过程**: - 对于调用数据库中的存储过程,我们使用`...
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO table_name (column1, column2) VALUES (?, ?)"); ``` 4. **执行SQL语句**:使用Statement或PreparedStatement对象执行SQL语句。对于查询操作,...
3. 创建Statement:`Connection.createStatement()`或`Connection.prepareStatement()`。 4. 执行SQL:`Statement.executeXXX()`方法,如`executeQuery()`、`executeUpdate()`。 5. 处理结果:对于查询,使用`...
首先,通过Connection对象的`prepareStatement()`方法创建PreparedStatement对象,传入SQL语句作为参数。例如: ```java Connection conn = ...; // 获取数据库连接 String sql = "INSERT INTO employees (name,...
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO your_table (column1, column2) VALUES (?, ?)"); ``` 4. **执行SQL语句** - **Statement**执行查询: ```java ResultSet rs = stmt....
protected abstract void prepareStatement(Connection conn, String sql); protected abstract void execute(); public void templateMethod() { Connection conn = null; try { conn = getDBConnection();...
PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "value1"); pstmt.setInt(2, 123); pstmt.executeUpdate(); ``` - **修改(UPDATE)**:更新现有记录,同样使用`...
PreparedStatement pstmt = con.prepareStatement("INSERT INTO Table1 (col1, col2) VALUES (?, ?)"); pstmt.setString(1, "value1"); pstmt.setInt(2, 2); int rowsAffected = pstmt.executeUpdate(); ``` 2...
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO users VALUES (?, ?)"); pstmt.setString(1, "John"); pstmt.setInt(2, 25); pstmt.executeUpdate(); ``` 4. **结果集ResultSet**:执行查询后,...
使用`Connection.prepareStatement()`创建。 6. **执行SQL**:`Statement`和`PreparedStatement`对象提供了多种执行SQL的方法,如`executeQuery()`用于执行返回单个结果集的SQL,`executeUpdate()`用于执行DML(增...
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO table (column1, column2) VALUES (?, ?)"); pstmt.setString(1, value1); pstmt.setInt(2, value2); pstmt.executeUpdate(); ``` 5. **...
- 执行SQL:`Statement stmt = conn.createStatement()` 或 `PreparedStatement pstmt = conn.prepareStatement(sql)` - 处理结果集:`ResultSet rs = stmt.executeQuery(sql)` - 关闭资源:确保每次操作后关闭...
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM myTable WHERE id = ?"); pstmt.setInt(1, 123); ResultSet rs = pstmt.executeQuery(); ``` 5. **批处理操作** 对于大量重复的SQL操作,...
PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "John"); pstmt.setInt(2, 30); pstmt.executeUpdate(); ``` 9. **连接池** 在实际项目中,通常使用连接池(如C3P0、HikariCP、...
PreparedStatement pstmt = connection.prepareStatement(sql); pstmt.setString(1, value1); pstmt.setInt(2, value2); int rowsAffected = pstmt.executeUpdate(); ``` ### 1.2 修改数据 更新数据同样使用`...
- 执行SQL:`Statement stmt = conn.createStatement()` 或 `PreparedStatement pstmt = conn.prepareStatement(sql)` - 处理结果集:`ResultSet rs = stmt.executeQuery(sql)`,遍历结果集进行数据操作。 - 关闭...
使用`Connection.prepareStatement()`创建,占位符用问号`?`表示,通过`setXXX()`方法设置参数。 ### 异常处理 在JDBC编程中,异常处理是关键。通常使用`try-catch-finally`结构,捕获`SQLException`及其子类。在`...