Use eclipse in ubuntu to connect to mysql and ubuntueclipse

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.