2013年9月23日星期一

I am learning jsp jdbc part , write a small program Mysql connection errors. Please help heroes .

String url = "jdbc:mysql://localhost:3306/test";
Connection conn;
PrintWriter pw = response.getWriter();
Statement stmt;
try {
Class.forName("com.mysql.jdbc.Driver");
}catch(ClassNotFoundException e) {
pw.println("ClassNotFoundException");
pw.println(e.getMessage());
}

try {
conn = DriverManager.getConnection(url,"root","");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

java.sql.SQLException: No suitable driver found for jdbc: mysql :/ / localhost: 3306/test
at java.sql.DriverManager.getConnection (DriverManager.java: 602)
at java.sql.DriverManager.getConnection (DriverManager.java: 185)
at servlet.ConnJDBC.doPost (ConnJDBC.java: 40)
at javax.servlet.http.HttpServlet.service (HttpServlet.java: 637)
at javax.servlet.http.HttpServlet.service (HttpServlet.java: 717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (ApplicationFilterChain.java: 290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter (ApplicationFilterChain.java: 206)
at org.apache.catalina.core.StandardWrapperValve.invoke (StandardWrapperValve.java: 233)
at org.apache.catalina.core.StandardContextValve.invoke (StandardContextValve.java: 191)
at org.apache.catalina.core.StandardHostValve.invoke (StandardHostValve.java: 127)
at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java: 102)
at org.apache.catalina.core.StandardEngineValve.invoke (StandardEngineValve.java: 109)
at org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java: 298)
at org.apache.coyote.http11.Http11AprProcessor.process (Http11AprProcessor.java: 864)
at org.apache.coyote.http11.Http11AprProtocol $ Http11ConnectionHandler.process (Http11AprProtocol.java: 579)
at org.apache.tomcat.util.net.AprEndpoint $ Worker.run (AprEndpoint.java: 1665)
at java.lang.Thread.run (Thread.java: 662)
------ Solution -------------------- ------------------------
driver package can be judged not less problems . ( This problem will occur classnotfound exception)
driver package you put a place on the line, not all over the place , put multiple copies but prone jar package conflicts.
conn = DriverManager.getConnection (url, "root", ""); error is this one .
I have encountered this problem before , is the url, username or password is incorrect. . .
Another reflection is not lead pack oh
------ For reference only ------------------------- --------------
there is no driver package ? The location of the driver package is wrong with it ?
------ For reference only ------------------------------ ---------
package wrong to say that missing package, you pour the driver package , you can go to Baidu no next
------ For reference only ------ ---------------------------------
upstairs solution
If this is the obvious exception or error prompted content , generally will be prompted to content directly copy and paste into Baidu on OK
------ For reference only ------- --------------------------------
see an error, certainly not the driver package or wrong version of the driver package .
------ For reference only -------------------------------------- -
No suitable driver found
------ For reference only ------------------------- --------------
same code that I wrote in java class , in the console output can be performed , and can query the contents of the database . But writing in the servlet inside, executed in jsp output is wrong.
------ For reference only -------------------------------------- -

package should be no problem, I have copies of each location a . . You see my reply to the sixth floor .
------ For reference only -------------------------------------- -
import java.sql.DriverManager;
import java.sql.ResultSet;

public class Test {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/test";
java.sql.Connection conn = null;
java.sql.Statement statement = null;
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFound!");
System.out.println(e.getMessage());
}

try {
conn = DriverManager.getConnection(url, "root", "");
if(conn != null) {
System.out.println("connecte database!");
}
statement = conn.createStatement();
ResultSet rs = statement.executeQuery("select * from student");
while(rs.next()) {
String id = rs.getString(1);
String name = rs.getString("name");
String sex = rs.getString("sex");
int age = rs.getInt(4);
System.out.println(id+"-"+name+"-"+sex+"-"+age);
}
statement.close();
conn.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}

This code written in a java class, using the console output to get the correct results:
connecte database!
1-deity-f-21
2-yumo-m-23

Description database driver class and database connections are normal. But writing in the servlet , jsp call in the wrong . Says can not find the driver class . I put the driver class placed in tomcat 's lib , but also placed the webapps lib inside my website copy. In myeclipse project also introduced a
------ For reference only ---------------------------- -----------
jsp import package ?
------ For reference only -------------------------------------- -
netbeans?
------ For reference only -------------------------------------- -

're right, there is the issue of Baidu , of course, Google is a better choice
------ For reference only ---------- -----------------------------
driver problem, apparently

没有评论:

发表评论