使用servlet和Java Bean訪問MySQL

來源:互聯網
上載者:User
mysql|servlet|訪問

行環境: JDK 5.0 + Tomcat 5.5.4

JDK安裝路徑: D:\Java\jdk1.5.0_01
Tomcat安裝路徑:D:\Tomcat 5.5

設定環境變數(控制台->系統->進階):
java_home=D:\Java\jdk1.5.0_01
path=%JAVA_HOME%\bin
classpath=.;D:\Java\jdk1.5.0_01\lib\dt.jar;D:\Java\jdk1.5.0_01\lib\tools.jar;D:\Tomcat 5.5\common\lib\servlet-api.jar;D:\Tomcat 5.5\common\lib\mysql-connector-java-3.1.6-bin.jar;


其中D:\Tomcat 5.5\common\lib\servlet-api.jar為Tomcat的servlet API實現,一定要加上,不如無法編譯servlet
D:\Tomcat 5.5\common\lib\mysql-connector-java-3.1.6-bin.jar;是MySQL的JDBC驅動,connect/J 3.1.6,可到MySQL官方網站下載

Tomcat5.5虛擬目錄設定:
D:\Tomcat 5.5\conf\Catalina\localhost下新加一test.xml
內容如下:
<Context path="/test" docBase="d:/www" reloadable="true" crossContext="true" debug="0" >
</Context>

d:\www路徑將是我們測試網站的根目錄,通過http://localhost:8080/test訪問該虛擬目錄
d:\www下建有目錄WEB-INF,下有web.xml設定檔案(可參考D:\Tomcat 5.5\webapps\ROOT\WEB-INF\web.xml),classes目錄和lib目錄

//串連資料庫的Java Bean檔案名稱dbconn.java
package NinGoo;
import java.sql.*;
public class dbconn {
public dbconn() {
}
//declare variable
private Connection conn = null;
ResultSet rs = null;
private String server = "127.0.0.1";
private String port = "3306";
private String db = "test";
private String user = "root";
private String pass = "password";
private String drivername="com.mysql.jdbc.Driver";
private String URL="jdbc:mysql://"+server+":"+port+"/"+db+"?user="+user+"&password="+pass;

public Connection getConn(){//get database connection
try{
Class.forName(drivername).newInstance();
conn = DriverManager.getConnection(URL);
}
catch(Exception e){
e.printStackTrace();
}
return this.conn ;
}

public ResultSet executeSQL(String str) {
try{
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(str);
}
catch(Exception e){
e.printStackTrace();
}
return this.rs;
}
}

編譯javac welcome.java,將編譯後的檔案welcome.class放到目錄D:\www\WEB-INF\classes\NinGoo\下

//調用Java Bean的servlet welcome.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;


public class welcome extends HttpServlet {
public welcome() {
}

public void doGet(HttpServletRequest Req,HttpServletResponse Res) throws ServletException,IOException {
Res.setContentType("text/html");

Connection conn=null;
Statement stmt=null;
ResultSet rs = null;

try{
NinGoo.database.dbconn myconn =new NinGoo.database.dbconn();
conn = myconn.getConn() ;
rs = myconn.executeSQL("select * from test"); //test是MySQL中的一個table
PrintWriter out=Res.getWriter();
out.println("<html><head><title>test</title></head>");
out.println("<body>");
while(rs.next()) {
out.println(rs.getString(1));
}
out.println("successfule!</body></html>");
}
catch(Exception e){
}
}
}
編譯javac welcome.java,將編譯後的檔案welcome.class放到目錄D:\www\WEB-INF\classes下
然後修改D:\www\WEB-INF\web.xml,加入如下設定
<servlet>
<servlet-name>welcome</servlet-name>
<servlet-class>welcome</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>welcome</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>

儲存後重啟Tomcat,然後在瀏覽器中敲入http://localhost:8080/test/welcome



作者Blog:http://blog.csdn.net/NinGoo/
相關文章
使用servlet和Java Bean訪問MySQL
JSP+Java Bean訪問MySQL資料庫
編寫第一個servlet
HOW TO:在 SQL Server 執行個體之間傳輸登入和密碼
jsp串連資料庫---Mysql



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.