Connect to the mysql database in JAVA and javamysql Database

Source: Internet
Author: User

Connect to the mysql database in JAVA and javamysql Database

Last week, I designed the database course, using java and mysql databases, which involves the knowledge of connecting to the database using java.

package entity;public class user {    private int id;    private String user;    private String pwd;    private String mark;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getUser() {        return user;    }    public void setUser(String user) {        this.user = user;    }    public String getPwd() {        return pwd;    }    public void setPwd(String pwd) {        this.pwd = pwd;    }    public String getMark() {        return mark;    }    public void setMark(String mark) {        this.mark = mark;    }}
Public int addUser (user u) {List <user> list = new ArrayList <user> (); Connection ct = null; PreparedStatement ps = null; ResultSet rs = null; int result = 0; try {Statement sta = null; Class. forName ("com. mysql. jdbc. driver "); ct = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/whj", "root", ""); // connect to the database String SQL = "insert into user (user, pwd, mark) values ('"+ u. getUser () + "','" + u. getPwd () + "','" + u. getMark () + "')"; // Insert new user sta = ct. createStatement (); result = sta.exe cuteUpdate (SQL);} catch (Exception e) {e. printStackTrace ();} return result ;}

Query:

Public int getUser (user u) {List <user> list = new ArrayList <user> (); Connection ct = null; PreparedStatement ps = null; ResultSet rs = null; try {Statement sta = null; Class. forName ("com. mysql. jdbc. driver "); // load the Driver ct = DriverManager. getConnection ("jdbc: mysql: // 127.0.0.1: 3306/whj", "root", ""); // connect to the database String SQL = "select * from user "; // query all user ps = (PreparedStatement) ct. prepareStatement (SQL); rs = ps.exe cuteQuery (); while (rs. next () {user use = new user (); use. setId (rs. getInt ("id"); use. setUser (rs. getString ("user"); use. setPwd (rs. getString ("pwd"); use. setMark (rs. getString ("mark"); list. add (use) ;}for (int I = 0; I <list. size (); I ++) {user use = list. get (I); if (use. getUser (). equals (u. getUser () // verify and Judge & use. getPwd (). equals (u. getPwd () {return 0;} if (I = list. size ()-1) {return-1 ;}} ct. close ();} catch (Exception e) {e. printStackTrace ();} return-1 ;}

Then this is some basic operations for connecting to the database in java.

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.