Java operation MySQL Database simple example

Source: Internet
Author: User
Tags stmt

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 connection database

public static String username = "root"; User name of the database

public static String password = ""; Password for the database

public static Connection conn=null;//Connection object

public static Statement stmt=null;//statement

public static ResultSet rs = null;//result set

1. Load MySQL Database driver

Static

{

try {

Class.forName ("Com.mysql.jdbc.Driver");

2. Establish database connection

conn = (Connection) drivermanager.getconnection (Url,username,password);

IF (conn! = null)

{

SYSTEM.OUT.PRINTLN ("Database connection is OK");

}

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 ("School Number:" +rs.getint ("Sid") + ", Name:" +rs.getstring ("sname") + ", Age:" +rs.getint ("ages") + ", Gender:" +rs.getstring ("gender"));

}

} catch (Exception e)

{

E.printstacktrace ();

}

finally

{

Destoryresource ();

}

}


Add Student method

public static Boolean Add ()

{

String sql = "INSERT into Students values (11, ' Three days ', 138, ' F ', ' [email protected] ', ' 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 ();

}

}

Ways to release 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 last Resource

public static void Destoryallresource ()

{

Try

{

if (conn! = null)

{

Conn.close ();

conn = null;

}

} catch (Exception e) {

Todo:handle exception

E.printstacktrace ();

}

}

Delete the student information of the assigned school 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 deletion succeeded");

return true;

}

Else

{

SYSTEM.OUT.PRINTLN ("Data Tim is not deleted");

return false;

}

}

catch (Exception ex)

{

Ex.printstacktrace ();

return false;

}

Finally

{

Destoryresource ();

}

}

Modification of all students to 20 years of age

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 statements

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 the student number 11);

Jdbc02.query ();

Jdbc02.update (20);

SYSTEM.OUT.PRINTLN ("------Modify all students aged 20 years--------");

Jdbc02.query ();

Jdbc02.destoryallresource (); Freeing resources

}

}


This article is from the "Know the Ming" blog, please be sure to keep this source http://357712148.blog.51cto.com/6440370/1894631

Java operation MySQL Database simple example

Related Article

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.