How to connect Java to MySQL database in ubuntu environment
The content of this article is about how to connect Java to the MySQL database in the ubuntu environment. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
For the jar package, we need to configure it into the development tool. As shown in the figure,
The first step is to create a directory in the project, usually called jar. You can call it whatever you want. Then put the jar package ctrl C V into it.
The second step is to right-click and select build path. There will be a milk bottle logo in it. I don’t remember what it’s called, just remember the shape of the bottle. I may have configured it here, but it’s gone.
If you see the jar package as shown in the picture in Referenced Libraries, you will be successful. Very simple.
#After the jar package is completed, we start coding.
The code I wrote is really ugly, but it still works. Let me break it down for you a little bit.
Based on the object-oriented principle, a separate package and a class are used for the purpose of returning a Connection object.
The first is initialization, driver, URL, user, password, you can just change it to your own.
The purpose of adding ?useUnicode=true&characterEncoding=UTF-8 to the url is to prevent garbled Chinese characters from being written into the database.
After initialization, the driver is loaded, then the Connection object is obtained, and finally a method is provided to return the object.
public class DBHelp { private static Connection conn; static { String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/qbxbf?useUnicode=true&characterEncoding=UTF-8"; String user = "root"; String password = "123"; try { Class.forName(driver); conn = DriverManager.getConnection(url,user,password); } catch (Exception e) { e.printStackTrace(); } } public static Connection getCoon() { return conn; } }
About dbutils, this is an Apache open source database processing package. The function is very powerful, everyone can learn it by themselves, hehe.
QueryRunner is an object of dbutils. This object can be used to add, delete, modify and check.
private static void write2DB(WX wx) throws SQLException { Connection conn = DBHelp.getCoon(); QueryRunner qr = new QueryRunner(); String sql = "insert into wx(title,author,institution,keywords,abstracts,journal,period) values(?,?,?,?,?,?,?)"; Object[] params = {wx.getTitle(),wx.getAuthor(),wx.getInstitution(),wx.getKeywords(),wx.getAbstracts(),wx.getJournal(),wx.getPeriod()}; qr.update(conn, sql, params); }
The data can be written to the database through the above method. Next, convert the text from the database to txt.
There is a very powerful function of dbutils, which is BeanListHandler. Of course, if it is just an entity object, you can use BeanHandler.
public static void main(String[] args) throws Exception { Connection conn = DBHelp.getCoon(); QueryRunner qr = new QueryRunner(); String sql = "select * from wx order by period desc"; List<WX> wx_list = qr.query(conn, sql, new BeanListHandler<WX>(WX.class)); File file = new File("/home/phe/桌面/情报学报2015_Y.txt"); if (!file.exists()) { file.createNewFile(); } BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,true))); for(WX wx: wx_list) { bw.write(wx.getJournal());bw.newLine();bw.flush(); bw.write("第"+wx.getPeriod()+"期");bw.newLine();bw.flush(); bw.write("Title:"+wx.getTitle());bw.newLine();bw.flush(); bw.write("Author:"+wx.getAuthor());bw.newLine();bw.flush(); bw.write("Institution:"+wx.getInstitution());bw.newLine();bw.flush(); bw.write("Keywords:"+wx.getKeywords());bw.newLine();bw.flush(); bw.write("Abstract:"+wx.getAbstracts());bw.newLine();bw.flush(); bw.newLine(); } bw.close(); }
The above is the detailed content of How to connect Java to MySQL database in ubuntu environment. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen
