Java program to operate MySQL database

Source: Internet
Author: User

Database content:

Java source code: (the Code implements the query of the personnel information with a score of 100, and there are comments in the Code for other functions) Note: When running a program in eclipse, You need to insert a jar package into the project, otherwise, an exception occurs! Import java. SQL .*;
Import java. io. *; class database_manage {public Connection conn = null; public ResultSet rs = null; private String DatabaseDriver = "com. mysql. jdbc. Driver ";
// DataSource data source name DSN
Private String DatabaseConnStr = "jdbc: mysql: // localhost: 3306/lele_manage? UseUnicode = true & characterEncoding = utf8"
+ ", Root, root"; public void setDatabaseDriver (String Driver ){
This. DatabaseDriver = Driver;
} Public String getDatabaseDriver (){
Return (this. DatabaseDriver );
} Public void setDatabaseConnStr (String ConnStr ){
This. DatabaseConnStr = ConnStr;
} Public String getDatabaseConnStr (){
Return (this. DatabaseConnStr );
} Public database_manage () {// constructor connects to the database
Try {
Class. forName (DatabaseDriver );
} Catch (java. lang. ClassNotFoundException e ){
System. err. println ("error loading drive:" + e. getMessage ());
System. out. print ("insert execution error:" + e. getMessage (); // output to the client
}
}
Public ResultSet query (String SQL) {// query a database
Rs = null;
Try {
Conn = DriverManager
. GetConnection (
"Jdbc: mysql: // localhost: 3306/people_manage? UseUnicode = true & characterEncoding = utf8 ",
"Root", "root ");
Statement stmt = conn. createStatement ();
Rs = stmt.exe cuteQuery (SQL );
} Catch (SQLException ex ){
System. err. println ("An error occurred while performing the query:" + ex. getMessage ());
System. out. print ("query execution error:" + ex. getMessage (); // output to the client
}

Return rs;

} Public int update_database (String SQL) {// update or insert a database
Int num = 0;
Try {
Conn = DriverManager
. GetConnection (
"Jdbc: mysql: // localhost: 3306/people_manage? UseUnicode = true & characterEncoding = utf8 ",
"Root", "root ");
Statement stmt = conn. createStatement ();
Num = stmt.exe cuteUpdate (SQL );
} Catch (SQLException ex ){
System. err. println ("An error occurred while executing the insert operation:" + ex. getMessage ());
System. out. print ("insert execution error:" + ex. getMessage (); // output to the client
} CloseDataBase ();
Return num;} public void CloseDataBase () {// close the database
Try {
Conn. close ();
} Catch (Exception end ){
System. err. println ("An error occurred while executing the close Connection object:" + end. getMessage ());
System. out. print ("An error occurred while executing and closing the Connection object:" + end. getMessage (); // output to the client
}
} Class people {
Private String uid;
Private String name;
Private String banji;
Private int score; public people (){
} Public people (String uid, String name, String banji ){
This. uid = uid;
This. name = name;
This. banji = banji;
} Public people (String uid, String name, String banji, int score ){
This. uid = uid;
This. name = name;
This. banji = banji;
This. score = score;
} Public String getUid (){
Return uid;
} Public void setUid (String uid ){
This. uid = uid;
} Public String getName (){
Return name;
} Public void setName (String name ){
This. name = name;
} Public String getBanji (){
Return banji;
} Public void setBanji (String banji ){
This. banji = banji;
} Public int getScore (){
Return score;
} Public void setScore (int score ){
This. score = score;
} Public class manage {private people [] people_array1; // object array public void add_people (String uid, String name) {String SQL = "insert people (uid, name) values ('"+ uid +"', '"+ name
+ "')"; // SQL insert statement
// String sql2 = "insert people (uid, name) values ('uid001', 'Tom ')"; database_manage db_obj = new database_manage ();
Db_obj.update_database (SQL );
} Public void update_people (String uid, String name) {String SQL = "update people set name = '" + name + "'where uid ='" + uid
+ "'";
Database_manage db_obj = new database_manage ();
Db_obj.update_database (SQL);} public void delete_people (String uid) {String SQL = "delete from people where uid = '" + uid + "'"; database_manage db_obj = new database_manage (); db_obj.update_database (SQL);} public people query_people (String uid ){
Database_manage db_obj = new database_manage ();
// String adminid = null;
String uid_new, name, banji;
Uid_new = null;
Name = null;
Banji = null;
String SQL _query = "select * from people where uid = '" + uid + "'"; try {
ResultSet rs = db_obj.query (SQL _query );
If (rs. next ()){
Uid_new = rs. getString ("uid ");
Name = rs. getString ("name ");
Banji = rs. getString ("banji ");
}
} Catch (Exception e ){
E. getMessage ();
}
People new_people = new people (uid_new, name, banji );
Return new_people;
} Public people [] query_people_byscore (int score ){
Database_manage db_obj = new database_manage ();
String uid_new, name, banji;
Uid_new = null;
Name = null;
Banji = null;
Int score_new = 0;

String SQL _query = "select * from people where score =" + score; // SQL query statement

Try {

ResultSet rs = db_obj.query (SQL _query); // return the result set after the query.

Int num = 0;

ResultSet rs_new = rs;

While (rs_new.next () {// Number of students in the statistical result set

Num ++;

}

// System. out. println (num );

Lele_array1 = new people [num];

Int I = 0;

Rs. beforeFirst (); // start of the returned result set

While (rs. next ()){

Uid_new = rs. getString ("uid ");

Name = rs. getString ("name ");

Banji = rs. getString ("banji ");

Score_new = rs. getInt ("score ");

People_array1 [I] = new people (uid_new, name, banji, score_new );

I ++;

}

} Catch (Exception e ){

E. getMessage ();

}

Return people_array1;

}

Public static void main (String args []) {

/*

* People new_people = new people ();

*

* Manage mr = new manage (); // mr. add_people ("000", "James"); insert a student's Information

*

* New_people = mr. query_people ("001"); // query the student information of uid = 001. The System. out object is returned.

* Println ("" + new_people.getName () + "" + new_people.getBanji ());

* Mr. update_people ("004", "John"); updates the information of a student.

*

* New_people = mr. query_people ("004"); // query after update

*

* System. out. println ("" + new_people.getName () + "" + new_people.getBanji (

*));

*/

Manage mr = new manage ();

// Mr. delete_people ("001"); Delete student information with uid = 001

People [] lele_array; // declares an array of objects.

Lele_array = mr. query_people_byscore (100); // returns an array of student classes whose score is one hundred, and then outputs

Int num = 0;

Num = lele_array.length;

For (int I = 0; I <num; I ++ ){

System. out. println (lele_array [I]. getUid () + ""

+ People_array [I]. getName () + ""

+ Lele_array [I]. getBanji () + ""

+ Lele_array [I]. getScore ());

}

}

}

Program running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.