This article mainly introduces how to access mysql database data and perform simple operations in Java programs, for more information about how to connect Myeclipse to the mysql database, see the previous article, this article describes how to access data in the mysql database in a Java program and perform simple operations. for details, see the following.
Create a javaProject and enter the following java code:
Package link; import java. SQL. *;/*** process of connecting to MySQL using JDBC * DataBase: fuck, table: person; * Basic operations for adding, deleting, modifying, and querying mysql databases using myeclipse. */Public class JDBCTest {public static Connection getConnection () throws SQLException, java. lang. classNotFoundException {// Step 1: load the JDBC driver Class of MySQL. forName ("com. mysql. jdbc. driver "); // get the connection url, the user name and password that can access the MySQL database; jsj: database name String url =" jdbc: mysql: // localhost:/fuck "; string username = "root"; String password = ""; // Step 2: Create an instance Connection con = DriverManager for the Connection class to the MySQL database. getConnection (url, username, password); return con;} public static void main (String args []) {try {// Step 3: Get the connection class instance con, use con to create a Statement object class instance SQL _statement Connection con = getConnection (); Statement SQL _statement = con. createStatement (); // if a database with the same name exists, delete // SQL _statement.executeUpdate ("drop table if exists student "); // execute an SQL statement to generate a table named student // SQL _statement.executeUpdate ("create table student (id int not null auto_increment, name varchar () not null default 'name ', math int not null default, primary key (id); "); // insert data into the person table SQL _statement.executeUpdate (" insert person values (, 'liying ',)"); values ("insert person values (, 'jiangshance',)"); SQL _statement.executeUpdate ("insert person values (, 'wangjiawu ',)"); SQL _statement.executeUpdate ("insert person values (, 'duchangfeng',) "); // Step 4: execute the query. use the ResultSet class object to return the query result String query =" select * from person "; resultSet result = SQL _statement.executeQuery (query); // display the content of the person table in the data: System. out. println ("data in the person table is as follows:"); System. out. println ("------------------------"); System. out. println ("no." + "" + "name" + "+" score "); System. out. println ("------------------------"); // process the obtained query results and operate on the objects of the Result class while (result. next () {int number = result. getInt ("number"); String name = result. getString ("name"); String mathsorce = result. getString ("mathsorce"); // obtain the data System in the database. out. println ("" + number + "" + name + "" + mathsorce);} // close the connection and declare SQL _statement.close (); con. close ();} catch (java. lang. classNotFoundException e) {System. err. print ("ClassNotFoundException"); System. err. println (e. getMessage ();} catch (SQLException ex) {System. err. println ("SQLException:" + ex. getMessage ());}}}
Note that you need to modify the settings.
For example, the url and account, and password must be consistent with your own.
The data to be accessed must match the type in the database to print the correct result.
Right-click the project name --> Build Path --> Configure bicould Path --> Libraries --> Add External JARs --> Add a jdbc package (for details, refer to Mysql's simple use (1 )) ---> OK
In this case, a Referenced Libraries package file is added to the package, indicating that the configuration is successful.
Click Run as ---> Run Java Application ---> JDBCTest -- link --->. The result is as follows:
The above is how to access the data in the mysql database in the Java program and perform simple operations _ MySQL content. For more information, please follow the PHP Chinese network (www.php1.cn )!