JBuilder2008下操作MYSQL教程

想来真是好笑,前几天还在学32位的汇编,转眼又跑java上来了,我这人永远是这么的好高骛远,真哪自己没办法!!!好了,闲话不说了,这几天正在看浙江大学钱晓军的“中间件技术”(重点讲j2ee),对初学者来说是个不错的视频教程。里面讲到了用JDBC操作MYSQL的技术,是基于JBuild2006开发的。由于我用的是JBuild2008,而且所用的JDBC Concepts也是最新的mysql-connector-java-5.1.7-bin.jar,因此操作上有所不同。

首先,我们需要构建一个软件柜架:

1.file->new->project->Web->Dynamic Web Project 建立一个Web站点;

2.向工程中添加一个名为"index.jsp"的jsp文件,其中代码见附一;

3.向工程中添加一个名为"MyServlet"的Servlet,并将其选择放在myServlet包下,代码见附二;

4.当然这样肯定是不行的,因为缺一个Mysql驱动。(显然,JAVA生下来是不知道怎么和MYSQL交流的,这就是为什么需要驱动的原因!),那么请到http://dev.mysql.com/downloads/下载mysql-connector-java-5.1.7-bin.jar(当然你可以选择旧版,但这将会导致我不解理你的动机 呵呵!)所幸把教程做的更傻瓜式一点,我要你下载的其实是"MySQL Connector/J — for connecting to MySQL from Java"下的"MySQL Connector/J 5.1 ";

5.解压文件到任何你喜欢的地方(除了回收站);

6.在工程名中点击右键->properties->J2EE Module Dependencies->Add External JARS->选择你刚才解压出来的mysql-connector-java-5.1.7-bin.jar->搞定!

7.启动工程->Run As->Run On Server->选择TomCat6.0为服务器->教程结束!

注意:

请把下面红字部分改成你的实际设置!

具体如何用JDBC操作MYSQL,在MYSQL官方网上有最权威的说明!可以到WWW.MYSQL.COM上搜索JDBC,将出来多得让你开始郁闷的帮助信息,对初学者来说我推荐下面几篇文章:http://dev.mysql.com/doc/refman/5.0/en/connector-j-examples.html

当然如果你觉得这样还是很麻烦,那么我已经为懒惰的你上传了这两个编译好的文件(index.jsp,MyServlet.class),你直拉下载就可以使用了:http://download.csdn.net/source/877999

附一:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="MyServlet" method="get">
<table align="center" width="50%">
<caption>用户登陆:</caption>
<tr>
<td>用户名:</td>
<td><input type="text" name="user"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd"></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="登陆">
</td>
</tr>
</table>
</form>
</body>
</html>

附二:

package myServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
* Servlet implementation class for Servlet: MyServlet
*
*/
public class MyServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public MyServlet() {
super();
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
private static final String CONTENT_TYPE="text/html; charset=GBK";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType(CONTENT_TYPE);
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet</title></head>");
out.println("<body>");
//out.println("<p>Welcom you zhangwenbo!</p>");


String user=request.getParameter("user");
String pwd=request.getParameter("pwd");
String dbname=null;
String dbpwd=null;
try {
// The newInstance() call is a work around for some
// broken Java implementations
String url="jdbc:mysql://localhost/shengji";
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection conn = DriverManager.getConnection(url,"user","password");
Statement stmt=conn.createStatement();
String sql="select user,pwd from user where user='"+user+"'";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next())
{
dbname=rs.getString(1);
dbpwd=rs.getString(2);
}
} catch (Exception ex) {
// handle the error
ex.printStackTrace();
}

if(dbname.equals(user) && dbpwd.equals(pwd))
{
out.println("<p>Welcom"+dbname+"</p>");
out.println("</html>");
}
else
{
response.sendRedirect("index.jsp");
}
}

/* (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}

---枯海(http://blog.csdn.net/xiaodao1986)

你可能感兴趣的:(JBuilder)