"MySQL" Java database additions and deletions to MySQL, Java system class

Source: Internet
Author: User
Tags first string

This part is the so-called JDBC, the content of the Web site data source, make the name very advanced, actually just Java in the MySQL database additions and deletions to change the content. Very simple, before writing so much of the content of MySQL, not a good summary, really should not. Today to implement a Java in the MySQL database additions and deletions, casually with a bit of Java out of the current system system name and system time knowledge, to complete a somewhat interesting small example.


I. BASIC OBJECTIVES

First of all, in the MySQL database there is a blank table named TestTable, the ID of the self-increment column, username,class are text columns, but the class is stored in numbers, that is, shaping, we later in Java to remove this column to be treated as plastic.


Every time the program executes, the program will insert the current system name (unchanged) with the username in the current system (will change), milliseconds, years and days will not be inserted, because beyond the scope of the shaping, this is not a problem, but in order to avoid the program is too complex, do not introduce "Java" Since the BigInteger, I don't have to worry about how much data to deal with (click to open link).



Second, the production process

1, first of all, Java to connect MySQL database, not like ASP link access,c# link SQL Server, are self-brought good. After all, it is the staff of Microsoft, although we often use Windows to program, develop, But we also want to download a mysql-connector-java-5.1.32.jar from the Internet, this thing in MySQL's official website has no way to test, there is no cross-platform version, on a Windows version of the MSI installation files, you or online search Mysql-conne Ctor-java, the version number is not important, have not seen the code is written correctly and the connection failed.

2. Then, create a new Lib folder under your project directory, Put this mysql-connector-java-5.1.32.jar in, do not call this name is also possible, but it is said that the industry basically, some companies do not even to the above that has been more and more can not write code manager scold


3. After that, right click on your project Select "Properties"


4, in the Java Build Path tab, select Add JARs ..., in the Lib folder to find just put in the Mysql-connector-java-5.1.32.jar, then click OK to start to happily write code


5, first you have to introduce the following things at the beginning:

Import Java.sql.*;import java.text.*;import java.util.*;import java.util.Date;
To be able to get the current time, and also use the following to ArrayList dynamic array, what is this thing, please turn to my previous "Java" Java Collections Class--java in the upgraded version of the data structure (click the Open link), Import java.util.* must be introduced in the header of the file, because the SQL class also has the Date () class, so you must add an import Java.util.Date; disambiguation, import java.text.*; It is also used to process the current time of the system, otherwise it can only get the number of milliseconds from 1970 New Year to the present.

6. Then write a database link class Dbcon

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 many 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 user name, password is the database password. 3306 is the port number of MySQL, generally this//behind that string of long parameters is to prevent garbled, eliminate each time need to add a set NAMES utf8string url = "jdbc:mysql://localhost:3306/ Test?useunicode=true&characterencoding=utf8&useoldaliasmetadatabehavior=true "; String user = "root"; String password = "root"; con = drivermanager.getconnection (url, user, password);} catch (Exception e) {e.printstacktrace ();} return con;}}

7, then is the core of the entire program how to operate the database:

