`

粗糙的区别prepareStatement:(为Statement的子类)与Statement

阅读更多

区别:
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);

分享到:
评论

相关推荐

    java数据库连接Statement

    PreparedStatement pstmt = con.prepareStatement("INSERT INTO Table1 (a, b) VALUES (?, ?)"); pstmt.setInt(1, 1); pstmt.setString(2, "value"); pstmt.executeUpdate(); ``` ### CallableStatement `...

    jdbc知识带注释

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO mytable VALUES (?, ?)"); pstmt.setString(1, "value1"); pstmt.setInt(2, 10); pstmt.addBatch(); pstmt.setString(1, "value2"); pstmt....

    jdbc连接数据库

    PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, value1); pstmt.setInt(2, value2); pstmt.executeUpdate(); ``` 5. **事务处理** JDBC支持事务管理,通过Connection对象的...

    java与各种数据库的连接与操作终极教程

    - 使用`Connection.prepareStatement()`创建`PreparedStatement`,然后设置参数(用`?`占位符表示),最后执行SQL。 4. **CallableStatement与存储过程**: - 对于调用数据库中的存储过程,我们使用`...

    JDBC连接数据库、增删改查

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO table_name (column1, column2) VALUES (?, ?)"); ``` 4. **执行SQL语句**:使用Statement或PreparedStatement对象执行SQL语句。对于查询操作,...

    第12章输入输出流、文件及数据库操作.doc

    3. 创建Statement:`Connection.createStatement()`或`Connection.prepareStatement()`。 4. 执行SQL:`Statement.executeXXX()`方法,如`executeQuery()`、`executeUpdate()`。 5. 处理结果:对于查询,使用`...

    parastatement的方法

    首先,通过Connection对象的`prepareStatement()`方法创建PreparedStatement对象,传入SQL语句作为参数。例如: ```java Connection conn = ...; // 获取数据库连接 String sql = "INSERT INTO employees (name,...

    链接JDBC该背的代码

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO your_table (column1, column2) VALUES (?, ?)"); ``` 4. **执行SQL语句** - **Statement**执行查询: ```java ResultSet rs = stmt....

    Java数据库连接+操作(模板方法模式应用)

    protected abstract void prepareStatement(Connection conn, String sql); protected abstract void execute(); public void templateMethod() { Connection conn = null; try { conn = getDBConnection();...

    java删除修改添加查询

    PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "value1"); pstmt.setInt(2, 123); pstmt.executeUpdate(); ``` - **修改(UPDATE)**:更新现有记录,同样使用`...

    JDBC基础教程之语句.doc

    PreparedStatement pstmt = con.prepareStatement("INSERT INTO Table1 (col1, col2) VALUES (?, ?)"); pstmt.setString(1, "value1"); pstmt.setInt(2, 2); int rowsAffected = pstmt.executeUpdate(); ``` 2...

    JDBC 3.0 数据库编程

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO users VALUES (?, ?)"); pstmt.setString(1, "John"); pstmt.setInt(2, 25); pstmt.executeUpdate(); ``` 4. **结果集ResultSet**:执行查询后,...

    jdbcbook.rar

    使用`Connection.prepareStatement()`创建。 6. **执行SQL**:`Statement`和`PreparedStatement`对象提供了多种执行SQL的方法,如`executeQuery()`用于执行返回单个结果集的SQL,`executeUpdate()`用于执行DML(增...

    JDBC详解.docx

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO table (column1, column2) VALUES (?, ?)"); pstmt.setString(1, value1); pstmt.setInt(2, value2); pstmt.executeUpdate(); ``` 5. **...

    JDBC操作技术 PDF

    - 执行SQL:`Statement stmt = conn.createStatement()` 或 `PreparedStatement pstmt = conn.prepareStatement(sql)` - 处理结果集:`ResultSet rs = stmt.executeQuery(sql)` - 关闭资源:确保每次操作后关闭...

    JDBC FOR SQL 2000 和2005

    PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM myTable WHERE id = ?"); pstmt.setInt(1, 123); ResultSet rs = pstmt.executeQuery(); ``` 5. **批处理操作** 对于大量重复的SQL操作,...

    JDBC连接mysq数据库Demo

    PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, "John"); pstmt.setInt(2, 30); pstmt.executeUpdate(); ``` 9. **连接池** 在实际项目中,通常使用连接池(如C3P0、HikariCP、...

    jdbc实例教程

    PreparedStatement pstmt = connection.prepareStatement(sql); pstmt.setString(1, value1); pstmt.setInt(2, value2); int rowsAffected = pstmt.executeUpdate(); ``` ### 1.2 修改数据 更新数据同样使用`...

    java数据库连接

    - 执行SQL:`Statement stmt = conn.createStatement()` 或 `PreparedStatement pstmt = conn.prepareStatement(sql)` - 处理结果集:`ResultSet rs = stmt.executeQuery(sql)`,遍历结果集进行数据操作。 - 关闭...

    JDBC学习笔记(笔记+包含详细注释的代码)

    使用`Connection.prepareStatement()`创建,占位符用问号`?`表示,通过`setXXX()`方法设置参数。 ### 异常处理 在JDBC编程中,异常处理是关键。通常使用`try-catch-finally`结构,捕获`SQLException`及其子类。在`...

Global site tag (gtag.js) - Google Analytics