J2EE connection to the database (Oracle, Mysql, SqlServer)

Source: Internet
Author: User

I have been reading books on JavaWeb recently and learning about jsp-related topics. No matter what you learn, you will encounter problems related to database connection. In addition, if you do not fully understand it, it will be a long time for you. In the future, it will be a lot of headache for various systems...

I sorted out my recent books, online videos, and videos for reference:

The overall steps are similar.

  1. Load driver
  2. Connect to database
  3. Database Operations
  4. Close Database

The usage of all databases is similar, but operations on databases are somewhat different, and they are similar!

String DBDRIVER = "sun.jdbc.odbc.JdbcOdbcDriver";String DBURL = "jdbc:odbc:mldn";Connection conn = null;Statement stmt = null;String sql = null;Class.forName(DBDRIVER);conn = DriverManager.getConnection(DBURL);

Then I posted the following small tests on several databases installed on my machine:

1. oracle Database (Oracle 9i)

<% @ Page contenttype = "text/html; charset = gb2312" %> <% @ page import = "Java. SQL. *" %> <%! // Define the database driver string dbdriver = "Sun. JDBC. ODBC. jdbcodbcdriver "; // defines the database connection address string dburl =" JDBC: ODBC: mldn "; // defines the database connection object, which belongs to Java. interface connection conn = NULL in the SQL package; // defines the statement object, which is used to operate the database statement stmt = NULL; // defines a string variable to save the SQL statement string SQL = NULL; %> <% // 1. Load the driver try {class. forname (dbdriver);} catch (exception e) {// use out here. print is for demonstration purpose. In actual development, all error messages cannot pass out. print print, otherwise there will be security issues out. PR Intln ("failed to load the database driver !!! ");} // 2. Connect to the database try {conn = drivermanager. getconnection (dburl);} catch (exception e) {out. println (" Database Connection Failed !!! ");} // 3. Operate the database // instantiate the statement object through the connection object try {stmt = Conn. createstatement (); // assign SQL = "create table person (name varchar (32)" to the SQL variable ))"; // execute this SQL statement stmt.exe cuteupdate (SQL) through the statement object;} catch (exception e) {out. println ("database operation failed !!! ");} // 4. Close the database try {// close the stmt operation. close (); // close the connection Conn. close ();} catch (exception e) {out. println ("failed to close the database !!! ") ;}%>

2. Mysql database (mysql 5.5)

<% @ Page contentType = "text/html; charset = gb2312" %> <% @ page import = "java. SQL. * "%> <% final String DBDRIVER =" org. gjt. mm. mysql. driver "; final String DBURL =" jdbc: mysql: // localhost/test "; final String DBUSER =" root "; final String DBPASSWORD = "********"; // your mysql Database Password Statement stmt = null; Connection conn = null; String SQL = null; %> <% try {Class. forName (DBDRIVER);} catch (Exception e) {%> 

3. SqlSwerver database (2008)

<% @ Page contentType = "text/html; charset = gb2312" %> <% @ page import = "java. SQL. * "%> <% -- connect to the sqlserver2008 database using JDBC -- %> <%! // String DBDRIVER = "com. microsoft. jdbc. sqlserver. SQLServerDriver "; // The above is the connection to an earlier version of SQL server. To connect to SQL server 2005/2008, use the following String DBDRIVER =" com. microsoft. sqlserver. jdbc. SQLServerDriver "; String DBURL =" jdbc: sqlserver: // 127.0.0.1: 1433; DatabaseName = Demo "; String DBUSER =" sa "; string DBPASSWORD = "********"; // Connection conn = null, String SQL = null, and Statement stmt = null in your SQL server; %> <% try {Class. forName (DBDRIVER);} catch (Exception e) {%> 

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.