JSP + Java Bean Access MySQL Database

xiaoxiao2021-03-06  37

Operating environment: JDK 5.0 Tomcat 5.5.4 mysql4.1.8

JDK installation path: d: /java/jdk1.5.0_01tomcat installation path: D: / Tomcat 5.5

Set an environment variable (Control Panel -> System -> Advanced): Java_Home = D: /JAVA/JDK1.5.0_01path=%java_Home%/binclasspath = .; 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 MySQL JDBC driver, CONNECT / J 3.1.6, can go to MySQL official website to download

Tomcat5.5 Virtual Directory Settings: D: / Tomcat 5.5 / Conf / Catalina / LocalHost New Test.xml content is as follows:

D: / WWW path will be the root of our test website, access to this virtual directory with http: // localhost: 8080 / test D: / www, copying a directory web-inf, there is a web.xml setting file (you can refer D: / Tomcat 5.5 / WebApps / root / web-inf / web.xml), classes directory and lib directory

// Java Bean connection database filename dbconn.javapackage 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 {Stmt = Conn.createStatement ();};} catch (Exception E) {E.PrintStackTrace ();} returnTackTrace ();} Return THIS;}

Compile Javac Welcome.java, put the compiled file Welcome.class to the directory D: / www / web-inf / classes / ningoo /

// Call the JSP file Test.jsp <% @ Page ContentType = "text / html; charset = GB2312" IMPORT = "scope =" PAGE "Class =" ningoo.dbconn "/> <% resultset = null; connection conn = null; ningoo.setserver (" 127.0.0.1 "); // Set the mysql server name or iPningoo.Setport (" 3306 "); / / Set mysql's listening port ningoo.setdb ("test"); // Set the database name NINGOO.SETUSER ("root") NINGOO.SETUSER ("root"); // Set the username Ningoo.SetPass ("password") ("password"); / / Set the password to connect 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 visit http: // localhost: 8080 / test / test.jsp, congratulations!

转载请注明原文地址:https://www.9cbs.com/read-63986.html

New Post(0)