In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "Java operation mysql database example introduction", in the daily operation, I believe that many people have doubts on the introduction of Java operation mysql database examples. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "Java operation mysql database example introduction". Next, please follow the editor to study!
Package com.Jdbc.demo
Import java.sql.DriverManager
Import java.sql.ResultSet
Import java.sql.Statement
Import com.mysql.jdbc.Connection
Public class jdbc02 {
Public static final String url = "jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=UTF-8"; / / the URL address of the connected database
Public static String username = "root"; / / user name of the database
Public static String password = ""; / / password of the database
Public static Connection conn=null;// connection object
Public static Statement stmt=null;// statement
Public static ResultSet rs = null;// result set
/ / 1. Load the MySQL database driver
Static
{
Try {
Class.forName ("com.mysql.jdbc.Driver")
/ / 2. Establish a database connection
Conn = (Connection) DriverManager.getConnection (url,username,password)
If (conn! = null)
{
System.out.println ("Database connection is normal")
}
Else
{
System.out.println (database connection failed)
}
}
Catch (Exception e)
{
E.printStackTrace ()
}
}
/ / query all student information
Public static void query ()
{
String sql = "select * from students;"
Try {
Stmt = conn.createStatement ()
Rs = stmt.executeQuery (sql)
While (rs.next ())
{
System.out.println ("student number:" + rs.getInt ("sid") + ", name:" + rs.getString ("sname") + ", age:" + rs.getInt ("age") + ", gender:" + rs.getString ("gender"))
}
} catch (Exception e)
{
E.printStackTrace ()
}
Finally
{
DestoryResource ()
}
}
/ / add student method
Public static boolean add ()
{
String sql = "insert into Students values (11pm 'Zhang Santian', 138 'Zhangsanq.com' Guangzhou Yangjiang').
Try
{
Stmt = conn.createStatement ()
Int result = stmt.executeUpdate (sql)
If (result > 0)
{
System.out.println ("data added successfully")
Return true
}
Else
{
System.out.println ("Database add failed")
Return false
}
}
Catch (Exception ex)
{
Ex.printStackTrace ()
Return false
}
Finally
{
DestoryResource ()
}
}
/ / method of releasing resources
Public static void destoryResource ()
{
Try {
If (rs! = null)
{
Rs.close ()
Rs = null
}
If (stmt! = null)
{
Stmt.close ()
Stmt = null
}
} catch (Exception e) {
/ / TODO: handle exception
E.printStackTrace ()
}
}
/ / release the final resources
Public static void destoryallResource ()
{
Try
{
If (conn! = null)
{
Conn.close ()
Conn = null
}
} catch (Exception e) {
/ / TODO: handle exception
E.printStackTrace ()
}
}
/ / Delete the student data of the designated student number
Public static boolean delete (int sid)
{
String sql = "delete from students where sid=" + sid
Try
{
Stmt = conn.createStatement ()
Int result = stmt.executeUpdate (sql)
If (result > 0)
{
System.out.println ("data added and deleted successfully")
Return true
}
Else
{
System.out.println ("data addition not deleted")
Return false
}
}
Catch (Exception ex)
{
Ex.printStackTrace ()
Return false
}
Finally
{
DestoryResource ()
}
}
/ / modify the age of all students to 20 years old
Public static boolean update (int age)
{
String sql = "update students set age=" + age
Try
{
Stmt = conn.createStatement ()
Int result = stmt.executeUpdate (sql)
If (result > 0)
{
Return true
}
Else
{
Return false
}
}
Catch (Exception ex)
{
Ex.printStackTrace ()
Return false
}
Finally
{
DestoryResource ()
}
}
Public static void main (String [] args)
{
Jdbc02.query (); / / query statement
If (jdbc02.add ())
{
System.out.println ("added successfully!")
}
Else
{
System.out.println ("add failed!")
}
System.out.println ("-")
Jdbc02.query ()
Jdbc02.delete (11)
System.out.println ("- after deleting student number 11 -")
Jdbc02.query ()
Jdbc02.update (20)
System.out.println ("- modify the age of all students to 20 -")
Jdbc02.query ()
Jdbc02.destoryallResource (); / / release resources
}
}
At this point, the study on "Java operation mysql database example introduction" is over. I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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: 217
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.