1.JDBCd 使用步骤
1.指定JDBC驱动类名
String driverName = "com.mysql.jdbc.Driver
2.指定MySQL的JDBCURL
String connectionString = "jdbc:mysql://主机IP:3306/数据库名?"+"user=用户名&password=密码&useUnicode=true&charcterEncoding=utf-8"
3加载JDBC驱动类
Class.forName(driverName);
4.得到一个Connection连接
Connection connection = DriverManager.getConnection(connectionString);
5.得到一个用于执行SQL语句的Statement
Statement statement = connection.createStatement();
6.指定要执行的SQL语句
String SQL = "SQL语句";
7.执行SQL语句
- 1).执行插入、删除、更新语句:
int count = statment.executeUpdate(SQL);
返回受影响的行数
- 2)执行查询语句
.ResultSet resultSet = statment.executeQuery(SQL)
;
返回应该ResultSet类型的对象,就是一个数据表。
获取方法:
确定哪一行:next()方法
确定那一列:getString("列名") 或getString(列序号)根据返回类型选用getInt()、getDate()
当SQL语句的内容事先不知道的情况:
- PreparedStatement = connection.paepareStatement(SQL语句,不确定位置写?)
- 填内容:preparedStatement.setString(1,tille);
8.关闭Statement
preparedStatement.close()
9.关闭Connection
connection.close();