Jsp+java beans access to MySQL database

Source: Internet
Author: User
Tags connect mysql stmt mysql database root directory server port tomcat
js|mysql| Access | data | Database operating environment: JDK 5.0 + Tomcat 5.5.4+mysql4.1.8

JDK Installation path: D:\Java\jdk1.5.0_01
Tomcat installation path: D:\Tomcat 5.5

Setting environment variables (Control Panel-> System-> Advanced):
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\ Mysql-connector-java-3.1.6-bin.jar;

Where D:\Tomcat 5.5\common\lib\mysql-connector-java-3.1.6-bin.jar is the JDBC driver of MySQL, connect/j 3.1.6, can be downloaded to the official MySQL website

Tomcat5.5 Virtual directory settings:
D:\Tomcat 5.5\conf\catalina\localhost under the new add a Test.xml
The contents are as follows:
<context path= "/test" docbase= "D:/www" reloadable= "true" crosscontext= "true" debug= "0" >
</Context>

The d:\www path will be the root directory of our test Web site, accessed through Http://localhost:8080/test access to the virtual directory
D:\www under the directory Web-inf, under the Web.xml settings file (refer to D:\Tomcat 5.5\webapps\root\web-inf\web.xml), classes directory and Lib directory

Java Bean file name for connection database 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 void Setserver (String str) {//set server name
SERVER=STR;
}

public void Setport (String str) {//set server port
Port = str;
}

public void Setdb (String str) {//set DB name
db = str;
}

public void SetUser (String str) {//set user name
user = str;
}

public void SetPass (String str) {//set user name
pass = STR;
}

Public ResultSet ExecuteSQL (String str) {
try{
Statement stmt = Conn.createstatement ();
rs = stmt.executequery (str);
}
catch (Exception e) {
E.printstacktrace ();
}
return this.rs;
}
}

Compile Javac Welcome.java, put the compiled file Welcome.class under the directory D:\www\WEB-INF\classes\NinGoo\

Call the Java Bean's JSP file test.jsp
<%@ page contenttype= "text/html;charset=gb2312" import= "java.sql.*"%>
<jsp:usebean id= "Ningoo" scope= "page" class= "Ningoo.dbconn"/>
<%
ResultSet rs = null;
Connection conn = null;
Ningoo.setserver ("127.0.0.1"); Set MySQL server name or IP
Ningoo.setport ("3306"); Set up the listening port for MySQL
Ningoo.setdb ("test"); Set MySQL database name
Ningoo.setuser ("root"); Set user name to connect to MySQL
Ningoo.setpass ("password"); Set the password to connect to MySQL
conn = Ningoo.getconn ();
rs = Ningoo.executesql ("SELECT * from Test");
while (Rs.next ()) {
%>
Row:<%=rs.getstring (1)%>
<%}%>
<%out.print ("successful!\n");%>
<%
Rs.close ();
Conn.close ();
%>

Then in the browser to visit http://localhost:8080/test/test.jsp, congratulations success!





Related Article

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.