ubuntu下eclipse串連mysql,ubuntueclipse

來源:互聯網
上載者:User

ubuntu下eclipse串連mysql,ubuntueclipse

提示:一定要保證電腦處於連網狀態


我們要下載一個mysql-connector-java-5.0.8-bin.jar的東西(當然這個jar包的版本和你的mysql版本的關係不是很大),放到你建立的dynamicproject下的WebContent目錄下的WEB-INF下的lib,讓這個Web工程能夠串連Mysql資料庫。官網http://dev.mysql.com/downloads/connector/j/5.0.html


之後在工程中的javaresources下的src目錄建立一個classdb_test.java(具體內容見附件)

右鍵runas-> java application,看是否已經ok了


如果ok的話,在webcontent目錄下建立jspfile index.jsp(具體內容見附件)

右鍵工程->runas ->run on server


應該可以在瀏覽器中看到你的內容了。如果報錯,可以多嘗試下幾遍,可能是哪個環節出現了什麼問題。也可能是版本不一致導致的,所以需要你多試幾次。

內容可參考

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.executeQuery(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.executeQuery(sql);

while(rs.next()){

Stringsno=rs.getString(1);

out.println(sno);

}

}

}catch(SQLException e) {

//TODO Auto-generated catch block

e.printStackTrace();

}


%>

</body>

</html>


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.