JSP tutorial connecting to the MySQL Tutorial database tutorial code
//Method One
compiles the Dbconn.java using the JDK's Javac command to form the corresponding class file.
= "cd /tomcat4112/webapps tutorial/examples/web-inf/classes/mysqltest/
=" javac -g -verbose Dbconn.java
7. Create mysqltest.jsp files in Tomcat's/webapps/examples/jsp directory. Its contents are as follows:
<%@ page contenttype= "text/html; charset=gb2312" % >
<%@ page language= "java" import= "java.sql.*" %>
& lt;jsp:usebean id= "DBconn1" scope= "page" class= "Mysqltest.dbconn" />
<%
resultset rs = dbconn1.executequery ("SELECT *& nbsp; from my_test_table ");
while (Rs.next ()) {
Out.print ("}
Rs.close ();
Method Two
Driver name
String drivername= "Com.mysql.jdbc.Driver";
Database user Name
String username= "cl41";
Password
String userpasswd= "123456";
Database name
String dbname= "DB";
Join string
String url= "jdbc:mysql://localhost/" +dbname+ "user=" +username+ "&password=" +USERPASSWD;
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
Connection connection=drivermanager.getconnection (URL);
Statement Statement = Connection.createstatement ();
Connect MySQL Database Two
class.forname ("Org.gjt.mm.mysql.Driver"). newinstance ();
String url = "jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useunicode=true& Characterencoding=8859_1 "
TestDB for your database name
Connection conn= drivermanager.getconnection (URL);
Statement stmt=conn.createstatement (resultset.type_scroll_sensitive,resultset.concur_updatable);
JSP connection MySQL Database three
Package mysqltest;
Import java.sql.*;
public class Dbconn
{String dbdriver= "com.mysql.jdbc.Driver";
String connstr= "Jdbc:mysql://school:3306/my_test";
String myuser= "Shimin";
String mypassword= "Shimin";
Connection conn = null;
ResultSet rs = null;
Public Dbconn ()
{Try
{Class.forName (dbdriver);
}
catch (Java.lang.ClassNotFoundException e)
{System.err.println ("dbconn ():" + e.getmessage ());
}
}
Public ResultSet executequery (String sql)
{rs = null;
Try
{conn = drivermanager.getconnection (Connstr,myuser,mypassword);
Statement stmt = Conn.createstatement ();
rs = stmt.executequery (SQL);
}
catch (SQLException ex)
{System.err.println ("Aq.executequery:" +ex.getmessage ());
}
Return RS;
}
}