Javaee-jdbc, javaee

Source: Internet
Author: User

Javaee-jdbc, javaee

1. First import the jdbc-mysql package

2. Program Framework:

3. Database Configuration File

Driver = com. mysql. jdbc. Driver
Url = jdbc: mysql: // localhost: 3306/jdbc_db
Username = root
Password = 1234

4. Access the underlying data:

Package cn.com. dale. utils;

Import java. io. IOException;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. SQLException;
Import java. util. Properties;

Public class Utils {

Public Connection openConnection (){
Properties pro = new Properties ();
Try {
Pro. load (this. getClass (). getClassLoader (). getResourceAsStream ("DBConfig. properties "));
String driver = pro. getProperty ("driver ");
String url = pro. getProperty ("url ");
String username = pro. getProperty ("username ");
String password = pro. getProperty ("password ");

Class. forName (driver );
Return DriverManager. getConnection (url, username, password );
} Catch (IOException e ){
E. printStackTrace ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return null;
}

Public void closeConnection (Connection conn ){
Try {
Conn. close ();
} Catch (SQLException e ){
E. printStackTrace ();
}
}
}


5. javabean:

Package cn.com. dale. Bean;

Public class User {
Private String name;
Private String password;
Public String getName (){
Return name;
}
Public void setName (String name ){
This. name = name;
}
Public String getPassword (){
Return password;
}
Public void setPassword (String password ){
This. password = password;
}

}

6. interface:

Package cn.com. dale. Dao;

Import cn.com. dale. Bean. User;

Public interface UserDao {

// Denglu
Public User login (User user );
// Zhuce
Public void register (User user );
// Jiancha
Public boolean check (String name );

}

7. Interface implementation class:

Package cn.com. dale. DaoImpl;

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

Import cn.com. dale. Bean. User;
Import cn.com. dale. Dao. UserDao;
Import cn.com. dale. utils. Utils;

Public class UserDaoImpl implements UserDao {

Public void register (User user ){

If (check (user. getName () = false ){
System. out. println ("this user name already exists! ");
Return;
}
String SQL = "insert into usertab (username, password) values (?,?) ";
Utils utils = new Utils ();
Connection conn = utils. openConnection ();
Try {
PreparedStatement stmt = conn. prepareStatement (SQL );
Stmt. setString (1, user. getName ());
Stmt. setString (2, user. getPassword ());

Stmt.exe cuteUpdate ();
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
Utils. closeConnection (conn );
}
}

Public User login (User user ){
String SQL = "select * from usertab where username =? And password =? ";
Utils utils = new Utils ();
Connection conn = utils. openConnection ();
Try {
PreparedStatement stmt = conn. prepareStatement (SQL );
Stmt. setString (1, user. getName ());
Stmt. setString (2, user. getPassword ());
ResultSet rs = stmt.exe cuteQuery ();
If (rs. next ()){
Return user;
}
} Catch (SQLException e ){
// TODO Auto-generated catch block
E. printStackTrace ();
} Finally {
Utils. closeConnection (conn );
}
Return null;
}


Public boolean check (String name ){
String SQL = "select * from usertab where username =? ";

Utils utils = new Utils ();
Connection conn = utils. openConnection ();
Try {
PreparedStatement stmt = conn. prepareStatement (SQL );
Stmt. setString (1, name );
ResultSet rs = stmt.exe cuteQuery ();
If (rs. next ()){
Return false;
}
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
Utils. closeConnection (conn );
}
Return true;
}
}


8. test:

Package cn.com. dale. test;

Import org. junit. Test;

Import cn.com. dale. Bean. User;
Import cn.com. dale. Dao. UserDao;
Import cn.com. dale. DaoImpl. UserDaoImpl;

Public class test01 {

@ Test
Public void test (){
// Utils utils = new Utils ();
// System. out. println (utils. openConnection ());

User user = new User ();
User. setName ("Small non ");
User. setPassword ("123 ");

UserDao userDao = new UserDaoImpl ();
UserDao. register (user );

User user1 = userDao. login (user );
System. out. println (user1.getName () + ":" + user1.getPassword ());

}
}



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.