首页 > 代码库 > JDBC技术基础整理

JDBC技术基础整理


mysql 启动和关闭命令:
启动:net start mysql 关闭:net stop mysql
mysql修改密码的命令:
use mysql;
update user set password=password(‘12345‘) where user=‘root‘;

登录的命令:
mysql -u root -p

新建登录用户
mysql> insert into mysql.user(Host,User<Password)values("localhost","test",password("123456"))

2.为用户授权

  授权格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 

  2.1 登录MYSQL(有ROOT权限),这里以ROOT身份登录:

  @>mysql -u root -p

  @>密码

  2.2 首先为用户创建一个数据库(testDB):

  mysql>create database testDB;

  2.3 授权test用户拥有testDB数据库的所有权限(某个数据库的所有权限):

   mysql>grant all privileges on testDB.* to test@localhost identified by ‘1234‘;

  mysql>flush privileges;//刷新系统权限表

  格式:grant 权限 on 数据库.* to 用户名@登录主机 identified by "密码"; 

  2.4 如果想指定部分权限给一用户,可以这样来写:

  mysql>grant select,update on testDB.* to test@localhost identified by ‘1234‘;

  mysql>flush privileges; //刷新系统权限表

  2.5 授权test用户拥有所有数据库的某些权限:  

  mysql>grant select,delete,update,create,drop on *.* to test@"%" identified by "1234";

//test用户对所有数据库都有select,delete,update,create,drop 权限。

  //@"%" 表示对所有非本地主机授权,不包括localhost。(localhost地址设为127.0.0.1,如果设为真实的本地地址,不知道是否可以,没有验证。)

 //对localhost授权:加上一句grant all privileges on testDB.* to test@localhost identified by ‘1234‘;即可。

 

3. 删除用户

  @>mysql -u root -p

  @>密码

  mysql>Delete FROM user Where User=‘test‘ and Host=‘localhost‘;

  mysql>flush privileges;

  mysql>drop database testDB; //删除用户的数据库

删除账户及权限:>drop user 用户名@‘%‘;

        >drop user 用户名@ localhost;

 

4. 修改指定用户密码

  @>mysql -u root -p

  @>密码

  mysql>update mysql.user set password=password(‘新密码‘) where User="test" and Host="localhost";

  mysql>flush privileges;

 

5. 列出所有数据库

  mysql>show database;

 

6. 切换数据库

  mysql>use ‘数据库名‘;

 

7. 列出所有表

  mysql>show tables;

 

8. 显示数据表结构

  mysql>describe 表名;

 

9. 删除数据库和数据表

  mysql>drop database 数据库名;

  mysql>drop table 数据表名;

JDBC

JDBC 连接数据库的过程

①注册数据库驱动

例:Class.forName("com.mysql.jdbc.Driver");

②构建数据库连接URL

例:URL基本格式:“JDBC协议+IP地址或域名+端口+数据库名称”
“jdbc:mysql://localhost:3306/test”

③获取Connection对象
DriverManager.getConnection(url,username,password);


连接代码如下:


try{
Class.forName("com.mysql.jdbc.Driver");//加载数据库驱动,注册到驱动管理器
String url = "jdbc:mysql://localhost:3306/testdatabase"; //数据库连接字符串
String username ="test"; //数据库用户名
String password = "123456"; //数据库密码
Connection conn = DriverManager.getConnection(url,username,password); //创建Connection连接
if(conn !=null){ //判断Connection对象是否为空
out.print("数据库连接成功!"); //不为空 输出"数据库连接成功"
conn.close(); //关闭连接
}else{
out.print("数据库连接失败"); //输出数据库连接失败
}

}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}




JDBC API:

Connection接口的方法声明和说明
1. void close() throws SQLException
立即释放Connection对象的数据库连接占用的JDBC 资源,在操作数据库后, 应立即调用此方法

2.void commit() throws SQLException
提交事务,并释放Connection对象当前持有的所有数据库锁,当事务被设置为手动提交模式时,需要调用该方法提交事务

3.Statement createStatement()throws SQLException
创建一个Statement对象來将SQL语句发送到数据库,该方法返回Statement对象

4.boolean getAutoCommint()throws SQLException
用于判断Connection对象是否被设置为自动提交模式,该方法返回布尔值

5.DatabaseMetaData getMetData() throws SQLException
获取Connection对象所连接的数据库的元素DatabaseMetaData对象,元数据包括关于数据库的表、受支持的SQL语法、存储过程、此链接功能等信息

6.int getTransactionIsolation () throws SQLException
获取Connection对象的当前事物隔离级别

7. boolean isClosa() throws SQLException
判断Connection对象是否与数据库断开连接,该方法返回布尔值,需要注意的是,如果connection 对象与数据库断开连接,
则不能再通过Connection对象操作数据库

8.boolean isReadOnly() throws SQLException

判断Connection对象是否为只读模式,该方法返回布尔值

9.PreparedStatement preparedStatement(String sql)throws SQLException
将参数化的SQL语句预编译并存储在PreparedStatement对象中,并返回所常见的这个PreparedStatement对象

10.void releaseSavepoint(Savepoint savepoint)throws SQLException
从当前事物中移除 指定的Savepoint 和后续Savepoint对象

11.void rollback() throws SQLException
回滚事物,并释放Connection对象当前持有的所有数据库锁,注意该方法需要应用于Connection对象的手动提交模式

12.void setAutoCommit(boolean autoCommit)throws SQLException
设置Connection对象的提交模式,如果参数autoCmmit的值设置为true,Connection对象则为自动提交模式,如果参数autoCmmit的值设置为false,
Connection对象则为手动提交模式

13.void setReadOnly(boolean ReadOnly)throws SQLException
将Connection对象的连接模式设置为只读,该方法用于对数据库的优化

14.Savepoint setSavepoint()throws SQLException
在当前事物中创建一个未命名的保留点,并返回这个保留点的对象

15.Savepoint setSavepoint(String name)throws SQLException
在当前事物中创建一个指定名称的保留点,并返回这个保留点的对象

16.void setTransactionIsolation(int level)throws SQLException
设置Connection对象的事物隔离级别



2.DriverManager类
1.public static void deregisterDriver(Driver driver)throws SQLException
从 DriverManager的管理列表中删除一个驱动程序,参数driver为要删除的驱动对象

2.public static Connection getConnection(String url)throws SQLException
根据指定的数据库连接URL,简历与数据库连接Connection,参数url为数据库连接URL

3.public static Connection getConnection(String url,Properties info)throws SQLException
根据指定的数据库连接URL及数据库连接属性信息建立数据库连接Connection。参数url为数据库URL,参数info为数据库连接属性

4.public static Connection getConnection(String url,String user,String password)throws SQLException
根据指定的数据库连接URL用户名和密码建立数据库连接Connection。参数url为数据库URL,参数user为数据库连接数据库的用户名
参数password为连接数据库的密码

5.public static Enumeration<Driver>getDriver()
获取当前Drivermanager中已加载的所有的驱动程序,他的返回值是Enumeration

6.public static void registerDriver(Driver driver)throws SQLException
箱Drivermanager注册一个驱动对象,参数driver为要注册的驱动


3.Statement接口
Statement接口的方法和说明

1.void aadBatch(String sql) throws SQLException
将SQL语句添加到Statement对象的当前命令列表中,该方法用于SQL命令的批量处理

2.void clearBatch ()throws SQLException
清空Statement对象中的命令列表

3.void close()throws SQLException
立即释放Statement对象的数据库和JDBC资源,而不是等待该对象自关闭时发生此操作

4. boolean execute (Sting sql)throws SQLException
执行指定的SQL语句,如果SQL语句返回结果,该方法返回true,反之返回false

5.int[]executeBatch()throws SQLException
将一批SQL命令提交给数据库执行,返回更新技术组成的数组

6.ResultSet execute Query(String sql)throws SQLException
执行查询类型(select)de SQL语句,该方法返回查询所获取的结果集ResuktSet对象

7.executeupdate int executeupdate(String sql)throws SQLException
执行SQL语句中DML类型(insert uodate delete)的SQL语句,返回更新所影响的行数

8.Connection getConnection() throws SQLException
获取生成Statement 对象的Connection

9.boolean isClosa() throws SQLException
判断Statement对象是否被关闭,如果关闭,则不能再调用该statement对象执行SQL语句,该方法返回布尔值



4. PreparedStatement接口
PreparedStatement接口的方法和说明
1.void setBinaryStream(int paramenterIndex,InputStream x)throws SQLException
将输入流x作为SQL语句中的参数值,paramenterIndex为参数位置的索引

2.void setBoolean(int paramenterIndex , boolean x)throws SQLException
将布尔值x作为SQL语句中的参数值,paramenterIndex为参数位置的索引

3.void setByte(int paramenterIndex, byte x)throws SQLException
将byte值x作为SQL语句中的参数值,paramenterIndex为参数位置的索引

4.void setDate(int paramenterIndex, Date x)throws SQLException
将java.sql.Date值x作为SQL语句中的参数值,paramenterIndex为参数位置的索引

5.void setDouble(int paramenterIndex, Double x)throws SQLException
将double值x作为SQL语句中的参数值,paramenterIndex为参数位置的索引

6.void setFloat(int paramenterIndex, Float x)throws SQLException
将Float值x作为SQL语句中的参数值,paramenterIndex为参数位置的索引

7.void setInt(int paramenterIndex, int x)throws SQLException
将Int值x作为SQL语句中的参数值,paramenterIndex为参数位置的索引

8.void setInt(int paramenterIndex, long x)throws SQLException
将long值x作为SQL语句中的参数值,paramenterIndex为参数位置的索引

9.void setObject(int paramenterIndex, Object x)throws SQLException
将Objiect对象x作为SQL语句中的参数值,paramenterIndex为参数位置的索引

10.void setShort(int paramenterIndex, Short x)throws SQLException
将Short值x作为SQL语句中的参数值,paramenterIndex为参数位置的索引

11.void setString(int paramenterIndex, String x)throws SQLException
将String值x作为SQL语句中的参数值,paramenterIndex为参数位置的索引

12.void setTimestamp(int paramenterIndex, Timestamp x)throws SQLException
将Timestamp值x作为SQL语句中的参数值,paramenterIndex为参数位置的索引



5.ResultSet接口
ResultSet接口的方法和说明

1.boolean absolute(int row)throws SQLException
将光标移动到ResultSet对象给定的航编号,参数row为航编号

2.void afterLast() throws SQLException
将光标移动到ResultSet对象的最后一行之后,如果结果集中不包含任何行,则该方法无效

3.void beforeFirst()throws SQLException
立即释放ResultSet对象的数据库和JDBC资源

4.void deteleRow() throws SQLException
从ResultSet对象和底层数据库中删除当前行

5.boolean first() throws SQLException
将光标移动到ResultSet对象的第一行

6.InputStream getBinaryStream(String columnLabel)throws SQLException
以byte流的方式获取ResultSet对象当前行中指定列的值,参数columnLabel为列的名称

7.Date getDate(String columnLabel)throws SQLException
以java.sql.Date 的方式获取ResultSet对象当前行中指定列的值,参数columnLabel为列的名称

8.doublee getDouble(String columnLabel)throws SQLException
以double的方式获取ResultSet对象当前行中指定列的值,参数columnLabel为列的名称

9.float getFloat(String columnLabel)throws SQLException
以float 的方式获取ResultSet对象当前行中指定列的值,参数columnLabel为列的名称

10.int getInt(String columnLabel)throws SQLException
以int的方式获取ResultSet对象当前行中指定列的值,参数columnLabel为列的名称

11.String getString(String columnLabel)throws SQLException
以String 的方式获取ResultSet对象当前行中指定列的值,参数columnLabel为列的名称

12.boolean isClose()throws SQLException
判断当前的ResultSet对象是否关闭

13.boolean last() throws SQLException
将光标移到ResultSet对象的最后一行

14.boolean next()throws SQLException
将光标位置向后移动一行,如果移动的新航有效返回值为true,否则饭后false

15.boolean previous()throws SQLException
将光标位置向前移动一行,如移动的新行有效返回true,否则返回false

JDBC技术基础整理