Accp8.0 conversion material chapter 6th connecting MySQL understanding and exercises, accp8.0mysql

Source: Internet
Author: User
Tags driver manager

Accp8.0 conversion material chapter 6th connecting MySQL understanding and exercises, accp8.0mysql

JDBC_ODBC, connecting to mysql in java-only mode

1. Word Section

① JDBCjava connection database ② driver manager driver ③ connection ④ statement

⑤ Execute 6 query 7 result set used for access by region connectivity and region access

Entity

Ii. Preview

1. What is the main function of jdbc?

Connect to database

2. jdbc two common driver Methods

Bridge Connection and pure java connection

3. What is persistence?

An operation that converts data in a program to a permanent state in an instantaneous state.

4. What is dao mode? What are the advantages of this mode?

In my understanding, even if dao is not a development mode

In the data operation layer, the data abstraction layer interface is provided for the business layer.

It facilitates developers to develop code hierarchically, reduce code coupling, and perform data persistence operations.

5. Which package does java properties belong to and which class is derived from?

Java. until; hashtable class

Iii. Exercises

1. Use java-only connection to the database, and handle exceptions (mysql-connector-java-5.1.0-bin.jar) first to export the package

Just test the class.

Package workOne;

Import java. SQL. Connection;
Import java. SQL. DriverManager;

Public class test {

/**
* @ Param args
*/
// Private static Logger logger = Logger. getLogger (test. class. getName ());
Public static void main (String [] args ){
// TODO Auto-generated method stub
Connection conn = null;
Try {
Class. forName ("com. mysql. jdbc. Driver ");
} Catch (Exception e ){
// TODO: handle exception
// Logger. error (e );
E. printStackTrace ();
}
Try {
Conn = DriverManager. getConnection (
"Jdbc: mysql: // localhost: 3306/epet", "root", "123"
);
System. out. println ("Connection established successfully! ");
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}
Finally {
Try {
If (null! = Conn ){
Conn. close ();
System. out. println ("the connection is closed successfully! ");

}
} Catch (Exception e2 ){
// TODO: handle exception
E2.printStackTrace ();
}

}
}

}

2. implement data access for PET host Login

First, create the dao package (basedao and masterdao class ):

Package Dao;

Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. PreparedStatement;
Import java. SQL. ResultSet;
Import java. SQL. SQLException;
Import java. SQL. Statement;


Public class BaseDao {
Private String driver = "com. mysql. jdbc. Driver ";
Private String url = "jdbc: mysql: // localhost: 3306/epet ";
Private String user = "root ";
Private String pass = "123 ";
PreparedStatement pstmt = null;
ResultSet resultSet = null;
Connection conn = null;
// Obtain the connection
Public Connection getConnection (){
If (conn = null ){
Try {
Class. forName (driver );
Conn = DriverManager. getConnection (url, user, pass );
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}

}
Return conn;

}

Public void closeConn (Connection con, Statement stmt, ResultSet rs ){
If (rs! = Null ){
Try {
Rs. close ();
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}


}
If (stmt! = Null ){
Try {
Stmt. close ();
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}


}
If (conn! = Null ){
Try {
Conn. close ();
} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}


}
}

Public int DoUpdate (String SQL, Object [] param ){

Int num = 0;
Conn = getConnection ();
Try {
Pstmt = conn. prepareStatement (SQL );
If (param! = Null ){
For (int I = 0; I <param. length; I ++ ){
Pstmt. setObject (I + 1, param [I]);
}


}
Num=pstmt.exe cuteUpdate ();

} Catch (Exception e ){
// TODO: handle exception
E. printStackTrace ();
}
Finally {

CloseConn (conn, pstmt, null );
}
Return num;
}

Public ResultSet DoQuery (String SQL, Object [] para ){
Conn = getConnection ();

Try {
Pstmt = conn. prepareStatement (SQL );
If (para! = Null ){
For (int I = 0; I <para. length; I ++ ){

Pstmt. setObject (I + 1, para [I]);
}

}
ResultSet=pstmt.exe cuteQuery ();

} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return resultSet;

}
}

