Advertisement
Guest User

SQLUtil

a guest
Apr 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. import java.sql.*;
  2.  
  3. public class SQLUtil {
  4.  
  5.     public static synchronized String getHtmlRows(ResultSet results) throws SQLException {
  6.         StringBuffer htmlRows = new StringBuffer();
  7.         ResultSetMetaData metaData = results.getMetaData();
  8.         int columnCount = metaData.getColumnCount();
  9.         htmlRows.append("<table>");
  10.         htmlRows.append("<tr>");
  11.         for(int i = 1; i <= columnCount; i++)
  12.             htmlRows.append("<td><b>" + metaData.getColumnName(i) + "</b></td>");
  13.         htmlRows.append("</tr>");
  14.  
  15.         while(results.next()) {
  16.             htmlRows.append("<tr>");
  17.             for(int i = 1; i <= columnCount; i++)
  18.                 htmlRows.append("<td>" + results.getString(1) + "</td>");
  19.  
  20.             htmlRows.append("</tr>");
  21.         }
  22.         htmlRows.append("</table>");
  23.         return  htmlRows.toString();
  24.     }
  25.  
  26.     public static Connection getConnection() {
  27.         try{
  28.             Class.forName("com.mysql.jdbc.Driver");
  29.             String dbURL = "jdbc:mysql://localhost:3306/murach";
  30.             String username = "root";
  31.             String password = "";
  32.             Connection connection;
  33.             connection = DriverManager.getConnection(dbURL, username, password);
  34.             System.out.println("Connection successful: " + connection.toString());
  35.             return connection;
  36.         } catch (Exception e) {
  37.             e.printStackTrace();
  38.             return null;
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement