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);
}
}
}
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);
}
}
}
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]