当前位置:网站首页>Database connection operations for MySQL and MyEclipse

Database connection operations for MySQL and MyEclipse

2022-08-09 10:28:00 Zuo Mingshui

1. The MySQL database and MyEclipse software have been installed correctly.
2. I set up the database school (name) and table (StudentInfo) under the doc
After entering the doc: refer to the relevant code
show databases;
Show all databases
exit;
Launch doc
create database School;
Create a database
use School;
Enter database
create table StudentInfo(
name char(10),
age char(3));
Create table
insert StudentInfo(name,age)
VALUES('David','18');
Insert data
select from StudentInfo;
Query
update StudentInfo set age='12' where name='HAHA';
Update data
insert into studentInfo (name,age)values('Jingzhu',20);
Insert data
delete from StudentInfo where age='20';
Delete data
3. Create a Javaproject (named Show) in MyEclipsemysql-connector-java-5.0.8-bin.jar in the downloaded JdbC compressed package)
Fourth, create a class (named Show) and related test code as follows:
import java.sql.
;
public class Show
{
public static void main(String[] args)
{
try
{
String url="jdbc:mysql://localhost:3306/school";//school is the name of the database to be connected
String user="root";
Stringpwd="root";

//Load mysql driver, this sentence can also be written as: Class.forName("com.mysql.jdbc.Driver").newInstance();Class.forName("com.mysql.jdbc.Driver");//Call the method of the DriverManager object to obtain a Connection object, representing an open connection.Establish a connection with MySQL.Connection conn = DriverManager.getConnection(url, user, pwd);//Use the method of the Connection interface to create a Statement statement object, which is used to pass a simple SQL statement without parameters to the database management system for execution.Statement stmt = conn.createStatement();ResultSet rs = stmt.executeQuery("select * from studentInfo");// process the result setwhile (rs.next()){String name = rs.getString("name");System.out.print("name-----"+name);String age=rs.getString("age");System.out.println("age------"+age);}//close the connectionrs.close();stmt.close();conn.close();}catch (Exception ex){System.out.println("Error : " + ex.toString());}

}
}
5. Running results.
name-----HAHA age------12
name-----David age------18
If the result is as follows, the connection to the database is successful.
6. Follow-up questions?
How to solve the problem of garbled characters in the running result?
name-----HAHA age------12

原网站

版权声明
本文为[Zuo Mingshui]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091021203194.html