public class Javadbtest {public static void main (string[] args) {//Here is a link to the database connection con = new Dbcon (). Getcon (); String sql = null;//This can get the name of the current operating system//the beginning of the database insert operation, modify and query the same reason, do not do, modify the SQL statement in the following SQL string username = System.getproperty ("Os.name");//This can get the current time, but must be introduced in the file header import java.util.*;//because the SQL class also has the date () This class, so you must add an import java.util.Date; disambiguate string classstring = new SimpleDateFormat ("HHMMSS"). Format (New Date (System.currenttimemillis ())) . toString (); sql = "INSERT into TestTable (username,class) VALUES ('" + username+ "', '" + classstring + "')";//Note: The statement that operates the database Insert into,update statement with query database Select in Java statement is different//operation database is Con.createstatement (). Execute (SQL);//Query database is rs= Con.preparestatement (SQL). ExecuteQuery (), and the query result must be ResultSet object Rs to catch try {con.createstatement (). Execute (SQL);// After finishing, people walk with the door Con.close ();} catch (Exception e) {e.printstacktrace ();} sql = "SELECT * from TestTable"; ResultSet rs = null;//These few arraylist that store the results of the query must be placed on the periphery of the try-catch, notice the variable valid range arraylist<integer> idarraylist = new arraylist<integer> (); arraylist<string> usernamearraylist = new arraylist<string> (); arraylist<integer> classarraylist = new arraylist<integer> (); try {rs = con.preparestatement (sql). ExecuteQuery ();//The result of this loop is that the entire query structure is read out while (Rs.next ()) {///out each column is printed out Idarraylist.add (rs.getint ("id")); Usernamearraylist.add (rs.getstring ("username"));//Even though class one column is full of numbers, because this column uses varchar to store//If you want to treat class as plastic, Therefore, it is not possible to use Rs.getint, only the first string is removed and then erased to Rs.getstring ("class"), Classarraylist.add (Integer.parseint (Rs.getstring (" Class ")));} After finishing, people walk with the door Con.close ();} catch (Exception e) {e.printstacktrace ();} SYSTEM.OUT.PRINTLN ("ID username class"); for (int i = 0; i < idarraylist.size (); i++) System.out.println ( Idarraylist.get (i) + "" + usernamearraylist.get (i) + "" + classarraylist.get (i));}}

8, to this point, the entire program is connected to this:

Package Test;import java.sql.*;import java.text.*;import java.util.*;import java.util.date;class Dbcon {//Here connect to database, Open a class independently, the subsequent operation of the database every connection does not have to write so many 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 user name, password is the database password. 3306 is the port number of MySQL, generally this//behind that string of long parameters is to prevent garbled, eliminate each time need to add a set NAMES utf8string url = "jdbc:mysql://localhost:3306/ Test?useunicode=true&characterencoding=utf8&useoldaliasmetadatabehavior=true "; String user = "root"; String password = "root"; con = drivermanager.getconnection (url, user, password);} catch (Exception e) {e.printstacktrace ();} return con;}} public class Javadbtest {public static void main (string[] args) {//Here is a link to the database connection con = new Dbcon (). Getcon (); String sql = null;//This can get the name of the current operating system//the beginning of the database insert operation, modify and query the same reason, do not do, modify the SQL statement in the following SQL string username = System.getproperty ("Os.name");//This can get the current time, but must be introduced in the file header import java.util.*;//because the SQL class also has the date () This class, so you must add an import java.util.Date; Eliminate ambiguity String classString = new SimpleDateFormat ("HHMMSS"). Format (New Date (System.currenttimemillis ())). toString (); sql = "INSERT into TestTable (Username,class) VALUES (' "+ username+" ', ' "+ classstring +" ') ";//Note: The statements that manipulate the database insert into,update with the query database statement Sele The CT statements in Java are different//operations database for Con.createstatement (). Execute (SQL);//Query database is rs=con.preparestatement (SQL). ExecuteQuery (); and the query result must be caught by the ResultSet object Rs catch try {con.createstatement (). Execute (SQL);//After the person walks with the door con.close ();} catch (Exception e) { E.printstacktrace ();} sql = "SELECT * from TestTable"; ResultSet rs = null;//These few arraylist that store the results of the query must be placed on the periphery of the try-catch, notice the variable valid range arraylist<integer> idarraylist = new Arraylist<integer> (); arraylist<string> usernamearraylist = new arraylist<string> (); arraylist<integer> classarraylist = new arraylist<integer> (); try {rs = con.preparestatement (sql). ExecuteQuery ();//The result of this loop is that the entire query structure is read out while (Rs.next ()) {///out each column is printed out Idarraylist.add (rs.getint ("id")); Usernamearraylist.add (rs.getstring ("username"))//Even though class one column is full of numbers,This column uses a varchar to store//If the class is to be treated as an orthopedic, so it is not available for rs.getint, it can only be removed by string and then erased as a plastic rs.getstring ("class"); Classarraylist.add ( Integer.parseint (rs.getstring ("Class")));} After finishing, people walk with the door Con.close ();} catch (Exception e) {e.printstacktrace ();} SYSTEM.OUT.PRINTLN ("ID username class"); for (int i = 0; i < idarraylist.size (); i++) System.out.println ( Idarraylist.get (i) + "" + usernamearraylist.get (i) + "" + classarraylist.get (i));}}


"MySQL" Java database additions and deletions to MySQL, Java system class

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.