java数据库问题,求高手解答!

李元静
HarmonyOS领域优质创作者
博客专家认证
2014-12-04 10:46:32
package com.example.mysql;

import java.io.*;
import java.nio.file.*;
import java.util.*;
import java.sql.*;

/**
* Executes all SQL statements in a file. Call this program as <br>
* java -classpath driverPath:. ExecSQL commandFile
*
* @version 1.31 2012-06-05
* @author Cay Horstmann
*/
class TestDB {
public static void main(String args[]) throws IOException {
try {
Scanner in = args.length == 0 ? new Scanner(System.in)
: new Scanner(Paths.get(args[0]));

try (Connection conn = getConnection()) {
Statement stat = conn.createStatement();

while (true) {
if (args.length == 0)
System.out.println("Enter command or EXIT to exit:");

if (!in.hasNextLine())
return;

String line = in.nextLine();
if (line.equalsIgnoreCase("EXIT"))
return;
if (line.trim().endsWith(";")) // remove trailing semicolon
{
line = line.trim();
line = line.substring(0, line.length() - 1);
}
try {
boolean isResult = stat.execute(line);
if (isResult) {
ResultSet rs = stat.getResultSet();
showResultSet(rs);
} else {
int updateCount = stat.getUpdateCount();
System.out.println(updateCount + " rows updated");
}
} catch (SQLException ex) {
for (Throwable e : ex)
e.printStackTrace();
}
}
}
} catch (SQLException e) {
for (Throwable t : e)
t.printStackTrace();
}
}

/**
* Gets a connection from the properties specified in the file
* database.properties
*
* @return the database connection
*/
public static Connection getConnection() throws SQLException, IOException {
Properties props = new Properties();
try (InputStream in = Files.newInputStream(Paths
.get("database.properties"))) {
props.load(in);
}

String drivers = props.getProperty("jdbc.drivers");
if (drivers != null)
System.setProperty("jdbc.drivers", drivers);

String url = props.getProperty("jdbc.url");
String username = props.getProperty("jdbc.username");
String password = props.getProperty("jdbc.password");

return DriverManager.getConnection(url, username, password);
}

/**
* Prints a result set.
*
* @param result
* the result set to be printed
*/
public static void showResultSet(ResultSet result) throws SQLException {
ResultSetMetaData metaData = result.getMetaData();
int columnCount = metaData.getColumnCount();

for (int i = 1; i <= columnCount; i++) {
if (i > 1)
System.out.print(", ");
System.out.print(metaData.getColumnLabel(i));
}
System.out.println();

while (result.next()) {
for (int i = 1; i <= columnCount; i++) {
if (i > 1)
System.out.print(", ");
System.out.print(result.getString(i));
}
System.out.println();
}
}
}
0 rows updated
java.sql.SQLNonTransientConnectionException: 遇到了网络协议错误,连接已终止:检测到协议错误(数据流语法错误)。原因:0x9,236。可能这是尝试与启用了 SSL 的服务器建立明文连接。
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.Statement.execute(Unknown Source)
at com.example.mysql.TestDB.main(TestDB.java:40)
Caused by: org.apache.derby.client.am.DisconnectException: 遇到了网络协议错误,连接已终止:检测到协议错误(数据流语法错误)。原因:0x9,236。可能这是尝试与启用了 SSL 的服务器建立明文连接。
at org.apache.derby.client.net.Reply.doSyntaxrmSemantics(Unknown Source)
at org.apache.derby.client.net.NetConnectionReply.parseSYNTAXRM(Unknown Source)
at org.apache.derby.client.net.NetConnectionReply.parseCommonError(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parsePrepareError(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.parsePRPSQLSTTreply(Unknown Source)
at org.apache.derby.client.net.NetStatementReply.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.net.StatementReply.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.net.NetStatement.readPrepareDescribeOutput_(Unknown Source)
at org.apache.derby.client.am.Statement.readPrepareDescribeOutput(Unknown Source)
at org.apache.derby.client.am.Statement.flowExecute(Unknown Source)
at org.apache.derby.client.am.Statement.executeX(Unknown Source)
... 2 more

database.properties文件内容:
jdbc.drivers=org.apache.derby.jdbc.ClientDriver
jdbc.url=jdbc:derby://localhost:1527/COREJAVA;create=true
jdbc.username=dbuser
jdbc.password=secret
args[0]的参数是文件名Publishers.sql
文件内容:
CREATE TABLE Publishe (Publisher_Id CHAR(6), Name CHAR(30), URL CHAR(80));
INSERT INTO Publishe VALUES ('0201', 'Addison-Wesley', 'www.aw-bc.com');
INSERT INTO Publishe VALUES ('0471', 'John Wiley & Sons', 'www.wiley.com');
INSERT INTO Publishe VALUES ('0262', 'MIT Press', 'mitpress.mit.edu');
INSERT INTO Publishe VALUES ('0596', 'O''Reilly', 'www.ora.com');
INSERT INTO Publishe VALUES ('019', 'Oxford University Press', 'www.oup.co.uk');
INSERT INTO Publishe VALUES ('013', 'Prentice Hall', 'www.phptr.com');
INSERT INTO Publishe VALUES ('0679', 'Random House', 'www.randomhouse.com');
INSERT INTO Publishe VALUES ('07434', 'Simon & Schuster', 'www.simonsays.com');
SELECT * FROM Publishe;
DROP TABLE Publishe;
...全文
221 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
可以把解决方案发出来吗,我也遇到同样的问题

50,504

社区成员

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

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