Eclipse + Tomcat implements mysql database connection

xiaoxiao2021-03-06  144

Note: This article assumes that you have successfully installed the Tomcat plugin Sysdeo!

First create Tomcat Project: File-> New-> Other, select Tomcat Project (Figure 1) Engine name input to connmysql (Figure 2) For convenience here, select CAN Update Server.xml file (default), click Finish to complete the project file Create (Figure 3) Creating a Java file in the Connmysql project. Select Connmysql From File-> New Click Class (Figure 4) Enter TEST in Package, Name Enter as SQLBean, other as default, complete file creation. The SQLBean file code is as follows: package test; import java.sql.connection; import java.sql.driverManager; // sqlbean is an abstraction function, implementing data connection and closing function public abstract class sqlbean {private string myDriver = "Org.gjt. mm.mysql.Driver "; private String myURL =" jdbc: mysql: //8.8.8.2:? 3306 / netbilling user = flux & password = 123456 "; protected Connection myConn; public sqlBean () {} public void makeConnection () throws Exception {Class.forName (myDriver) .newInstance (); myConn = DriverManager.getConnection (myURL);} public abstract void cleanup () throws Exception; public void takeDown () throws Exception {cleanup (); myConn.close ();} } The same method creates a dbbean file. The DBBean file code is as follows: package test;

Import java.sql.resultset; import java.sql.statement; // dbbean inherited from SQLBean implementation data public class dbean extends sqlbean {

String mySQL = "select operatorno, operatorname, description from operator"; ResultSet myResuleSet = null; Statement stmt = null; public dbBean () {super ();} public boolean getNextDB () throws Exception {return myResuleSet.next ();} public String getColumn (String inCol) throws Exception {return myResuleSet.getString (inCol);} public boolean getData () throws Exception {String myQuery = mySQL; stmt = myConn.createStatement (); myResuleSet = stmt.executeQuery (myQuery); return (MyResuleSet! = null);} public void cleanup () throws exception {stmt.close ();}} Next creates a JSP file called this bean. At this time, pay attention to select the project file connmysql and create a JSP file so that the file is directly created in the connmysql directory, which is convenient to call. File-> new-> file (Figure 5)

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

New Post(0)