想获取预编译语句中的参数类型,可是一直错误。
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ems", "root", "1234");
System.out.println("连接成功!");
PreparedStatement stmt = conn
.prepareStatement("select * from emp where id=?");
ParameterMetaData pmd=stmt.getParameterMetaData();
System.out.println(pmd.getParameterCount());
System.out.println(pmd.getParameterType(1));
输出连接成功!
1
Exception in thread "main" java.sql.SQLException: Parameter metadata not available for the given statement
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.MysqlParameterMetadata.checkAvailable(MysqlParameterMetadata.java:66)
at com.mysql.jdbc.MysqlParameterMetadata.getParameterType(MysqlParameterMetadata.java:115)
at demo.OracleJdbcDemo.main(OracleJdbcDemo.java:26)
谁能告诉我什么原因。
Connection conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/ems", "root", "1234");
System.out.println("连接成功!");
PreparedStatement stmt = conn
.prepareStatement("select * from emp where id=?");
ParameterMetaData pmd=stmt.getParameterMetaData();
System.out.println(pmd.getParameterCount());
System.out.println(pmd.getParameterType(1));
输出连接成功!
1
Exception in thread "main" java.sql.SQLException: Parameter metadata not available for the given statement
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.MysqlParameterMetadata.checkAvailable(MysqlParameterMetadata.java:66)
at com.mysql.jdbc.MysqlParameterMetadata.getParameterType(MysqlParameterMetadata.java:115)
at demo.OracleJdbcDemo.main(OracleJdbcDemo.java:26)
谁能告诉我什么原因。
改为
"jdbc:mysql://localhost:3306/ems?generateSimpleParameterMetadata=true"
看可行吗
String className = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/ems?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";
String userName = "root";
String password = "1234";
Connection connect = null;
if (className == "" || url == "" || userName == "") {
return null;
}
try {
Class.forName(className).newInstance();
connect = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
System.out.println("无法链接到数据库服务器!");
throw new SQLException();
}
return connect;
}
public static void step1() {
Connection connect=null;
ResultSet rs = null;
PreparedStatement ps = null;
String sql = "select * from emp limit 0,10";
try {
connect = getConnection();
ps = connect.prepareStatement(sql);
rs = ps.executeQuery();
while(rs.next()) {
String name=rs.getString("name");
System.out.println(name);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeConnection(connect);
}
}