JSP + MySQL database operations

Source: Internet
Author: User

 

Package Test;
Import Java. SQL. * ;

Public   Class Conn
{

Static String data = " JDBC: mysql: // localhost/first " ;
Static Java. SQL. Statement st;


Public   Static   Void Databaseconnect ()
{ // Function used to connect to the database
Try  
{
Class. forname ("Com. MySQL. JDBC. Driver");

Connection Conn=Drivermanager. getconnection (data,"Root","Root");
St=Conn. createstatement ();

}  
Catch (Exception E)
{
System. Out. println ("Exception:"+E. tostring ());
}  
}  

Public   Static String pwquery (string name)
{
Databaseconnect ();
String inname = Name;
String pw =   "" ;
Resultset rs = Null ;
Try  
{
RS = (Java. SQL. Statement) stanc.exe cutequery ( " 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; charsets = 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 >

Note that Tomcat \ common \ Lib
Add
Mysql-connector-java-5.0.8-bin.jar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.