Home  >  Article  >  Database  >  Mysql的简单操作_MySQL

Mysql的简单操作_MySQL

WBOY
WBOYOriginal
2016-06-01 13:07:06767browse

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

创建数据库,表格。


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

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


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

添加数据。

select * from stuinfo;

查询数据。


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();		}	}


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

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

C:/Program Files/MySQL/Connector J 5.1.23

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

mysql-connector-java-5.1.23-bin.jar


这里有一份w3cschool上的教程:

点击打开链接



Statement:
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