public int getPrimaryID(String userName) { Connection koneksi = null; Statement stat = null; String str = ""; int iD = 0; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); koneksi = DriverManager.getConnection("jdbc:mysql://localhost/shopkartdb", "shopkart", "welcome"); System.out.println(koneksi); stat = koneksi.createStatement(); ResultSet hasil = stat.executeQuery("SELECT iD FROM loginInfo where userName='******'"); if (hasil.next()) iD = hasil.getInt(1); stat.close(); koneksi.close(); } catch (SQLException sqle) { str = "SQLException error"; } catch (ClassNotFoundException cnfe) { str = "ClassNotFoundException error"; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } System.out.println(str); return iD; }
public String checkUserOrEmailexit(String userName, String email) { Connection koneksi = null; Statement stat = null; String str = "Accepted"; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); koneksi = DriverManager.getConnection("jdbc:mysql://localhost/shopkartdb", "shopkart", "welcome"); System.out.println(koneksi); stat = koneksi.createStatement(); ResultSet hasil = stat.executeQuery( "SELECT userName, emailID FROM loginInfo WHERE userName='******' OR emailID='" + email + "';"); if (hasil.next()) str = "Rejected"; stat.close(); koneksi.close(); } catch (SQLException sqle) { str = "SQLException error"; } catch (ClassNotFoundException cnfe) { str = "ClassNotFoundException error"; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return str; }
public void addPasswordToDataBase(String password, int iD) { Connection koneksi = null; PreparedStatement pstmt = null; String str = ""; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); koneksi = DriverManager.getConnection("jdbc:mysql://localhost/shopkartdb", "shopkart", "welcome"); System.out.println("I am Here " + koneksi); String sql = "INSERT INTO loginPass(" + "iD," + "passWord) " + "VALUES(?,?)"; pstmt = koneksi.prepareStatement(sql); pstmt.setInt(1, iD); pstmt.setString(2, password); pstmt.executeUpdate(); System.out.println("Hello"); koneksi.close(); } catch (SQLException sqle) { str = "SQLException error"; } catch (ClassNotFoundException cnfe) { str = "ClassNotFoundException error"; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
public String selectionQuery() { Connection koneksi = null; Statement stat = null; String str = ""; try { Class.forName("com.mysql.jdbc.Driver").newInstance(); koneksi = DriverManager.getConnection("jdbc:mysql://localhost/shopkartdb", "shopkart", "welcome"); System.out.println(koneksi); stat = koneksi.createStatement(); ResultSet hasil = stat.executeQuery("SELECT * FROM loginInfo"); while (hasil.next()) { str = str + (hasil.getString(1)) + hasil.getString(2); } stat.close(); koneksi.close(); } catch (SQLException sqle) { str = "SQLException error"; } catch (ClassNotFoundException cnfe) { str = "ClassNotFoundException error"; } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return str; }
@Override public boolean connect() { try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance(); String url = "jdbc:sqlserver://" + Config.DATABASE_IP + ":" + Config.DATABASE_PORT + ";databaseName=Narda"; conn = DriverManager.getConnection(url, Config.DATABASE_USERNAME, Config.DATABASE_PASSWORD); OrbitStamps.log(OrbitStamps.LOG_NOTICE, "HuddingeDataMapper: connection success!"); return true; } catch (ClassNotFoundException ex) { System.err.println(ex.getMessage()); } catch (IllegalAccessException ex) { System.err.println(ex.getMessage()); } catch (InstantiationException ex) { System.err.println(ex.getMessage()); } catch (SQLException ex) { System.err.println(ex.getMessage()); } OrbitStamps.log(OrbitStamps.LOG_NOTICE, "HuddingeDataMapper: connection failed!"); return false; }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub // TODO Auto-generated method stub HttpSession session = request.getSession(); request.setCharacterEncoding("UTF-8"); BufferedInputStream fileIn = new BufferedInputStream(request.getInputStream()); String fn = request.getParameter("fileName"); byte[] buf = new byte[1024]; File file = new File("/var/www/uploadres/" + session.getAttribute("username") + fn); BufferedOutputStream fileOut = new BufferedOutputStream(new FileOutputStream(file)); while (true) { int bytesIn = fileIn.read(buf, 0, 1024); if (bytesIn == -1) break; else fileOut.write(buf, 0, bytesIn); } fileOut.flush(); fileOut.close(); System.out.println(file.getAbsolutePath()); try { Class.forName("com.mysql.jdbc.Driver").newInstance(); try { Connection conn = DriverManager.getConnection(url, user, pwd); Statement stmt = conn.createStatement(); String sql = "UPDATE Users SET photo = '" + file.getName() + "' WHERE username='******'"; stmt.execute(sql); // PreparedStatement pstmt = conn.prepareStatement("UPDATE Users SET photo = ? WHERE // username='******'"); // InputStream inp = new BufferedInputStream(new FileInputStream(file)); // pstmt.setBinaryStream(1, inp, (int)file.length()); // pstmt.executeUpdate(); System.out.println("OK"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public String execute() { HttpSession session = ServletActionContext.getRequest().getSession(); if (photo == null) return "setinfo"; String uploadPath = "/var/www/uploadres"; String photoName = session.getAttribute("username") + this.getPhotoFileName(); File toPhotoFile = new File(new File(uploadPath), photoName); if (!toPhotoFile.getParentFile().exists()) toPhotoFile.getParentFile().mkdirs(); try { FileUtils.copyFile(photo, toPhotoFile); try { Class.forName("com.mysql.jdbc.Driver").newInstance(); try { Connection conn = DriverManager.getConnection(url, user, pwd); Statement stmt = conn.createStatement(); String sql = "UPDATE Users SET photo = '" + photoName + "' WHERE username='******'"; stmt.execute(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return "setinfo"; }
@Test public void estConnection() throws IllegalAccessException, ClassNotFoundException, SQLException { Connection conn = null; String url = "jdbc:mysql://localhost:3306/"; String driver = "com.mysql.jdbc.Driver"; String dbName = "sakila"; String userName = "******"; String password = "******"; try { // This will create Object of Driver class Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url + dbName, userName, password); } catch (InstantiationException e) { System.out.println("Connection failed"); e.printStackTrace(); } }