JDBC-MYSQLEclipse connection and simple use
I have provided a lot of tutorials on connecting JDBC and MYSQL to the Internet, but I feel that they are not detailed enough. JDBC is used in this job, so I wrote the next step by referring to the online method for future use.
First download a WAMP and attach the Network Disk download link:
Http://pan.baidu.com/s/1qW2Uzc4
The introduction of WAMP will not be mentioned here. It integrates the Mysql database and provides phpMyadmin for ease of use.
Next, download the JDBC driver link:
Http://pan.baidu.com/s/1eQDylfO
The final tool is eclipse or myeclipse, which can be searched on the internet, any version. Eclipse is free of charge.
Appendix: Integrated android Development Environment ADT (ECLIPSE)
Http://pan.baidu.com/s/1bn7yBNL
The connection procedure is as follows:
1. First open phpMyadmin and create a database (you can manually or use SQL)
Create database test; // CREATE a DATABASE
Use test; // specify test as the database to be operated
Then create a table:
Create table user (nameVARCHAR (20), password VARCHAR (20 ));
// Create a table user and set two fields.
Insert data:
Insert into userVALUES ('huzhiheng', '000000'); // INSERT a data entry to the table.
2. Open Eclipse and create a project (my ),
Operation: Right-click the project and choose my ---> build Path ---> add external Archiver... select the jdbc driver and click OK.
My project list:
3. the driver has been imported. Let's write a program to verify it.
Import java. SQL. *; public class MysqlJdbc {public static void main (String args []) {try {Class. forName ("com. mysql. jdbc. driver "); // load the mysql jdbc Driver // Class. forName ("org. gjt. mm. mysql. driver "); System. out. println ("Success loading Mysql Driver! ");} Catch (Exception e) {System. out. print (" Error loading Mysql Driver! "); E. printStackTrace ();} try {Connection connect = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/test", "root", ""); // The Connection URL is jdbc: mysql // server address/database name, the following two parameters are respectively the login user name and password. The start password is generally blank. out. println ("Success connect Mysql server! "); Statement stmt = connect. createStatement (); ResultSet rs = stmt.exe cuteQuery ("select * from user"); // user is your table name while (rs. next () {System. out. println (rs. getString ("name") ;}} catch (Exception e) {System. out. print ("get data error! "); E. printStackTrace ();}}}
The output result is as follows:
Success loading Mysql Driver!
Success connect Mysql server!
Huzhiheng
The connection is successful.
For JDBC data insertion, query, and deletion details, see the following code:
Add:JDBC_Add(String name,String character,String introduce) throws SQLException{Connection conn = null;Statement st = null;String sql ="insert into game(gamename,gamecharacter,gameintroduce)" + "VALUES('" + name + "','" + character + "','" + introduce +"')";conn = DriverManager.getConnection(DBURL,DBUSER,"");st = conn.createStatement();st.executeUpdate(sql);st.close();conn.close();}
DEL: JDBC_DEL (String name) throws SQLException {Connection conn = null; Statement st = null; String SQL = "DELETE FROM game WHERE gamename = '" + name + "'"; // The NAME is of the String type. Therefore, you must quote conn = DriverManager. getConnection (DBURL, DBUSER, ""); st = conn.createstatement(;;st.exe cuteUpdate (SQL); st. close (); conn. close ();}
SEL:JDBC_SEL(String name,JLabel showname,JLabel character,JLabel introduce) throws SQLException {Connection conn = null;Statement st = null;ResultSet rs = null;String sql ="SELECT gamename,gamecharacter,gameintroduce from game WHERE gamename ='" + name + "'";conn = DriverManager.getConnection(DBURL,DBUSER,"");st = conn.createStatement();rs = st.executeQuery(sql);while(rs.next()){showname.setText(rs.getString("gamename"));character.setText(rs.getString("gamecharacter"));introduce.setText(rs.getString("gameintroduce"));}rs.close();st.close();conn.close();}
UPD: public class JDBC_UPD {private static final String DBDRIVER = "org. gjt. mm. mysql. driver "; private static final String DBURL =" jdbc: mysql: // localhost: 3306/mysql "; private static final String DBUSER =" root "; JDBC_UPD (String name, string character, String introduce) throws SQLException {Connection conn = null; Statement st = null; String SQL = "UPDATE game SET gamename = '" + name + "', gamecharacter = '"+ character +"', gameintroduce = '"+ introduce +" 'where gamename =' "+ name +" '"; // rewrite N times conn = DriverManager. getConnection (DBURL, DBUSER, ""); st = conn.createstatement(;;st.exe cuteUpdate (SQL); st. close (); conn. close ();}
---------------------------------------------- END -----------------------------------------------------