时间:2021-07-01 10:21:17 帮助过:2人阅读
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上的教程:
点击打开链接