JSP链接mysql

//项目名称上右键,选build path,添加外部jar

package javaBean;

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

public class MyDmp {
public static void main(String[] args) throws Exception {

Connection conn = null;
String sql;
// MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称?参数=值
// 避免中文乱码要指定useUnicode和characterEncoding
// 执行数据库操作之前要在数据库管理系统上创建一个数据库,名字自己定,
// 下面语句之前就要先创建javademo数据库
String url = “jdbc:mysql://localhost:3306/ycdmp?”
+ “user=ycharriet&password=ycharriet&useUnicode=true&characterEncoding=UTF8”;
try {
// 之所以要使用下面这条语句,是因为要使用MySQL的驱动,所以我们要把它驱动起来,
// 可以通过Class.forName把它加载进去,也可以通过初始化来驱动起来,下面三种形式都可以
Class.forName(“com.mysql.jdbc.Driver”);// 动态加载mysql驱动
// or:
// com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
// or:
// new com.mysql.jdbc.Driver();

System.out.println(“成功加载MySQL驱动程序”);
// 一个Connection代表一个数据库连接
conn = DriverManager.getConnection(url);
// Statement里面带有很多方法,比如executeUpdate可以实现插入,更新和删除等
Statement stmt = conn.createStatement();
// sql = “create table users(NO char(20),name varchar(20),primary key(NO))”;
// int result = stmt.executeUpdate(sql);// executeUpdate语句会返回一个受影响的行数,如果返回-1就没有成功
// if (result != -1) {
// System.out.println(“创建数据表成功”);
int result;
sql = “insert into users(NO,name) values(‘011′,’陶伟基’)”;
result = stmt.executeUpdate(sql);
sql = “insert into users(NO,name) values(‘012′,’周小俊’)”;
result = stmt.executeUpdate(sql);
sql = “select * from users”;
ResultSet rs = stmt.executeQuery(sql);// executeQuery会返回结果的集合,否则返回空值
System.out.println(“id\t姓名”);
while (rs.next()) {
System.out
.println(rs.getString(1) + “\t” + rs.getString(2));// 入如果返回的是int类型可以用getInt()
}
// }
} catch (SQLException e) {
System.out.println(“MySQL操作错误”);
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}

}

}

As3与jsp交互

//test.jsp
<%@page import="javaBean.simpleBean"%>
<% out.clear(); //避免出现多余html代码 request.setCharacterEncoding("UTF-8"); //避免中文乱码 response.setCharacterEncoding("UTF-8"); simpleBean a=new simpleBean(); out.println(a.returnName("1234")); %>
~~~~~~~~~~~~~~~~~~~~~~
//simpleBean.java
package javaBean;

public class simpleBean {

public String returnName(String name)
{
if(name.equals(“”))
{
return “&data=hello guest”;
}else{
return “&data=hello”+name;
}
}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~

//as3 code LoginPage.as关联到fla中,定义了一个btn和txt
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.system.System;

/**
* …
* @author amyblog.ml
*/
public class LoginPage extends Sprite
{

public function LoginPage()
{
super();
main();
}

private function main():void
{
System.useCodePage=true;
btn.addEventListener(MouseEvent.CLICK,loadMss);
}

private function loadMss(e:MouseEvent):void
{
var postLoader:URLLoader=new URLLoader();
var request:URLRequest=new URLRequest();
var vars:URLVariables=new URLVariables();
vars.s1=”flash”;
vars.s2=”flex”;
request.url=”http://https://https://cfzy.site:8080/myproject/test.jsp”;
request.method=URLRequestMethod.POST;
request.data=vars;//这里的data可以是一个Object,或者Array
postLoader.load(request);
postLoader.addEventListener(Event.COMPLETE,completeFun);
}

private function completeFun(e:Event):void
{
txt.text = “test:” + (e.currentTarget as URLLoader).data;
trace(e.target.data);
//trace((e.currentTarget as URLLoader).data);
}

}

}