数据库学习笔记

来源:互联网 发布:jdbc连接数据库代码 编辑:程序博客网 时间:2024/06/05 08:26
ADO.net连接sqlserver数据库Data Source=localhost;Initial Catalog=DataBaseName;Integrated Security=SSPI;
1、连接mysql数据库
string strdriver="com.mysql.jdbc.Driver";
string strurl="jdbc:mysql://localhost:3306/student";student为要访问的数据库名;
string strname="root";
string strpassword="admin";
class.forname(strdriver)//加载jdbc驱动
connection conn=DriverManager.getconnection(strurl,strname,strpassword);//得到连接
操作数据库的两种方式:
1、statement st=conn.createstatement();
   resultset set=st.executequery(sql查询语句);//查询结果的数据集合,执行第一行的前一行;
   string str=set.getstring(index/"参数名")//获取当前查询结果中指定列的值;
   int i=st.executeupdate(insert、update、delete);//i为查询结果的个数;
2、preparedstatement ps=conn.preparestatement(sql语句)//推荐使用第二种方式
   resultset set=ps.executequery();当sql语句为查询语言时;
   int i=ps.executeupdate();当sql语句为delete、insert、update
   ps.setstring(index,str)//当sql语句需要传参数时,使用此词语传递参数;
0 0
原创粉丝点击