Home >Java >JavaBase >How to connect to database in java

How to connect to database in java

angryTom
angryTomOriginal
2019-11-11 16:16:4018710browse

How to connect to database in java

How to connect java to the database

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

This is the connection method officially provided by MySQL (i.e. MySQL connection driver). After decompression, you will get the jar library file, which needs to be imported into the project.

This is the decompressed file: (Recommended tutorial: java tutorial)

How to connect to database in java

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

How to connect to database in java

Right-click the project name (mine is sqldemo1) - Create New - Folder —Folder name is libs—Finish.

2. Add the package to connect to the mysql database in the package

That is, the mysql-connector-java-5.1.45-bin.jar## obtained after decompression.

#Then copy and paste it under the libs folder of our java project (note that it is the physical address);

At this time, in eclipse - right-click the libs folder - Refresh, and it will be downloaded Put the JDBC in the folder, as shown in the following figure:

How to connect to database in java

3. The jar package

on the build path is in In eclipse

a: Click on the project Project - select the properties Properties

How to connect to database in java

b: Add

After opening the properties Properties, click java Build path (Java Build Path)

Click Add jar (Add JARs...), select the jar package under your project, then confirm, and finally add it

How to connect to database in javaHow to connect to database in java

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

How to connect to database in java

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

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 perform related query operations

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();  
        }  
    }
    
}

After that, you can test

How to connect to database in java

The query results are as follows

How to connect to database in java##5. Analysis of the reasons for failure to connect to the mysql database:

1. Whether the database service is open

2. Whether the corresponding database is created

3. Whether the jar package is added

4. Whether the driver is loaded

5. Whether the connection string, login name and password are correct

If the connection fails, check whether these factors occur

The above is the detailed content of How to connect to database in java. 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