新手求助

orangewxy 2012-09-21 10:36:57
请问下面这段代码为什么会报错:
package com.tar;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class ConnectionUtils {

private static String url;
private static String driver;
private static String username;
private static String password;

static {
Properties props = new Properties();
try {
// 从属性文件中读取数据库配置信息
props.load(ConnectionUtils.class.getClassLoader()
.getResourceAsStream(
"db_oracle.properties"));
} catch (IOException e) {
e.printStackTrace();
}
if (props != null) {
url = props.getProperty("url");
driver = props.getProperty("driver");
username = props.getProperty("username");
password = props.getProperty("password");

// 装载并注册数据库驱动
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
}
}
}

public static Connection openConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}

public static void closeConnection(Connection con) {
try {
if (con != null) {
con.close();
}
} catch (SQLException e) {
}
}

public static void closeStatement(Statement stmt) {
try {
if (stmt != null) {
stmt.close();
}
} catch (SQLException e) {
}
}

public static void closeResultSet(ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException e) {
}
}

}

db_oracle.properties文件内容:
driver=oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@localhost:1521:w
name = scott
Password = tiger


错误代码:
java.sql.SQLException: ORA-06550: 第 1 行, 第 7 列:
PLS-00201: 必须声明标识符 'TOTAL_COUNT'
ORA-06550: 第 1 行, 第 7 列:
PL/SQL: Statement ignored

at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521)
at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:202)
at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1005)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1307)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3550)
at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4710)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1374)
at com.tar.TestCallableStmt.callSpWithOutParam(TestCallableStmt.java:35)
at com.tar.TestCallableStmt.main(TestCallableStmt.java:23)
...全文
117 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
orangewxy 2012-09-22
  • 打赏
  • 举报
回复
没有人回答啊
orangewxy 2012-09-21
  • 打赏
  • 举报
回复
下面这段代码引用了ConnectionUtil:
package com.tarena;

import java.sql.*;

public class TestBatch {

/**
* @param args
*/
public static void main(String[] args) {
batch();
}

public static void batch() {
Connection conn = null;
Statement stmt = null;
String sql1 = "insert into mystu"
+ " values(200, 'rose', 19, 'F')";
String sql2 = "insert into mystu"
+ " values(210, 'martha', 25, 'F')";
try {
conn = ConnectionUtils.openConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.addBatch(sql1);
stmt.addBatch(sql2);
int [] result = stmt.executeBatch();
conn.commit();
for (int i : result){
System.out.println(i);
}


} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtils.closeStatement(stmt);
ConnectionUtils.closeConnection(conn);
}
}
}
菖蒲老先生 2012-09-21
  • 打赏
  • 举报
回复
晕,我都不知道你的sql语句或存储过程,怎么改?
orangewxy 2012-09-21
  • 打赏
  • 举报
回复
回得挺快的,那我应该怎么修改呢?
菖蒲老先生 2012-09-21
  • 打赏
  • 举报
回复
com.tar.TestCallableStmt.callSpWithOutParam(TestCallableStmt.java:35)

看上面这行,不是你上面代码的问题,应该是你sql语句或者存储过程语法有问题,跟java代码无关。
orangewxy 2012-09-21
  • 打赏
  • 举报
回复
错误在配置文件db_oracle.properties文件内容:
driver=oracle.jdbc.driver.OracleDriver
url = jdbc:oracle:thin:@localhost:1521:w
name = scott
Password = tiger
在localhost和scott下面划了红线,直接在ConnectionUtils类中写
username ='scott'
password = 'tiger'问题解决。
我想问一下1.为什么配置文件里会划线,而直接写就没问题呢?
2.运行下了代码为什么会报错:
package com.tar;

import java.sql.*;

public class TestBatch {

/**
* @param args
*/
public static void main(String[] args) {
batch();
}

public static void batch() {
Connection conn = null;
Statement stmt = null;
String sql1 = "insert into emp"
+ " values(200, 'rose', , 3000,20)";

try {
conn = ConnectionUtils.openConnection();
conn.setAutoCommit(false);
stmt = conn.createStatement();
stmt.addBatch(sql1);

int [] result = stmt.executeBatch();
conn.commit();
for (int i : result){
System.out.println(i);
}


} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtils.closeStatement(stmt);
ConnectionUtils.closeConnection(conn);
}
}
}
错误是:批处理中出现错误: ORA-00936: 缺失表达式

50,549

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