[ MPesic @ 14.06.2016. 19:48 ] @
Jednostavno ne znam sta radim pogresno. U NetBeansu sam ucitao MySQL connector, nalazi se u Libraries folderu. Imam index.html stranicu i link koji poziva Driver servlet koji bi trebalo da izlista imena iz tabele Users. Trebalo bi da bude prosto ali mi javlja gresku java.sql.SQLException: No suitable driver found for mysql://localhost/laravel. Da napomenem da imam Wamp server Kod koji se nalazi u u Driver.java: Code: import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.*; public class Driver extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); response.setContentType("text/html;charset=UTF-8"); try { Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost/laravel", "root", ""); PreparedStatement mySt = myConn.prepareStatement("SELECT * FROM users"); ResultSet myRs = mySt.executeQuery(); while(myRs.next()) { String name = myRs.getString("name"); out.println("<h3>" + name + "</h3>"); out.println("</br>"); } } catch(Exception e) { out.println(e); } } } EDIT: Deluje kao da sam ishitreno trazio pomoc bez detaljnijeg pretrazivanja po internetu ali sam nakon 4-5 sati zakljucio da mi je potrebno pozvati Class.forName("com.mysql.jdbc.Driver"); pre Connection myConn = DriverManager.getConnection(...) EDIT 2: Samo ne kapiram jednu stvar. Probao sam da iscitam te iste podatke u konzoli koristeci kod koji iscitava isto. Code: package paket; import java.sql.*; public class Driver { public static void main(String[] args) { try { Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost/laravel", "root", ""); PreparedStatement mySt = myConn.prepareStatement("SELECT * FROM USERS"); ResultSet myRs = mySt.executeQuery(); while(myRs.next()) { System.out.println(myRs.getString("username") + ", " + myRs.getString("name")); } } catch (Exception e) { System.out.println(e); } } } U Eclipse-u se taj kod uspesno izvrsava i vraca podatke, dok je kod NetBeans-a potrebno dodati Class.forName("com.mysql.jdbc.Driver");. Kako i zasto? [Ovu poruku je menjao MPesic dana 14.06.2016. u 23:20 GMT+1] |