-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathExecSQL.java
More file actions
117 lines (104 loc) · 3.27 KB
/
Copy pathExecSQL.java
File metadata and controls
117 lines (104 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package exec;
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 ExecSQL
{
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();
}
}
}