Home > Database > Mysql Tutorial > body text

Mysql的简单操作_MySQL

WBOY
Release: 2016-06-01 13:07:06
Original
738 people have browsed it

create database Student;create table StuInfo(	stuName varchar(8),	stuId varchar(12) primary key,	stuAge int,	stuSex varchar(4));
Copy after login

创建数据库,表格。


alter table stuinfo drop stuSex,add stuSex varchar(4);
Copy after login

修改表格式。其中表格不区分大小写,字段区分大小写。


insert into stuinfo(stuName,stuAge,stuSex,stuId)values("朱豪",22,"男","123456");
Copy after login

添加数据。

select * from stuinfo;
Copy after login

查询数据。


jdbc连接mysql


public static void main(String[] args) {		// TODO Auto-generated method stub		Connection conn=null;		try{			String mysqlStr="com.mysql.jdbc.Driver";			String url="jdbc:mysql://localhost:3306/Student";			String user="root";			String password="mysql";			String selectSql="select * from stuinfo;";			String createSql="create table stuinfo(stuName varchar(8),stuId varchar(8),stuSex varchar(4),stuAge int);";			String insertSql="insert into stuinfo(stuName,stuId,stuSex,stuAge)values(?,?,?,?);";			Class.forName(mysqlStr);						conn=DriverManager.getConnection(url,user,password);						PreparedStatement ps=conn.prepareStatement(insertSql);						ps.setString(1, "呵呵");			ps.setString(2, "12345");			ps.setString(3, "男");			ps.setInt(4, 22);			ps.execute();			ResultSet rs=ps.executeQuery(selectSql);						if(conn!=null){				System.out.println("连接成功!");							}						while(rs.next()){				String tname=rs.getString("stuName");				String tsex=rs.getString("stuSex");				String tid=rs.getString("stuId");				int tage=rs.getInt("stuAge");				//System.out.println(rs.getRow());				System.out.println(tname+"-----"+tsex+"----"+tid+"-----"+tage);			}					}catch(ClassNotFoundException e){			e.printStackTrace();		}catch(SQLException e){			e.printStackTrace();		}	}
Copy after login


别忘记添加jdbc连接的jar文件

mysql装在c盘program files的话,路径如下:

C:/Program Files/MySQL/Connector J 5.1.23

文件名为(不同版本可能略微有所区别):

mysql-connector-java-5.1.23-bin.jar


这里有一份w3cschool上的教程:

点击打开链接



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!