List of usage examples for java.sql ResultSet next
boolean next() throws SQLException;
From source file:PrintResultSet.java
public static void main(String args[]) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Inventory"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select * from employee"); ResultSetMetaData md = rs.getMetaData(); int nColumns = md.getColumnCount(); for (int i = 1; i <= nColumns; i++) { System.out.print(md.getColumnLabel(i) + ((i == nColumns) ? "\n" : "\t")); }//from w w w.ja v a2 s .co m while (rs.next()) { for (int i = 1; i <= nColumns; i++) { System.out.print(rs.getString(i) + ((i == nColumns) ? "\n" : "\t")); } } }
From source file:PrintIndexedResultSet.java
public static void main(String args[]) throws Exception { String query = "SELECT STATE, COUNT(STATE) FROM MEMBER_PROFILES GROUP BY STATE"; Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection("jdbc:odbc:Members"); Statement stmt = con.createStatement(); stmt.executeUpdate("CREATE INDEX STATE_INDEX ON MEMBER_PROFILES(STATE)"); java.util.Date startTime = new java.util.Date(); ResultSet rs = stmt.executeQuery(query); ResultSetMetaData md = rs.getMetaData(); int nColumns = md.getColumnCount(); for (int i = 1; i <= nColumns; i++) { System.out.print(md.getColumnLabel(i) + ((i == nColumns) ? "\n" : "\t")); }//from ww w . j a v a 2s . com while (rs.next()) { for (int i = 1; i <= nColumns; i++) { System.out.print(rs.getString(i) + ((i == nColumns) ? "\n" : "\t")); } } java.util.Date endTime = new java.util.Date(); long elapsedTime = endTime.getTime() - startTime.getTime(); System.out.println("Elapsed time: " + elapsedTime); stmt.executeUpdate("DROP INDEX MEMBER_PROFILES.STATE_INDEX"); }
From source file:Main.java
public static void main(String[] args) throws Exception { Connection conn = getConnection(); Statement st = conn.createStatement(); st.executeUpdate("create table survey (id int,myDate DATE );"); String INSERT_RECORD = "insert into survey(id, myDate) values(?, ?)"; PreparedStatement pstmt = conn.prepareStatement(INSERT_RECORD); pstmt.setString(1, "1"); java.sql.Date sqlDate = new java.sql.Date(new java.util.Date().getTime()); pstmt.setDate(2, sqlDate);//from www. j a va 2s.com pstmt.executeUpdate(); ResultSet rs = st.executeQuery("SELECT * FROM survey"); Calendar cal = Calendar.getInstance(); // get the TimeZone for "America/Los_Angeles" TimeZone tz = TimeZone.getTimeZone("America/Los_Angeles"); cal.setTimeZone(tz); while (rs.next()) { // the JDBC driver will use the time zone information in // Calendar to calculate the date, with the result that // the variable dateCreated contains a java.sql.Date object // that is accurate for "America/Los_Angeles". java.sql.Date dateCreated = rs.getDate(2, cal); System.out.println(dateCreated); } rs.close(); st.close(); conn.close(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); ResultSet rs = DriverManager.getConnection("jdbc:odbc:employee_xls").createStatement() .executeQuery("Select * from [Sheet1$]"); ResultSetMetaData rsmd = rs.getMetaData(); int numberOfColumns = rsmd.getColumnCount(); System.out.println("No of cols " + numberOfColumns); while (rs.next()) { for (int i = 1; i <= numberOfColumns; i++) { String columnValue = rs.getString(i); System.out.println(columnValue); }/*from w w w. j a v a 2s .c om*/ } rs.close(); }
From source file:GetDateFromOracle.java
public static void main(String args[]) { String GET_RECORD = "select date_column, time_column, " + "timestamp_column from TestDates where id = ?"; ResultSet rs = null; Connection conn = null;/*w w w . j av a 2 s. c o m*/ PreparedStatement pstmt = null; try { conn = getConnection(); pstmt = conn.prepareStatement(GET_RECORD); pstmt.setString(1, "0001"); rs = pstmt.executeQuery(); while (rs.next()) { java.sql.Date dbSqlDate = rs.getDate(1); java.sql.Time dbSqlTime = rs.getTime(2); java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3); System.out.println("dbSqlDate=" + dbSqlDate); System.out.println("dbSqlTime=" + dbSqlTime); System.out.println("dbSqlTimestamp=" + dbSqlTimestamp); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:History.PieChart_DB.java
public static void main(String[] args) throws Exception { String mobilebrands[] = { "IPhone 5s", "SamSung Grand", "MotoG", "Nokia Lumia" }; int statOfRepair = 0; java.util.Date now = new java.util.Date(); String adminDate = (now.getYear() + 1900) + "-" + (now.getMonth() + 1) + "-" + now.getDate(); /* Create MySQL Database Connection */ Class.forName("com.mysql.jdbc.Driver"); Connection connect = ConnectDatabase.connectDb("win", "win016"); Statement statement = connect.createStatement(); ResultSet resultSet = statement .executeQuery("SELECT COUNT(transID) AS statRepair FROM `Transaction` WHERE dateTime LIKE \'" + adminDate + "%\' AND action LIKE 'Repair'"); DefaultPieDataset dataset = new DefaultPieDataset(); while (resultSet.next()) { dataset.setValue(resultSet.getString("statRepair"), Double.parseDouble(resultSet.getString("unit_sale"))); }//from w w w .j a v a 2 s . c om JFreeChart chart = ChartFactory.createPieChart("History", // chart title dataset, // data true, // include legend true, false); int width = 560; /* Width of the image */ int height = 370; /* Height of the image */ File pieChart = new File("Pie_Chart.jpeg"); ChartUtilities.saveChartAsJPEG(pieChart, chart, width, height); }
From source file:ex4.java
public static void main(String[] params) { CommandLine commandLine = null;/* w ww . ja va 2s. c o m*/ String sqlpath = "", host = "", port = "3306", username = "", password = "", database = ""; Boolean query = false; Option option_sql = Option.builder("s").argName("sql").hasArg() .desc("Path to a file containing a valid MySQL sql statement").build(); Option option_hostname = Option.builder("h").argName("host").hasArg().desc("ClearDB MySQL Hostname") .build(); Option option_port = Option.builder("n").argName("port").hasArg().desc("ClearDB MySQL Port").build(); Option option_username = Option.builder("u").argName("username").hasArg().desc("ClearDB MySQL Username") .build(); Option option_password = Option.builder("p").argName("password").hasArg().desc("ClearDB MySQL Password") .build(); Option option_dbname = Option.builder("d").argName("dbname").hasArg().desc("ClearDB MySQL Database Name") .build(); Option option_help = Option.builder("w").argName("wanthelp").hasArg().desc("Help").build(); Option option_query = Option.builder().longOpt("query").desc("Query type SQL Statement").build(); Options options = new Options(); CommandLineParser parser = new DefaultParser(); options.addOption(option_sql); options.addOption(option_hostname); options.addOption(option_port); options.addOption(option_username); options.addOption(option_password); options.addOption(option_dbname); options.addOption(option_query); options.addOption(option_help); try { commandLine = parser.parse(options, params); } catch (MissingOptionException e) { help(options); } catch (MissingArgumentException e) { help(options); } catch (ParseException e) { System.out.println(e); } if (commandLine.hasOption("w") || params.length == 0) { help(options); } if (commandLine.hasOption("s")) { sqlpath = commandLine.getOptionValue("s"); } else { System.out.println("Missing path to a SQL statement file"); help(options); } if (commandLine.hasOption("h")) { host = commandLine.getOptionValue("h"); } else { System.out.println("Missing ClearDB hostname (e.g. us-cdbr-iron-east-??.cleardb.net)"); help(options); } if (commandLine.hasOption("n")) { port = commandLine.getOptionValue("n"); } else { System.out.println("Missing ClearDB Port Value. Defaulting to 3306"); } if (commandLine.hasOption("u")) { username = commandLine.getOptionValue("u"); } else { System.out.println("Missing ClearDB Username"); help(options); } if (commandLine.hasOption("p")) { password = commandLine.getOptionValue("p"); } else { System.out.println("Missing ClearDB Password"); help(options); } if (commandLine.hasOption("d")) { database = commandLine.getOptionValue("d"); } else { System.out.println("Missing ClearDB Database Name"); help(options); } if (commandLine.hasOption("query")) { query = true; } String connectionURL = new StringBuilder().append("jdbc:mysql://").append(host).append(":").append(port) .append("/").append(database).append("?reconnect=true").toString(); try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println(e); } try { Connection con = DriverManager.getConnection(connectionURL, username, password); Statement stmt = con.createStatement(); if (query) { System.out.println("Querying target MySQL DB ..."); ResultSet rs = stmt.executeQuery(readFile(sqlpath, Charset.defaultCharset())); while (rs.next()) System.out.println(rs.getInt("emp_no") + " " + rs.getDate("birth_date") + " " + rs.getString("first_name") + " " + rs.getString("last_name") + " " + rs.getString("gender") + " " + rs.getDate("hire_date")); } else { System.out.println("Updating target MySQL DB ..."); int result = stmt.executeUpdate(readFile(sqlpath, Charset.defaultCharset())); System.out.println(result); } con.close(); } catch (Exception e) { System.out.println(e); } }
From source file:movierecommend.MovieRecommend.java
public static void main(String[] args) throws ClassNotFoundException, SQLException { String url = "jdbc:sqlserver://localhost;databaseName=MovieDB;integratedSecurity=true"; Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); Connection conn = DriverManager.getConnection(url); Statement stm = conn.createStatement(); ResultSet rsRecnik = stm.executeQuery("SELECT Recnik FROM Recnik WHERE (ID_Zanra = 1)"); //citam recnik iz baze za odredjeni zanr String recnik[] = null;//from w w w . j a va 2 s .co m while (rsRecnik.next()) { recnik = rsRecnik.getString("Recnik").split(","); //delim recnik na reci } ResultSet rsFilmovi = stm.executeQuery( "SELECT TOP (200) Naziv_Filma, LemmaPlots, " + "ID_Filma FROM Film WHERE (ID_Zanra = 1)"); List<Film> listaFilmova = new ArrayList<>(); Film f = null; int rb = 0; while (rsFilmovi.next()) { f = new Film(rb, Integer.parseInt(rsFilmovi.getString("ID_Filma")), rsFilmovi.getString("Naziv_Filma"), rsFilmovi.getString("LemmaPlots")); listaFilmova.add(f); rb++; } //kreiranje vektorskog modela M = MatrixUtils.createRealMatrix(recnik.length, listaFilmova.size()); System.out.println("Prva tezinska matrica"); for (int i = 0; i < recnik.length; i++) { String recBaza = recnik[i]; for (Film film : listaFilmova) { for (String lemmaRec : film.getPlotLema()) { if (recBaza.equals(lemmaRec)) { M.setEntry(i, film.getRb(), M.getEntry(i, film.getRb()) + 1); } } } } //racunanje tf-idf System.out.println("td-idf"); M = LSA.calculateTfIdf(M); System.out.println("SVD"); //SVD SingularValueDecomposition svd = new SingularValueDecomposition(M); RealMatrix V = svd.getV(); RealMatrix Vk = V.getSubMatrix(0, V.getRowDimension() - 1, 0, brojDimenzija - 1); //dimenzija je poslednji argument //kosinusna slicnost System.out.println("Cosin simmilarity"); CallableStatement stmTop = conn.prepareCall("{call Dodaj_TopList(?,?,?)}"); for (int j = 0; j < listaFilmova.size(); j++) { Film fl = listaFilmova.get(j); List<Film> lFilmova1 = new ArrayList<>(); lFilmova1.add(listaFilmova.get(j)); double sim = 0.0; for (int k = 0; k < listaFilmova.size(); k++) { // System.out.println(listaFilmova.size()); sim = LSA.cosinSim(j, k, Vk.transpose()); listaFilmova.get(k).setSimilarity(sim); lFilmova1.add(listaFilmova.get(k)); } Collections.sort(lFilmova1); for (int k = 2; k < 13; k++) { stmTop.setString(1, fl.getID() + ""); stmTop.setString(2, lFilmova1.get(k).getID() + ""); stmTop.setString(3, lFilmova1.get(k).getSimilarity() + ""); stmTop.execute(); } } stm.close(); rsRecnik.close(); rsFilmovi.close(); conn.close(); }
From source file:CountRecordsUsingPreparedStatement.java
public static void main(String[] args) { ResultSet rs = null; Connection conn = null;//from www. j a va 2 s . c o m PreparedStatement pstmt = null; try { conn = getConnection(); String query = "select count(*) from tableName"; pstmt = conn.prepareStatement(query); rs = pstmt.executeQuery(); if (rs.next()) { int numberOfRows = rs.getInt(1); System.out.println("numberOfRows= " + numberOfRows); } else { System.out.println("error: could not get the record counts"); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); pstmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:GetDateFromMySql.java
public static void main(String args[]) { ResultSet rs = null; Connection conn = null;//w w w.j ava 2 s. c o m Statement stmt = null; try { conn = getMySQLConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery("select timeCol, dateCol, dateTimeCol from dateTimeTable"); while (rs.next()) { java.sql.Time dbSqlTime = rs.getTime(1); java.sql.Date dbSqlDate = rs.getDate(2); java.sql.Timestamp dbSqlTimestamp = rs.getTimestamp(3); System.out.println("dbSqlTime=" + dbSqlTime); System.out.println("dbSqlDate=" + dbSqlDate); System.out.println("dbSqlTimestamp=" + dbSqlTimestamp); java.util.Date dbSqlTimeConverted = new java.util.Date(dbSqlTime.getTime()); java.util.Date dbSqlDateConverted = new java.util.Date(dbSqlDate.getTime()); System.out.println("in standard date"); System.out.println(dbSqlTimeConverted); System.out.println(dbSqlDateConverted); } } catch (Exception e) { e.printStackTrace(); } finally { try { rs.close(); stmt.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }