怎么获取PreparedStatement 的最终执行SQL

怎么获取PreparedStatement 的最终执行SQL,第1张

自己看吧。

public void update(User u) {

Connection conn = null;

PreparedStatement stmt = null;

String sql = "update t_user set uname=,upwd=,addNum= where id = ";

try {

conn = ConnectionUtilsgetConnection();

stmt = connprepareStatement(sql);

stmtsetString(1, ugetUname());

stmtsetString(2, ugetUpwd());

stmtsetInt(3, ugetAddNum());

stmtsetInt(4, ugetId());

stmtexecuteUpdate();

} catch (SQLException e) {

eprintStackTrace();

} finally{

ConnectionUtilsclose(stmt);

ConnectionUtilsclose(conn);

}

}

用JDBC连接数据库,然后用sql语句。要导入mysql的驱动包。

import javasql;

public class TestMySql {

static Connection con = null; // 声明Connection对象

static Statement sql = null;

static ResultSet res = null;

public static void main(String[] args) {

TestMySql c = new TestMySql();

con = cgetConnection();

try {

sql = concreateStatement();

res = sqlexecuteQuery("select from dept");

//sql语句,我数据库里有张dept表

while (resnext()) {//输出结果

Systemoutprint(resgetString(1) + "<——>");

Systemoutprint(resgetString(2) + "<——>");

Systemoutprint(resgetString(3) );

Systemoutprintln();

}

} catch (SQLException e) {

eprintStackTrace();

} finally {

try {

if (res != null) {

resclose();

res =null;

}

if (sql != null) {

sqlclose();

sql =null;

}

if (con != null) {

conclose();

con =null;

}

} catch (SQLException e) {

eprintStackTrace();

}

}

}

public Connection getConnection() {

try {

ClassforName("commysqljdbcDriver");

// 加载oracleJDBC驱动

Systemoutprintln("数据库驱动加载成功");

} catch (ClassNotFoundException e) {

eprintStackTrace();

}

try {// 通过访问数据库的URL获取数据库连接对象

con = DriverManagergetConnection(

"jdbc:mysql://localhost:3306/mydata", "root", "qwer1234");

//mydata为mysql名字

Systemoutprintln("数据库连接成功");

} catch (SQLException e) {

eprintStackTrace();

}

return con; // 按方法要求返回一个Connection对象

}

}

好像是这个方法

getParameterMetaData

ParameterMetaData getParameterMetaData()

throws SQLException检索此 PreparedStatement 对象的参数的编号、类型和属性。

返回:一个 ParameterMetaData 对象,它包含有关此 PreparedStatement 对象的参数的编号、类型和属性的信息抛出:SQLException - 如果发生数据库访问错误

不过还是不明白,"select from table where field= and field="我想设置的参数应该是通过函数参数传递过来,

以上就是关于怎么获取PreparedStatement 的最终执行SQL全部的内容,包括:怎么获取PreparedStatement 的最终执行SQL、java中怎么获取mysql数据库的数据、如何获取PreparedStatement参数设置的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9542989.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存