Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of connecting Java to MySQL Database in ubuntu Environment

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to make Java connect to MySQL database in ubuntu environment. I hope you will learn a lot after reading this article. Let's discuss it together.

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, which is usually called jar. Whatever you want. Then put the jar package ctrl+C+V into it.

The second step, right-click, select build path, which has a bottle logo. I don't remember the name, just remember the shape of the bottle. I may have been configured here. It's gone.

If you see the jar package shown in Referenced Libraries, it is successful. It's very simple.

After the jar package is finished, we will code it.

Look at the code you write is really ugly, but it can still work. I'll explain it to you a little bit.

In line with the principle of face object, a separate package and a class are used to return a Connection object.

The first is initialization, driver, URL, user, password, just look at it and change it to your own.

The purpose of adding useUnicode=true&characterEncoding=UTF-8 to url is to prevent Chinese writing into the database from garbled.

After initialization, the driver is loaded, the Connection object is obtained, and 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 open source database processing package for Apache. The function is very powerful, everyone to learn by themselves, hehe.

QueryRunner is an object of dbutils. Using this object, you can 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);}

Through the above method, the data can be written to the database. Next, move from the database to the txt text.

There is a very powerful function of dbutils in this, which is BeanListHandler. Of course, if it's just a physical 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_list = qr.query (conn, sql, new BeanListHandler (WX.class)) File file = new File ("/ home/phe/ Desktop / Intelligence Journal 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 ("1st + wx.getPeriod () +" period "); 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 () } after reading this article, I believe you have a certain understanding of how to make Java connect to MySQL database in ubuntu environment. If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report