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>
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。