JSP connection MySQL Database code

Source: Internet
Author: User

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;
}
}

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.