Use eclipse in ubuntu to connect to mysql and ubuntueclipse
Tip: Make sure your computer is connected to IOT platform.
We want to download something for a mysql-connector-java-5.0.8-bin.jar (of course the version of this jar package is not very relevant to your mysql version ), place the lib under the WEB-INF under the WebContent directory under the dynamicproject you created, so that the Web project can connect to the Mysql database. Official http://dev.mysql.com/downloads/connector/j/5.0.html
Then create a classdb_test.java In the src directory under javaresources in the project (for details, see the attachment)
Right-click runas-> java application to check if it is OK.
If OK, create jspfile index. jsp IN THE webcontent directory (for details, see the attachment)
Right-click the project and choose runas> run on server.
You can see your content in the browser. If an error is reported, you can try it several times. What is the possible cause. It may also be caused by version inconsistency, so you need to try it several more times.
For more information, see
Http://www.2cto.com/ OS /201504/392308.html
Db_test.java
ImportJava. SQL .*;
PublicClassDb_test {
PublicStaticVoidMain (String [] srg ){
Try{
Class.ForName("Com. mysql. jdbc. Driver"). newInstance ();
}Catch(InstantiationException | IllegalAccessException
| ClassNotFoundException e ){
//TODOAuto-generated catch block
E. printStackTrace ();
}
Connection conn =Null;
Try{
Conn = DriverManager.GetConnection("Jdbc: mysql: // localhost/mysql1", "root", "wtt561111 ");
}Catch(SQLException e ){
//TODOAuto-generated catch block
E. printStackTrace ();
}
Statement stmt =Null;
Try{
Stmt = conn. createStatement (ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
}Catch(SQLException e ){
//TODOAuto-generated catch block
E. printStackTrace ();
}
String SQL = "select * from student ";
Try{
ResultSetrs = stmt.exe cuteQuery (SQL );
While(Rs. next ()){
Stringsno = rs. getString (2 );
System.Out. Println (sno );
}
}Catch(SQLException e ){
//TODOAuto-generated catch block
E. printStackTrace ();
}
}
}
Index. jsp
<% @ PagecontentType ="Text/html; charset = gb2312"%>
<% @ Pageimport ="Java. SQL .*"%>
<Html>
<Body>
<%
Try{
Class. forName ("com. mysql. jdbc. Driver"). newInstance ();
}Catch(Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Connection conn =Null;
Try{
Conn = DriverManager. getConnection ("jdbc: mysql: // localhost/mysql1", "root", "wtt561111 ");
}Catch(SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Try{
If(Conn. equals (Null))
{Out. println ("nullerror ");}
Else{
// Stmt = conn. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
Statementstmt = conn. createStatement (ResultSet. TYPE_SCROLL_SENSITIVE, ResultSet. CONCUR_UPDATABLE );
Stringsql = "select * from student ";
ResultSetrs = stmt.exe cuteQuery (SQL );
While(Rs. next ()){
Stringsno = rs. getString (1 );
Out. println (sno );
}
}
}Catch(SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
%>
</Body>
</Html>
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.