package test;
import java.sql.*;
public class Conn
{
static String data ="jdbc:mysql://localhost/first";
static java.sql.Statement st;
public static void databaseconnect()
{ //函數用於 串連資料庫
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(data, "root", "root");
st = conn.createStatement();
}
catch (Exception e)
{
System.out.println("異常:"+e.toString());
}
}
public static String pwquery(String name)
{
databaseconnect();
String inname = name;
String pw = "";
ResultSet rs=null;
try
{
rs = ((java.sql.Statement) st).executeQuery("select * from test2 where name='"+name+"'");
while(rs.next())
{
pw=rs.getString("pass");
}
rs.close();
st.close();
//pw="abc";
}
catch (SQLException e)
{
e.printStackTrace();
}
return pw;
}
} <?xml version="1.0" encoding="gb2312" ?>
<%@ page language="java" contentType="text/html; charset=gb2312"%>
<%@page import="test.Conn" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>test</title>
</head>
<body>
<%
String temp=(String)session.getValue("login");
temp= Conn.pwquery("admin");
// out.print(temp);
out.print(temp);
%>
</body>
</html>
注意把 tomcat\common\lib
添加
mysql-connector-java-5.0.8-bin.jar