"Java" uses Javase to make additions and deletions to MySQL database

Source: Internet
Author: User

Main program:

Import Bean.studentbean;
Import Impl.studentimpl;

public class T7 {
public static void Main (string[] args) {
Studentimpl stdimp = new Studentimpl ();
Studentbean student = new Studentbean ("Halala", "Woman", "China");
Stdimp.add (student);
Stdimp.select ("Libin");
Stdimp.delete ("Halala");
Stdimp.add ("Halazi", "Woman", "computer");
Stdimp.update ("Halazi", "Libin");
}
}

DAO Layer:

Package Bean;

public class Studentbean {

private int number;
private String name;
Private String sex;
Private String major;

Public String GetName () {
return name;
}

public void SetName (String name) {
THIS.name = name;
}

public int GetNumber () {
return number;
}

public void Setnumber (int number) {
This.number = number;
}

Public String Getsex () {
return sex;
}

public void Setsex (String sex) {
This.sex = sex;
}

Public String Getmajor () {
Return major;
}

public void Setmajor (String major) {
This.major = major;
}

@Override
Public String toString () {
Return "Studentbean [number=" + number + ", name=" + name + ", sex="
+ Sex + ", major=" + Major + "]";
}

Public Studentbean () {
Super ();
TODO auto-generated Constructor stub
}

Public Studentbean (string name, String sex, String major) {
Super ();
THIS.name = name;
This.sex = sex;
This.major = major;
}

}

Implementation layer:

Package Impl;

Import java.sql.Connection;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.sql.Statement;

Import Bean.studentbean;
Import conn.bd;

public class Studentimpl {
BD BD = new BD ();

public int Add (Studentbean std) {
int row = 0;
Statement stmt = null;
Connection conn = null;
String sql = "INSERT INTO Info" (name,sex,major) VALUES ("
+ "' Std.getname () '" + "," + "' std.getsex () '" + "," + "' std.getmajor () '"
// + ");";
String sql = "INSERT into info (name,sex,major) VALUES ('"
+ std.getname () + "', '" + std.getsex () + "', '" + std.getmajor ()
+ "');";
SYSTEM.OUT.PRINTLN (SQL);
try {
stmt = Bd.conn (conn). Createstatement ();
row = stmt.executeupdate (SQL);
Stmt.close ();

return row;
} catch (SQLException e) {

E.printstacktrace ();
try {
Stmt.close ();
Conn.close ();
} catch (SQLException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}

return-1;
}

}

public int Add (string name, String sex, String major) {
int row = 0;
Connection conn = null;
Statement stmt = null;

Studentbean std = new Studentbean ();
Std.setname (name);
Std.setsex (Sex);
Std.setmajor (Major);

String sql = "INSERT INTO Info" (name,sex,major) VALUES ("
+ "' Std.getname () '" + "," + "' std.getsex () '" + "," + "' std.getmajor () '"
// + ");";
String sql = "INSERT into info (name,sex,major) VALUES ('"
+ std.getname () + "', '" + std.getsex () + "', '" + std.getmajor ()
+ "');";
SYSTEM.OUT.PRINTLN (SQL);
try {
stmt = Bd.conn (conn). Createstatement ();
row = stmt.executeupdate (SQL);
Stmt.close ();
return row;
} catch (SQLException e) {

E.printstacktrace ();
try {
Stmt.close ();
Conn.close ();
} catch (SQLException E1) {
TODO auto-generated Catch block
E1.printstacktrace ();
}

return-1;
}

}

public int Delete (String name) {
Studentbean std = new Studentbean ();
Statement stmt = null;
Connection conn = null;
int dem = 0;
Std.setname (name);
String sql = "Delete from info where name= '" + std.getname () + "';";
SYSTEM.OUT.PRINTLN (SQL);
try {
stmt = Bd.conn (conn). Createstatement ();
dem = stmt.executeupdate (SQL);
System.out.println ("Delete succeeded! ");
Return dem;
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
return-1;
}
}
public void Select (String name) {
Studentbean std = new Studentbean ();
Statement stmt = null;
Connection conn = null;
ResultSet rs = null;
Std.setname (name);
String sql = "SELECT * from info where name= '" + std.getname () + "';";
SYSTEM.OUT.PRINTLN (SQL);
try {
stmt = Bd.conn (conn). Createstatement ();
rs = stmt.executequery (SQL);
SYSTEM.OUT.PRINTLN ("Query succeeded! ");
while (Rs.next ()) {
System.out.println ();
System.out.println (+rs.getint ("number") + "\ T"
+ rs.getstring ("name") + "\ T" + rs.getstring ("Sex")
+ "\ T" + rs.getstring ("major") + "\ T");
}
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
SYSTEM.OUT.PRINTLN ("Query failed! ");
}
}

//
public int update (String name,string upinfo) {

Statement stmt = null;
Connection conn = null;
Studentbean std=new Studentbean ();
Std.setname (name);
int upd = 0;

String sql = "Update info set name= '" +upinfo+ "' Where Name= '" +std.getname () + "';";
SYSTEM.OUT.PRINTLN (SQL);
try {
stmt = Bd.conn (conn). Createstatement ();

UPD = stmt.executeupdate (sql);
Stmt.close ();
return upd;
} catch (SQLException e) {

E.printstacktrace ();


return-1;
}

}
}

and database connection layer:

Package Conn;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.SQLException;
Import java.sql.Statement;

public class BD {

Public Connection Conn (Connection conn) {
Driver name
String Driver = "Com.mysql.jdbc.Driver";
The URL points to the drive name of the database you are accessing
String url = "Jdbc:mysql://127.0.0.1:3306/student";
Configure the user name for MySQL
String username = "root";
Java password for MySQL connection
String password = "root123";
try {
Class.forName (driver);
conn = drivermanager.getconnection (URL, username, password);

} catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

Return conn;
}

}

The relationship between them is:

BD connection database, DAO layer is a database element mapping, implementation layer calls DAO layer implementation of data additions and deletions, the main function call implementation layer for concrete operation.

Novice Pure hand dozen, also please many advice!!

"Java" uses Javase to make additions and deletions to MySQL database

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.