Using JUnit to test database joins

Source: Internet
Author: User
Using JUnit test database joins 2007-04-13 13:05
Create a database demo in MySQL
First, establish a simple database connection class based on MySQL
Conmysql.java
Import java.sql.Connection;
Import Java.sql.DriverManager;

public class Conmysql {
Public Conmysql () {

}

PRIVATE Connection conn = null;
Private String URL = "Jdbc:mysql://localhost:3306/demo";
Private String user = "root";

Public Connection Conn () {
try {
Class.forName ("Org.gjt.mm.mysql.Driver"). newinstance ();
conn = drivermanager.getconnection (URL, user, "");
Return conn;
catch (Exception e) {
E.printstacktrace ();
return null;
}
}
}

Test data connection

Import Junit.framework.TestCase;
Import java.sql.*;

public class TestDB extends TestCase {

Public TestDB (String name) {
Super (name);
}

/*
* Test method for "Com.test.ConMysql.ConMysql ()"
*/
public void Testconmysql () {
Conmysql con = new Conmysql ();
Connection C = Con.conn ();
try {
Statement stmt = C.createstatement ();
String sql = "INSERT into customer values (' 1 ', ' a ', ' M ', ' nn ')";
Stmt.executeupdate (SQL);
Assertnotnull (Stmt.executequery ("SELECT * from Customer"));
Assertequals (1, Stmt.executequery ("SELECT count (*) from Customer"));

catch (SQLException e) {
TODO automatically generate catch blocks
E.printstacktrace ();
}

}

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.