JAVA technology Case - Packaging JDBC Tools

JDBC

Brief introduction


- JDBC

Java DataBase Connectivity,java
Database Connectivity) is a method for performing
SQL
Statement
Java API
It can provide uniform access to a variety of relational databases, which consists of a group with
Java
Written in language classes and interfaces.
JDBC
It provides a benchmark that allow you to build more advanced tools and interfaces that enable database developers to write database applications



- Java

It has a sturdy, safe, easy to use, easy to understand and can be automatically downloaded from the Internet and other characteristics, is to write database applications outstanding language. Need is
Java
Applications and various methods of dialogue between different databases.



- JDBC

It can be used on a variety of platforms
Java
,Such as
Windows
Mac OS
And various versions of the
UNIX



- JDBC

Library includes commonly used with databases mentioned below each of the tasks related to the
API

 

 

JDBC

Login code verification realize ideas

 

*

Keyboard to enter a user name and password, the user comparison information in the database to determine whether the login is successful


* 1

,Connect to the database

 

* MyJDBCUtils.getConnection()

* 2

To get the request object
stmt

 

* conn.createStmtement()

* 3

Create keyboard objects, obtain a user name and password

 

* 3.1

Create a keyboard input objects

 

* 3.2

Prompt the user to enter

 

* 3.3

Get user input

 

* 4

,write
SQL
Statement, the user name and password into the
SQL
Statement

 

* 5

Execute the query
,
Obtaining query results

 

* stmt.executeQuery(sql);

* 6

Judgment based on the result of the query is successful login


* 7

, Close the connection

 

Java

Tools

 

 

in

java

Development process
Often use some code

Scanner

Random

The same class
They are the keyboard input
Generating a random number based
Like as a tool
in

java

It is referred to as tools

 

 

When we write your own code
Some functions and codes

java

Similar tools
Such as database connections, authenticated login will be used to get long, every time writing too much trouble, we can try to write your own tools, when used directly to guide each packet call on the line, can improve our development efficiency.

 

l

Package

JDBC

Tools

 

n

Incorporation database connection object

 

n

Method of adding connection release

 

 

code show as below

 

 

Tools Code

 

package com.qianfeng.util;

 

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

 

/**

* JDBC

Tools

 

*

The method of obtaining connection with a

 

* @author dushine

*/

public class JDBCUtil {

 

/**

*

Method for obtaining a database connection

 

* @return Connection conn

* @throws SQLException

*/

public static Connection getConnection() throws SQLException {

String url = "jdbc:mysql://localhost:3306/class?useSSL=false";

String user = "root";

String password = "root";

Connection conn = DriverManager.getConnection(url,user,password);

return conn;

}

 

/**

*

Connection release method

 

* @param conn

* @throws SQLException

*/

public static void releaseSourse(Connection conn) throws SQLException {

if (conn != null) {

conn.close();

}

}

 

 

/**

*

Connection release method

 

* @param conn

Database connection object

 

* @param stmt

carried out

SQL

Object statement

 

* @throws SQLException

*/

public static void releaseSourse(Connection conn,Statement stmt) throws SQLException {

if (stmt != null) {

stmt.close();

}

 

if (conn != null) {

conn.close();

}

}

 

/**

*

Connection release method

 

* @param conn

Database connection object

 

* @param stmt

carried out

SQL

Object statement

 

* @param resultSet

carried out

SQL

Statement returns the result set

 

* @throws SQLException

*/

public static void releaseSourse(Connection conn,Statement stmt,ResultSet resultSet) throws SQLException {

if (resultSet != null) {

resultSet.close();

}

 

if (stmt != null) {

stmt.close();

}

 

if (conn != null) {

conn.close();

}

}

}

 

 

 

Test Class Code

 

package com.qianfeng.demos;

 

import java.sql.Connection;

import java.sql.ResultSet;

import java.sql.Statement;

import java.util.Scanner;

 

import com.qianfeng.util.JDBCUtil;

 

public class Demo04 {

public static void main(String[] args) throws Exception {

/**

*

log in Register

 

*

Get user input

 

*

As a condition of the contents of the input query the database

 

*/

Scanner sc = new Scanner(System.in);

 

System.out.println("

please enter user name
:");

 

String name = sc.nextLine();

 

System.out.println("

Please enter your password
:");

 

String pwd = sc.nextLine();

 

//

Registration drive

 

Class.forName("com.mysql.jdbc.Driver");

/*

String url = "jdbc:mysql://localhost:3306/class?useSSL=false";

String user = "root";

String password = "root";

//

Acquisition and database connection

 

Connection conn = DriverManager.getConnection(url, user, password);*/

 

Connection conn = JDBCUtil.getConnection();

 

//

Using a connection object gets executed
sql
Object

 

Statement stmt = conn.createStatement();

 

//

write
SQL
Statement

 

String sql = "select * from userinfo where username='"+name+"' and password='"+pwd+"'";

System.out.println(sql);

 

//

carried out
SQL
Statement returns the result to obtain

 

ResultSet resultSet = stmt.executeQuery(sql);

if (resultSet.next()) {

System.out.println("

Landed successfully!
");

 

} else {

System.out.println("

wrong user name or password!
");

 

}

JDBCUtil.releaseSourse(conn, stmt, resultSet);

sc.close();

}

}

 

Guess you like

Origin www.cnblogs.com/qfchen/p/11590441.html