Java additions and deletions to MySQL

Source: Internet
Author: User

-----Connect to a database

Package connectdb;
Import java.sql.*;
Class Dbcon {
Here to connect to the database, open a class independently, the subsequent operation of the database each connection will not have to write so much
Public Connection Getcon () {
Connection con = null;
try {
Class.forName ("Com.mysql.jdbc.Driver");
Where test is the database we want to link, user is the database username, password is the database password.
3306 is the port number of MySQL, which is usually the
The long argument behind the string is to prevent garbled, eliminating the need to add a set NAMES in any statement every time UTF8
String url = "Jdbc:mysql://localhost/testdb?useunicode=true&characterencoding=utf8&usessl=false";
String user = "root";
String Password = "basketball";
con = drivermanager.getconnection (url, user, password);
System.out.println ("Sucess to connect DB");
} catch (Exception e) {
SYSTEM.OUT.PRINTLN ("Fail to connect DB");
E.printstacktrace ();
}
return con;
}
}

Additions and deletions to check the operation:

Package connectdb;

Import java.sql.*;
Import java.util.ArrayList;
public class Applydb {
Defines a student class that is stored to the database data type student
Class student{
private String number;
private String name;
private String grade;
Public Student (String num,string name,string grade) {
This.number = num;
THIS.name = name;
This.grade = grade;
}
Public String GetNumber () {
return number;
}
Public String GetName () {
return name;
}
Public String Getgrade () {
return grade;
}
}
Define a ArrayList to hold the data
Public arraylist<student> dataList () {
Student stu1 = new Student ("1", "Li Qing", "90");
Student STU2 = new Student ("2", "Xiaoming", "60");
arraylist<student> stu = new arraylist<student> ();
Stu.add (STU1);
Stu.add (STU2);
return Stu;
}
Create a table
public void Create () {
Connection con = new Dbcon (). Getcon ();
try {
Statement s = con.createstatement ();
String Creatt = "CREATE TABLE student (number char", "name Char", Grade char (15)) ";
S.executeupdate (Creatt);
S.close ();
Con.close ();
System.out.println ("sucess to create");
} catch (SQLException e) {
TODO auto-generated Catch block
SYSTEM.OUT.PRINTLN ("fail to create");
E.printstacktrace ();
}
}
Inserting data
public void Insert () {
Connection con = new Dbcon (). Getcon ();
arraylist<student> Arstu = DataList ();
int length = Arstu.size ();
String number = "";
String name = "";
String grade = "";
PreparedStatement PS = null;
try {
for (int i = 0; i<length;i++) {
Number = Arstu.get (i). number;
Name = Arstu.get (i). Name;
Grade = Arstu.get (i). grade;
String sql = "INSERT into student values (?,?,?)";
PS = con.preparestatement (SQL);
Ps.setstring (1,number);
Ps.setstring (2, name);
Ps.setstring (3, grade);
Ps.executeupdate ();
}
Ps.close ();
Con.close ();
System.out.println ("sucess to insert");
} catch (SQLException E1) {
TODO auto-generated Catch block
SYSTEM.OUT.PRINTLN ("fail to insert");
E1.printstacktrace ();
}
}
Delete data
public void Delete () {
Connection con = new Dbcon (). Getcon ();
String sql = "Delete from student where number=?";
PreparedStatement PS = null;
try {
PS = con.preparestatement (SQL);
Ps.setstring (1, "1");
Ps.executeupdate ();
Ps.close ();
Con.close ();
System.out.println ("sucess to delete");
} catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("fail to delete");
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Find data
public void query () {
Connection con = new Dbcon (). Getcon ();
String sql = "SELECT * from student where number=?";
PreparedStatement PS = null;
ResultSet rs = null;
try {
ps= con.preparestatement (SQL);
Ps.setstring (1, "2");
rs = Ps.executequery ();
while (Rs.next ()) {
System.out.println (rs.getstring ("number"));
System.out.println (rs.getstring ("name"));
System.out.println (rs.getstring ("grade"));
}
Ps.close ();
Con.close ();
} catch (SQLException e) {
SYSTEM.OUT.PRINTLN ("fail to query");
TODO auto-generated Catch block
E.printstacktrace ();
}

}
public static void Main (String args[]) {
Applydb db = new Applydb ();
Db.create ();
Db.insert ();
Db.delete ();
Db.query ();
}
}

Java additions and deletions to MySQL

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.