// Master dao

Package Dao;

Import entity. master;

Public interface masterDao {
Boolean findMaster (master mas );
}

// Create a daoimpl package dao implementation class

Package daoImpl;

Import java. SQL. ResultSet;
Import java. SQL. SQLException;

Import entity. master;
Import Dao. BaseDao;
Import Dao. masterDao;

Public class masterDaoImpl extends BaseDao implements masterDao {


Public boolean findMaster (master mas ){
// TODO Auto-generated method stub
String SQL = "SELECT * FROM 'master' WHERE 'name' =? AND 'Password' =? ";
Object [] para = {mas. getName (), mas. getPass ()};
ResultSet rs = DoQuery (SQL, para );
Boolean flag = false;
//. MasterDaoImpl // master ma = new master ();
Try {
While (rs. next ()){
Flag = true;
}
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return flag;
}

}

// Create three entity class primary human pet class

// Master

Package entity;

Public class master {
Private int id;
Private String name;
Private int pass;
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getPass (){
Return pass;
}
Public void setPass (int pass ){
This. pass = pass;
}
}

// Pet_type pet type

Package entity;

Public class pet_type {
Private int id;
Private String name;
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
}

// Pet

Package entity;

Public class pet {
Private int id;
Private int master_id;
Private String name;
Private int type_id;
Private int health;
Private int love;
Private String adop_time;
Private int status;
Public int getId (){
Return id;
}
Public void setId (int id ){
This. id = id;
}
Public int getMaster_id (){
Return master_id;
}
Public void setMaster_id (int master_id ){
This. master_id = master_id;
}
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public int getType_id (){
Return type_id;
}
Public void setType_id (int type_id ){
This. type_id = type_id;
}
Public int getHealth (){
Return health;
}
Public void setHealth (int health ){
This. health = health;
}
Public int getLove (){
Return love;
}
Public void setLove (int love ){
This. love = love;
}
Public String getAdop_time (){
Return adop_time;
}
Public void setAdop_time (String adop_time ){
This. adop_time = adop_time;
}
Public int getStatus (){
Return status;
}
Public void setStatus (int status ){
This. status = status;
}

}

// Create the main interface of the test package

Package test;

Import java. util. collections;

Import daoImpl. masterDaoImpl;

Import Dao. masterDao;

Import entity. master;

Public class test {

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Wrote input = new partition (System. in );
System. out. println ("------- welcome to pet park -------");
System. out. println ("Enter the login name :");
String loginid = input. next ();
System. out. println ("enter the password :");
Int loginpass = input. nextInt ();
Master mas = new master ();
Mas. setName (loginid );
Mas. setPass (loginpass );
MasterDao = new masterDaoImpl ();
Boolean res = masterDao. findMaster (mas );
If (res ){
System. out. println ("Login successful! ");
}
Else {
System. out. println ("Logon Failed! ");
}
}

}

3. Implement the pet owner login business

In the second exercise, the third exercise has been performed.

 

 

Summary:

1. The jdbc interface specification provided by sun is provided by the jdbc-api database vendor or a third party to provide specific implementation for different databases. This is the jdbc driver.

2. to access the database through jdbc, first load the driver, get in touch with the database, create the statement or preparedstatement object, then send the SQL statement, obtain the returned result, and finally process it.

3. The preparedstatement interface inherits from the statement interface. Improves the readability and maintainability of codes, and improves the performance and security of SQL statements.

4. dao full name data access objects (data access Object)

 

You are welcome to ask questions. You are welcome to refer to the error. You can reply to the discussion and learning information in private chat and comments if necessary.

For the original article, visit http://www.cnblogs.com/a782126844/if you have any questions, please contact us at: 2265682997.

 

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.