《数据库系统概论》第八章数据库编程 ODBC 和 JDBC
程序员文章站
2022-05-02 20:25:30
...
第八章 数据库编程
ODBC
代码参考:https://blog.csdn.net/buptlihang/article/details/80275641
#include "stdafx.h"
#include <windows.h>
#include <iostream>
#include <sql.h>
#include <sqlext.h>
#include <sqltypes.h>
#include <string.h>
using namespace std;
int main()
{
SQLHENV serverhenv; //环境句柄
SQLHDBC serverhdbc; //连接句柄
SQLHSTMT serverhstmt;//语句句柄
SQLRETURN ret; //结果句柄
SQLCHAR Sno[10] = { 0 }, Sname[10] = { 0 }, Grade[5] = {0};
SQLLEN length;
//分配环境句柄
ret = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &serverhenv);
//设置环境句柄
ret = SQLSetEnvAttr(serverhenv,SQL_ATTR_ODBC_VERSION,(void*)SQL_OV_ODBC3,0);
if (!SQL_SUCCEEDED(ret)) {
cout << "AllocEnvHandle error!" << endl;
system("pause");
}
//分配连接句柄
ret = SQLAllocHandle(SQL_HANDLE_DBC, serverhenv, &serverhdbc);
if (!SQL_SUCCEEDED(ret)) {
cout << "AllocDbcHandle error!" << endl;
system("pause");
}
//连接数据库
ret = SQLConnect(serverhdbc, (SQLWCHAR *)"ODBC_TEST", SQL_NTS, (SQLWCHAR*)"root", SQL_NTS, (SQLWCHAR*)"root", SQL_NTS);
if (!SQL_SUCCEEDED(ret)) {
cout << "SQL_Connect error!" << endl;
system("pause");
}
//分配执行语句句柄
ret = SQLAllocHandle(SQL_HANDLE_STMT, serverhdbc, &serverhstmt);
//执行SQL语句
ret = SQLExecDirect(serverhstmt,(SQLWCHAR*)"INSERT INTO TEST VALUES(160004,'王兰花',120);", SQL_NTS);
ret = SQLExecDirect(serverhstmt, (SQLWCHAR*)"SELECT Sno,Sname,Grade FROM TEST;", SQL_NTS); //查询结果也可能为空
if (ret == SQL_SUCCESS || ret == SQL_SUCCESS_WITH_INFO) {
//绑定数据(定义游标区,让游标知道怎么去移动取数据,比如移动Sno长度就是取Sno,移动了length就是取了一行数据)
SQLBindCol(serverhstmt, 1, SQL_C_CHAR, (void*)Sno, sizeof(Sno), &length); //推测这里length是一行的长度,每次就要加上去
SQLBindCol(serverhstmt, 2, SQL_C_CHAR, (void*)Sname, sizeof(Sname), &length);
SQLBindCol(serverhstmt, 3, SQL_C_CHAR, (void*)Grade, sizeof(Grade), &length);
//将光标移动到下一行,获取下一行数据
while (SQL_NO_DATA != SQLFetch(serverhstmt)) {
//只要还有数据就向后推进
cout << "学生学号:" << Sno << " 学生姓名:" << Sname << " 学生成绩:" << Grade << endl;
}
}
//释放语句句柄
ret = SQLFreeHandle(SQL_HANDLE_STMT, serverhstmt);
if (SQL_SUCCESS != ret && SQL_SUCCESS_WITH_INFO != ret) //如果没有显示成功并且没有显示有数据就表示出错
cout << "free hstmt error!" << endl;
//断开数据库连接
ret = SQLDisconnect(serverhdbc);
if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO) //同上
cout << "disconnect error!" << endl;
//释放连接句柄
ret = SQLFreeHandle(SQL_HANDLE_DBC, serverhdbc);
if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
cout << "free hdbc error !" << endl;
//释放环境句柄
ret = SQLFreeHandle(SQL_HANDLE_ENV, serverhenv);
if (ret != SQL_SUCCESS && ret != SQL_SUCCESS_WITH_INFO)
cout << "free henv error !" << endl;
system("pause");
return 0;
}
JDBC
参考代码:http://www.cnblogs.com/wuyuegb2312/p/3872607.html
https://blog.csdn.net/wk992337444/article/details/79279276
首先要下载一个mysql的jdbc驱动程序,也就是一个jar包,放到如下的路径中,也就是java根目录的放lib库的目录
我使用的是idea,需要先将这个驱动程序导入,具体的步骤是 File -> Project Struct -> Modules -> Dependencies -> 右边的加号,然后把上图这个选中的包导入就OK了。
PS:JDBC访问数据库时候更新删除和修改操作要调用 Statement对象的ExecuteUpdate()方法,最基本的参数就是sql语句,返回的是int类型变量
进行查询的时候要调用Statement对象的ExecuteQuery()方法,返回的是查询结果集
package com.mysql;
import java.sql.*;
public class sql_test {
public static void main(String[] args){
Connection conn=null; //初始化连接对象
Statement statement=null;//初始化语句对象
ResultSet res=null; //初始化结果集对象
int success;
//连接数据库
try{
Driver driver = new com.mysql.jdbc.Driver();
DriverManager.registerDriver(driver); //注册驱动器
String url = "jdbc:mysql://localhost:3306/jdbc_test";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url,user,password);
}catch(SQLException e){
e.printStackTrace();
}
//插入操作
try{
//获取数据操作的对象
statement = conn.createStatement();
String sql_insert = "insert into student values(160003,'小李',142)"; //插入语句
success = statement.executeUpdate(sql_insert);
}catch(SQLException e){
e.printStackTrace();
}
//查询操作
try{
statement = conn.createStatement();
String sql_query = "select * from student";
res = statement.executeQuery(sql_query);
while(res.next()){
//每当还有数据的时候
int sno = res.getInt("Sno");
String sname = res.getString("Sname");
int grade = res.getInt("Grade");
System.out.println(sno+ " " + sname + " " + grade);
}
}catch (SQLException e){
e.printStackTrace();
}
//删除操作
try{
statement = conn.createStatement();
String sql_delete = "delete from student where Sno = 160001";
success = statement.executeUpdate(sql_delete);
}catch(SQLException e){
e.printStackTrace();
}
//修改操作
try{
statement = conn.createStatement();
String sql_update = "update student set Grade=145 where Sno=160002";
success = statement.executeUpdate(sql_update);
}catch(SQLException e){
e.printStackTrace();
}
//最后再查询
try{
statement = conn.createStatement();
String sql_query = "select * from student";
res = statement.executeQuery(sql_query);
System.out.println("最后的一次查询,看看前面的操作正确执行了没");
while(res.next()){
int sno = res.getInt("Sno");
String sname = res.getString("Sname");
int grade = res.getInt("Grade");
System.out.println(sno+ " " + sname + " " + grade);
}
}catch(SQLException e){
e.printStackTrace();
}
//断开连接
try{
res.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
最后的结果:
下一篇: [转] Maven 环境搭建