MYSQL_1_2010_03_23
来源:互联网 发布:网络五星宏辉 编辑:程序博客网 时间:2024/06/06 15:39
MYSQL
创建数据库: create database database_name;
进入数据库:use database_name //use命令必须在一个单行上给出
创建表用脚本(保存为*.sql文件)
create table batle_name(
(column_name datatype [column-level-constraint])
)
查询: select * from table_name
插入:insert into table_name values (,,,,,)
提交:commit
查询表结构: desc table_name
自增:auto_increment //放在字段名定义后面你
select
版本号: version()
当前日期: current_date
当前时间:now()
查看当前用户:user()
把日期转换成字符串:date_fomat(now(),'%Y-%m-%d %H:%i:%s')
show
查看当前存在的数据库: databases
查看当前存在的表: tables
-----------------------------------------------------------------------------------------------------------------------------------------------------
import java.sql.*;
public class MySqlConnection {
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try{
Class.forName("com.mysq.jdbc.Driver");
conn = DriverManager
.getConnection("jdbc:mysql://localhost/mydata?user=root&password=root");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from dept");
while(rs.next()){
System.out.println(rs.getString("deptno"));
}
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException ex){
System.out.println("SQLException:"+ex.getMessage());
System.out.println("SQLState:"+ex.getSQLState());
System.out.println("VendorError:"+ex.getErrorCode());
}finally{
try{
if(rs != null){
rs.close();
rs =null;
}
if(stmt != null){
stmt.close();
stmt=null;
}if(conn != null){
conn.close();
conn = null;
}
}catch(SQLException e){
e.printStackTrace();
}
}
}
}