Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.82 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import javax.swing.border.*;
  5. import java.sql.*;
  6. import java.util.*;
  7.  
  8. public class SQLClient extends JApplet {
  9. // Connection to the database
  10. private Connection connection;
  11.  
  12. // Statement to execute SQL commands
  13. private Statement statement;
  14.  
  15. // Text area to enter SQL commands
  16. private JTextArea jtasqlCommand = new JTextArea();
  17.  
  18. // Text area to display results from SQL commands
  19. private JTextArea jtaSQLResult = new JTextArea();
  20.  
  21. // JDBC info for a database connection
  22. JTextField jtfUsername = new JTextField();
  23. JPasswordField jpfPassword = new JPasswordField();
  24. JComboBox jcboURL = new JComboBox(new String[] {
  25. "jdbc:sqlserver://s16988308.onlinehome-server.com:1433;databaseName=CUNY_DB;integratedSecurity=false",
  26. "jdbc:mysql://liang.armstrong.edu/javabook",
  27. "jdbc:odbc:exampleMDBDataSource",
  28. "jdbc:oracle:thin:@liang.armstrong.edu:1521:orcl"});
  29. JComboBox jcboDriver = new JComboBox(new String[] {
  30. "com.microsoft.sqlserver.jdbc.SQLServerDriver",
  31. "com.mysql.jdbc.Driver", "sun.jdbc.odbc.JdbcOdbcDriver",
  32. "oracle.jdbc.driver.OracleDriver"});
  33.  
  34. JButton jbtExecuteSQL = new JButton("Execute SQL Command");
  35. JButton jbtClearSQLCommand = new JButton("Clear");
  36. JButton jbtConnectDB1 = new JButton("Connect to Database");
  37. JButton jbtClearSQLResult = new JButton("Clear Result");
  38.  
  39. // Create titled borders
  40. Border titledBorder1 = new TitledBorder("Enter an SQL Command");
  41. Border titledBorder2 = new TitledBorder("SQL Execution Result");
  42. Border titledBorder3 = new TitledBorder(
  43. "Enter Database Information");
  44.  
  45. JLabel jlblConnectionStatus = new JLabel("No connection now");
  46.  
  47. /** Initialize the applet */
  48. public void init() {
  49. JScrollPane jScrollPane1 = new JScrollPane(jtasqlCommand);
  50. jScrollPane1.setBorder(titledBorder1);
  51. JScrollPane jScrollPane2 = new JScrollPane(jtaSQLResult);
  52. jScrollPane2.setBorder(titledBorder2);
  53.  
  54. JPanel jPanel1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
  55. jPanel1.add(jbtClearSQLCommand);
  56. jPanel1.add(jbtExecuteSQL);
  57.  
  58. JPanel jPanel2 = new JPanel();
  59. jPanel2.setLayout(new BorderLayout());
  60. jPanel2.add(jScrollPane1, BorderLayout.CENTER);
  61. jPanel2.add(jPanel1, BorderLayout.SOUTH);
  62. jPanel2.setPreferredSize(new Dimension(100, 100));
  63.  
  64. JPanel jPanel3 = new JPanel();
  65. jPanel3.setLayout(new BorderLayout());
  66. jPanel3.add(jlblConnectionStatus, BorderLayout.CENTER);
  67. jPanel3.add(jbtConnectDB1, BorderLayout.EAST);
  68.  
  69. JPanel jPanel4 = new JPanel();
  70. jPanel4.setLayout(new GridLayout(4, 1, 10, 5));
  71. jPanel4.add(jcboDriver);
  72. jPanel4.add(jcboURL);
  73. jPanel4.add(jtfUsername);
  74. jPanel4.add(jpfPassword);
  75.  
  76. JPanel jPanel5 = new JPanel();
  77. jPanel5.setLayout(new GridLayout(4, 1));
  78. jPanel5.add(new JLabel("JDBC Driver"));
  79. jPanel5.add(new JLabel("Database URL"));
  80. jPanel5.add(new JLabel("Username"));
  81. jPanel5.add(new JLabel("Password"));
  82.  
  83. JPanel jPanel6 = new JPanel();
  84. jPanel6.setLayout(new BorderLayout());
  85. jPanel6.setBorder(titledBorder3);
  86. jPanel6.add(jPanel4, BorderLayout.CENTER);
  87. jPanel6.add(jPanel5, BorderLayout.WEST);
  88.  
  89. JPanel jPanel7 = new JPanel();
  90. jPanel7.setLayout(new BorderLayout());
  91. jPanel7.add(jPanel3, BorderLayout.SOUTH);
  92. jPanel7.add(jPanel6, BorderLayout.CENTER);
  93.  
  94. JPanel jPanel8 = new JPanel();
  95. jPanel8.setLayout(new BorderLayout());
  96. jPanel8.add(jPanel2, BorderLayout.CENTER);
  97. jPanel8.add(jPanel7, BorderLayout.WEST);
  98.  
  99. JPanel jPanel9 = new JPanel(new FlowLayout(FlowLayout.LEFT));
  100. jPanel9.add(jbtClearSQLResult);
  101.  
  102. jcboURL.setEditable(true);
  103. jcboDriver.setEditable(true);
  104.  
  105. add(jPanel8, BorderLayout.NORTH);
  106. add(jScrollPane2, BorderLayout.CENTER);
  107. add(jPanel9, BorderLayout.SOUTH);
  108.  
  109. jbtExecuteSQL.addActionListener(new ActionListener() {
  110. // @Override
  111. public void actionPerformed(ActionEvent e) {
  112. executeSQL();
  113. }
  114. });
  115. jbtConnectDB1.addActionListener(new ActionListener() {
  116. // @Override
  117. public void actionPerformed(ActionEvent e) {
  118. connectToDB();
  119. }
  120. });
  121. jbtClearSQLCommand.addActionListener(new ActionListener() {
  122. //@Override
  123. public void actionPerformed(ActionEvent e) {
  124. jtasqlCommand.setText(null);
  125. }
  126. });
  127. jbtClearSQLResult.addActionListener(new ActionListener() {
  128. //@Override
  129. public void actionPerformed(ActionEvent e) {
  130. jtaSQLResult.setText(null);
  131. }
  132. });
  133. }
  134.  
  135. /** Connect to DB */
  136. private void connectToDB() {
  137. // Get database information from the user input
  138. String driver = (String)jcboDriver.getSelectedItem();
  139. String url = (String)jcboURL.getSelectedItem();
  140. String username = jtfUsername.getText().trim();
  141. String password = new String(jpfPassword.getPassword());
  142.  
  143. // Connection to the database
  144. try {
  145. Class.forName(driver);
  146. connection = DriverManager.getConnection(
  147. url, username, password);
  148. jlblConnectionStatus.setText("Connected to " + url);
  149. }
  150. catch (java.lang.Exception ex) {
  151. ex.printStackTrace();
  152. }
  153. }
  154.  
  155. /** Execute SQL commands */
  156. private void executeSQL() {
  157. if (connection == null) {
  158. jtaSQLResult.setText("Please connect to a database first");
  159. return;
  160. }
  161. else {
  162. String sqlCommands = jtasqlCommand.getText().trim();
  163. String[] commands = sqlCommands.replace('\n', ' ').split(";");
  164.  
  165. for (String aCommand: commands) {
  166. if (aCommand.trim().toUpperCase().startsWith("SELECT")) {
  167. processSQLSelect(aCommand);
  168. }
  169. else {
  170. processSQLNonSelect(aCommand);
  171. }
  172. }
  173. }
  174. }
  175.  
  176. /** Execute SQL SELECT commands */
  177. private void processSQLSelect(String sqlCommand) {
  178. try {
  179. // Get a new statement for the current connection
  180. statement = connection.createStatement();
  181.  
  182. // Execute a SELECT SQL command
  183. ResultSet resultSet = statement.executeQuery(sqlCommand);
  184.  
  185. // Find the number of columns in the result set
  186. int columnCount = resultSet.getMetaData().getColumnCount();
  187. String row = "";
  188.  
  189. // Display column names
  190. for (int i = 1; i <= columnCount; i++) {
  191. row += resultSet.getMetaData().getColumnName(i) + "\t";
  192. }
  193.  
  194. jtaSQLResult.append(row + '\n');
  195.  
  196. while (resultSet.next()) {
  197. // Reset row to empty
  198. row = "";
  199.  
  200. for (int i = 1; i <= columnCount; i++) {
  201. // A non-String column is converted to a string
  202. row += resultSet.getString(i) + "\t";
  203. }
  204.  
  205. jtaSQLResult.append(row + '\n');
  206. }
  207. }
  208. catch (SQLException ex) {
  209. jtaSQLResult.setText(ex.toString());
  210. }
  211. }
  212.  
  213. /** Execute SQL DDL, and modification commands */
  214. private void processSQLNonSelect(String sqlCommand) {
  215. try {
  216. // Get a new statement for the current connection
  217. statement = connection.createStatement();
  218.  
  219. // Execute a non-SELECT SQL command
  220. statement.executeUpdate(sqlCommand);
  221.  
  222. jtaSQLResult.setText("SQL command executed");
  223. }
  224. catch (SQLException ex) {
  225. jtaSQLResult.setText(ex.toString());
  226. }
  227. }
  228.  
  229. /** Main method */
  230. public static void main(String[] args) {
  231. SQLClient applet = new SQLClient();
  232. JFrame frame = new JFrame();
  233. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  234. frame.setTitle("Interactive SQL Client");
  235. frame.getContentPane().add(applet, BorderLayout.CENTER);
  236. applet.init();
  237. applet.start();
  238. frame.setSize(800, 320);
  239. Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
  240. frame.setLocation((d.width - frame.getSize().width) / 2,
  241. (d.height - frame.getSize().height) / 2);
  242. frame.setVisible(true);
  243. }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement