Sample Code for connecting to mysql using java

Source: Internet
Author: User

[Java]
Package com. qiyi. datatools;
 
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. util. ArrayList;
 
Import junit. framework. TestCase;
 
Import org. junit. After;
Import org. junit. Before;
Import org. junit. Test;
 
Public class TableVoHelper extends TestCase {
 
@ Test
Public void testOp () throws Exception {
Pstmt = con. prepareStatement ("delete from tableB ");
Pstmt.exe cute ();
Pstmt = con. prepareStatement ("select * from tableA ");
Rs = pstmt.exe cuteQuery ();
ArrayList <TableVo> tVoList = new ArrayList <TableVo> ();
While (rs. next ()){
TableVo tVo = new TableVo ();
TVo. setId (rs. getInt (1 ));
TVoList. add (tVo );
}
 
Pstmt = con. prepareStatement ("insert into tableB (id) values (?) ");
For (TableVo tVo: tVoList ){
Pstmt. setInt (1, tVo. getId ());
Pstmt.exe cute ();
}
 
}
 
@ Before
Public void setUp () throws Exception {
String CLASSNAME = "com. mysql. jdbc. Driver ";
String URL = "jdbc: mysql: // localhost: 3306/fiona ";
String UNAME = "root ";
String PWD = "root ";
Class. forName (CLASSNAME );
Con = DriverManager. getConnection (URL, UNAME, PWD );
}
 
@ After
Public void tearDown () throws Exception {
If (rs! = Null ){
Rs. close ();
}
If (pstmt! = Null ){
Pstmt. close ();
}
If (con! = Null ){
Con. close ();
}
}
 
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
 
Class TableVo {
Public int getId (){
Return id;
}
 
Public void setId (int id ){
This. id = id;
}
 
Private int id;
}
}
Package com. qiyi. datatools;

Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. util. ArrayList;

Import junit. framework. TestCase;

Import org. junit. After;
Import org. junit. Before;
Import org. junit. Test;

Public class TableVoHelper extends TestCase {

@ Test
Public void testOp () throws Exception {
Pstmt = con. prepareStatement ("delete from tableB ");
Pstmt.exe cute ();
Pstmt = con. prepareStatement ("select * from tableA ");
Rs = pstmt.exe cuteQuery ();
ArrayList <TableVo> tVoList = new ArrayList <TableVo> ();
While (rs. next ()){
TableVo tVo = new TableVo ();
TVo. setId (rs. getInt (1 ));
TVoList. add (tVo );
}

Pstmt = con. prepareStatement ("insert into tableB (id) values (?) ");
For (TableVo tVo: tVoList ){
Pstmt. setInt (1, tVo. getId ());
Pstmt.exe cute ();
}

}

@ Before
Public void setUp () throws Exception {
String CLASSNAME = "com. mysql. jdbc. Driver ";
String URL = "jdbc: mysql: // localhost: 3306/fiona ";
String UNAME = "root ";
String PWD = "root ";
Class. forName (CLASSNAME );
Con = DriverManager. getConnection (URL, UNAME, PWD );
}

@ After www.2cto.com
Public void tearDown () throws Exception {
If (rs! = Null ){
Rs. close ();
}
If (pstmt! = Null ){
Pstmt. close ();
}
If (con! = Null ){
Con. close ();
}
}

Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

Class TableVo {
Public int getId (){
Return id;
}

Public void setId (int id ){
This. id = id;
}

Private int id
}
}
Author: hongqishi
 


 

Related Article

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.