Home >Java >JavaBase >Steps to connect java to database

Steps to connect java to database

王林
王林Original
2019-11-11 16:34:0920875browse

Steps to connect java to database

Tools:

eclipse, MySQL, MySQL connection driver: mysql-connector-java-5.1.45.jar

1, Create a new java project and then create a new folder - libs (used to store various external packages)

Steps to connect java to database

2. Add a connection to the mysql database in the package The package

is mysql-connector-java-5.1.45-bin.jar, then copy and paste it under the libs folder of our java project (note that it is in the physical address); at this time, in In eclipse—right-click the libs folder—Refresh, and put the downloaded JDBC into the folder, as shown in the following figure:

Steps to connect java to database

##3, Jar package on the build path

a. Click on the project - select Properties

Steps to connect java to database

b. Add

After opening Properties, click Java Build Path, click Add jar (Add JARs...), select the jar package under your project, then confirm, and finally add it

Steps to connect java to database

4. Connect the mysql database in the java project

Create two new Class class files in the java project package, which I named DBHelper and Demo

Steps to connect java to database

A class is used to open or close the database: DBHelper.java

The code is as follows:

package sqldemo1;
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.PreparedStatement;  
import java.sql.SQLException;

public class DBHelper {
    public static final String url = "jdbc:mysql://127.0.0.1/student";  
    public static final String name = "com.mysql.jdbc.Driver";  
    public static final String user = "root";  
    public static final String password = "";  
 
    public Connection conn = null;  
    public PreparedStatement pst = null;  
 
    public DBHelper(String sql) {  
        try {  
            Class.forName(name);//指定连接类型  
            conn = DriverManager.getConnection(url,user,password);//获取连接  
            pst = conn.prepareStatement(sql);//准备执行语句  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
 
    public void close() {  
        try {  
            this.conn.close();  
            this.pst.close();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }
}

Write another Demo.java to execute related queries Operation

The code is as follows:

 package sqldemo1;

import java.sql.ResultSet;  
import java.sql.SQLException;

public class Demo {  
    
    static String sql = null;  
    static DBHelper db1 = null;  
    static ResultSet ret = null;  
    
    public static void main(String[] args) {  
        sql = "select *from stuinfo";//SQL语句  
        db1 = new DBHelper(sql);//创建DBHelper对象
        
        try {  
            ret = db1.pst.executeQuery();//执行语句,得到结果集  
            while (ret.next()) {  
                String uid = ret.getString(1);  
                String ufname = ret.getString(2);  
                String ulname = ret.getString(3);  
                String udate = ret.getString(4);  
                System.out.println(uid + "\t" + ufname + "\t" + ulname + "\t" + udate );  
            }//显示数据  
            ret.close();  
            db1.close();//关闭连接  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }
    
}

5. Analysis of the reasons for failure to connect to the mysql database

a. Is the database service open?

b. Whether the corresponding database has been created;

c. Whether the jar package has been added;

d. Whether the driver has been loaded;

e. Connection string, Are the login name and password correct?

Recommended tutorial:

Java tutorial

The above is the detailed content of Steps to connect java to database. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn