Java database programming
1. Relational database system
1. Structure of database system
2. Relational database system
In a relational database system, a row is a tuple, and a column represents an attribute
Two.SQL
1. First enter the bin directory where the MySQL file is located on the command line
2. Log in to the database with root privileges
2. Check which databases are there, among which MySql and Test are necessary, MySql is used to store server information and other user information tables; used to create users, grant permissions, revoke users, and create for administrators; Test database is used Store data or create a new database.
3. Create a user named "Bob" and a password of "123"
4.
Grant permissions 5. Grant privileges for access from any IP
6. Grant privileges for remote access from a specific IP
7. Use Bob to log in to
other common MySql commands :
exit;//退出
net stop mysql;//停止mysql服务
net start mtsql;//开启mysql服务
source script.sql;//运行sql脚本文件
\c //退出当前命令,重新输入
show grants;//查看权限
Three. JDBC
1. JDBC is an API for accessing relational databases, providing an interface for accessing and manipulating databases.
2. Four interfaces needed to develop database applications in Java:
Driver//加载一个合适的驱动
Connection//连接到数据库
Statement//创建和运行SQL语句
ResultSet//处理结果
Four typical steps to access the database
[Extension] Add the mysql jar package to EclipseIDE, you can access to import the jar package into eclipse
1. Load the appropriate driver
The database I use is mySql, so load the MySql database driver
public static void main(String[] args) {
try {
//第一步:加载合适的驱动
Class.forName("com.mysql.jdbc.Driver");
//下面打印语句测试驱动是否加载成功,可选择性添加
System.out.println("驱动加载成功!");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
2. Create a connection
//第二步:建立连接
Connection connection=DriverManager.getConnection("jdbc:mysql://192.168.0.103/javabook","Bob","123");
//Javabook数据库是登录Bob用户新创建的
【Extension】
database | URL pattern |
---|---|
Access | jdbc:odbc:dataSource |
MySql | jdbc:mysql://hostname/dbname |
Oracle | jdbc:oracle:thin:@hostname:port#:oracleDBSID |
3. Create SQL statement
Without parameters: Statement interface
//第三步:创建SQL语句
Statement statement=connection.createStatement();
With or without parameters can be used: PreparedStatement interface
PreparedStatement ps=connection.prepareStatement("select firstName,lastName from student where ssn=?");
[Note] The SQL statement must be written to use the PreparedStatement interface. (Question mark) represents a placeholder, pass the value through the setter modifier
ps.setString(1,"444111110");//1代表传给第几个参数,后面的代表传的内容
The stored procedure used to execute SQL: CallableStatement interface
CallableStatement cs=connection.prepareCall("{call samplePrepare (?,?,?)}");///三个?代表转义语句;samplrPrepare是已经创建好的存储过程
Execution statement: Result Set interface
ResultSet rs=ps.executeQuery();//执行查询语句
ResultSet rs2=ps.executeUpdate();//执行更新语句
process result
Method: the current initial position is null, move through the next method
while(rs.next()){
System.out.println(rs.getString(1)+" "+rs.getString(2));//分别是firstName和lastName
}
Final running result
Five. Get metadata
- DatabaseMetaData interface: obtain database-wide information
DatabaseMetaData dmd=connection.getMetaData();
Result:
Not only these two information can be obtained, as long as the information is within the scope of the database.
- ResultSetMetaData interface: get result set metadata
ResultSetMetaData rsmd=rs.getMetaData();
for(int i=1;i<=rsmd.getColumnCount();i++){
System.out.printf("%-10s\t",rsmd.getColumnName(i));
}
Result:
【Supplement】
- Java6 does not need to explicitly load the driver
- The split method needs to be escaped when splitting "."
Come on!