JDBC
2006-7-27
//创建数据库:mysql>createdatabase数据库名;
//显示数据库:mysql>showdatabases;
//使用数据库:mysql>use数据库名;
//显示数据库中的表:mysql>showtables;
//显示表的内容:mysql>select*from表名;
//删除表:droptable表名;
//删除数据库:dropdatabase数据库名;
//显示mysql帮助:shell>mysql?-help
//连接数据库:
packageStudent;
//在students数据库中建立表student.
importjava.sql.*;
publicclassStudentMake{
publicstaticvoidmain(Stringargs[]){
Stringurl="jdbc:mysql://localhost/数据库名";//定义要连接的数据库文件
Connectioncon;//连接数据库
StringcreateString;
createString="createtable表名"+"(字段名1类型,"
+"字段名2类型,"+"字段名3类型CHAR(1),"+"字段名4类型VARCHAR(20),"
+"字段名5类型DATE,"+"字段名6类型VARCHAR(20))";//定义在数据库中建立表及相关字段的相关参数。
//类型为(INTEGER;)
Statementstmt;//创建表
try{
Class.forName("com.mysql.jdbc.Driver");//定义数据库的驱动
}catch(java.lang.ClassNotFoundExceptione){
System.err.print("ClassNotFoundException:");
System.err.println(e.getMessage());
}
try{
con=DriverManager.getConnection(url,"root","1234");//定义连接数据库时的相关参数。
stmt=con.createStatement();//创建连接数据库。
stmt.executeUpdate(createString);//创建表。
stmt.close();//关闭创建的对象。
con.close();//关闭连接的数据库。
}catch(Exceptione){
e.printStackTrace();
}
}
}