连接mysql数据库语句
首先创建连接
就创建个Cnnection对象,然后把驱动加上,还有帐号密码什么的!我没连过sql数据库,不知道驱动,你可以在网上搜一下。
给你一个我连接mysql数据库的例子
public Connection Getdata(){//这是连接数据库的一个方法,还可以配置连接池。
Connection con=null;
try {
Class.forName("com.mysql.jdbc.Driver");//加驱动
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/data","root","qq");//数据库路径、端口、库名、数据库用户名和密码
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return con;
}
public void delete(int id){//对数据库进行操作的方法,先调用上面写好的连接!
Connection con=Getdata();
Statement st;
try {
st=con.createStatement();
st.executeUpdate("delete from kaoshi where id="+id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
‘贰’ 如何用python连接mysql数据库
在 Python 语言环境下我们这样连接数据库。
In [1]: from mysql import connector
In [2]: cnx = connector.connect(host="172.16.192.100",port=3306,user="appuser",password="xxxxxx")
但是连接数据库的背后发生了什么呢?
答案
当我们通过驱动程序(mysql-connector-python,pymysql)连接 MySQL 服务端的时候,就是把连接参数传递给驱动程序,驱动程序再根据参数会发起到 MySQL 服务端的 TCP 连接。当 TCP 连接建立之后驱动程序与服务端之间会按特定的格式和次序交换数据包,数据包的格式和发送次序由MySQL 协议规定。MySQL 协议:https://dev.mysql.com/doc/internals/en/client-server-protocol.html整个连接的过程中 MySQL 服务端与驱动程序之间,按如下的次序发送了这些包。
MySQL 服务端向客户端发送一个握手包,包里记录了 MySQL-Server 的版本,默认的授权插件,密码盐值(auth-data)。
2. MySQL 客户端发出 ssl 连接请求包(如果有必要的话)。
3. MySQL 客户端发出握手包的响应包,这个包时记录了用户名,密码加密后的串,客户端属性,等等其它信息。
4. MySQL 服务端发出响应包,这个包里记录了登录是否成功,如果没有成功也会给出错误信息。
‘叁’ java怎么连接mysql数据库
这里介绍两种方式:
一,jdbc链接MySQL数据库:
1,如果你用jdbc方式,则按照下列方式进行连接:
A,注册驱动
B,链接数据库
C,执行sql
D,返回结果集
如下为一个基本完整流程:
packagecom.hu.demo;
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.PreparedStatement;
importjava.sql.SQLException;
publicclassDBHelper{
publicstaticfinalStringurl="jdbc:mysql://127.0.0.1/student";
publicstaticfinalStringname="com.mysql.jdbc.Driver";
publicstaticfinalStringuser="root";
="root";
publicConnectionconn=null;
publicPreparedStatementpst=null;
publicDBHelper(Stringsql){
try{
Class.forName(name);//指定连接类型
conn=DriverManager.getConnection(url,user,password);//获取连接
pst=conn.prepareStatement(sql);//准备执行语句
}catch(Exceptione){
e.printStackTrace();
}
}
publicvoidclose(){
try{
this.conn.close();
this.pst.close();
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
2,将注册,链接封装好,执行sql语句,返回结果集,代码如下:
packagecom.hu.demo;
importjava.sql.ResultSet;
importjava.sql.SQLException;
publicclassDemo{
staticStringsql=null;
staticDBHelperdb1=null;
staticResultSetret=null;
publicstaticvoidmain(String[]args){
sql="select*fromstuinfo";//SQL语句
db1=newDBHelper(sql);//创建DBHelper对象
try{
ret=db1.pst.executeQuery();//执行语句,得到结果集
while(ret.next()){
Stringuid=ret.getString(1);
Stringufname=ret.getString(2);
Stringulname=ret.getString(3);
Stringudate=ret.getString(4);
System.out.println(uid+" "+ufname+" "+ulname+" "+udate);
}//显示数据
ret.close();
db1.close();//关闭连接
}catch(SQLExceptione){
e.printStackTrace();
}
}
}
3,查询结果如下:
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!--配置数据源-->
<beanname="dataSource"class="com.alibaba.druid.pool.DruidDataSource"
init-method="init"destroy-method="close">
<propertyname="url"value="${jdbc_url}"/>
<propertyname="username"value="${jdbc_username}"/>
<propertyname="password"value="${jdbc_password}"/>
<!--初始化连接大小-->
<propertyname="initialSize"value="0"/>
<!--连接池最大使用连接数量-->
<propertyname="maxActive"value="20"/>
<!--连接池最小空闲-->
<propertyname="minIdle"value="0"/>
<!--获取连接最大等待时间-->
<propertyname="maxWait"value="60000"/>
<!--<propertyname="poolPreparedStatements"value="true"/><property
name=""value="33"/>-->
<propertyname="validationQuery"value="${validationQuery}"/>
<propertyname="testOnBorrow"value="false"/>
<propertyname="testOnReturn"value="false"/>
<propertyname="testWhileIdle"value="true"/>
<!--配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒-->
<propertyname="timeBetweenEvictionRunsMillis"value="60000"/>
<!--配置一个连接在池中最小生存的时间,单位是毫秒-->
<propertyname="minEvictableIdleTimeMillis"value="25200000"/>
<!--打开removeAbandoned功能-->
<propertyname="removeAbandoned"value="true"/>
<!--1800秒,也就是30分钟-->
<propertyname="removeAbandonedTimeout"value="1800"/>
<!--关闭abanded连接时输出错误日志-->
<propertyname="logAbandoned"value="true"/>
<!--监控数据库-->
<!--<propertyname="filters"value="stat"/>-->
<propertyname="filters"value="mergeStat"/>
</bean>
<!--myBatis文件-->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<!--自动扫描entity目录,省掉Configuration.xml里的手工配置-->
<propertyname="mapperLocations"value="classpath:com/fourfaith/*/mapping/*.xml"/>
</bean>
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="com.fourfaith.**."/>
<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"/>
</bean>
<!--配置事务管理器-->
<beanid="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<!--拦截器方式配置事物-->
<tx:adviceid="transactionAdvice"transaction-manager="transactionManager">
<tx:attributes>
<tx:methodname="add*"propagation="REQUIRED"/>
<tx:methodname="append*"propagation="REQUIRED"/>
<tx:methodname="insert*"propagation="REQUIRED"/>
<tx:methodname="save*"propagation="REQUIRED"/>
<tx:methodname="update*"propagation="REQUIRED"/>
<tx:methodname="modify*"propagation="REQUIRED"/>
<tx:methodname="edit*"propagation="REQUIRED"/>
<tx:methodname="delete*"propagation="REQUIRED"/>
<tx:methodname="remove*"propagation="REQUIRED"/>
<tx:methodname="repair"propagation="REQUIRED"/>
<tx:methodname="delAndRepair"propagation="REQUIRED"/>
<tx:methodname="import*"propagation="REQUIRED"read-only="false"
rollback-for="java.lang.Exception"/>
<tx:methodname="get*"propagation="SUPPORTS"/>
<tx:methodname="find*"propagation="SUPPORTS"/>
<tx:methodname="load*"propagation="SUPPORTS"/>
<tx:methodname="search*"propagation="SUPPORTS"/>
<tx:methodname="datagrid*"propagation="SUPPORTS"/>
<tx:methodname="*"propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcutid="transactionPointcut"
expression="execution(*com...*.service..*Impl.*(..))"/>
<aop:advisorpointcut-ref="transactionPointcut"
advice-ref="transactionAdvice"/>
</aop:config>
<!--配置druid监控springjdbc-->
<beanid="druid-stat-interceptor"
class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
</bean>
<beanid="druid-stat-pointcut"class="org.springframework.aop.support.JdkRegexpMethodPointcut"
scope="prototype">
<propertyname="patterns">
<list>
<value>com...*.service.*</value>
</list>
</property>
</bean>
<aop:config>
<aop:advisoradvice-ref="druid-stat-interceptor"
pointcut-ref="druid-stat-pointcut"/>
</aop:config>
</beans>
还有很多方式可以实现,这里就简略的描述一番。
‘肆’ 怎样连接mysql数据库java代码
首先你要导包
JDBC连接数据库
•创建一个以JDBC连接数据库的程序,包含7个步骤:
1、加载JDBC驱动程序:
在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机),
这通过java.lang.Class类的静态方法forName(String className)实现。
例如:
try{
//加载MySql的驱动类
Class.forName("com.mysql.jdbc.Driver") ;
}catch(ClassNotFoundException e){
System.out.println("找不到驱动程序类 ,加载驱动失败!");
e.printStackTrace() ;
}
成功加载后,会将Driver类的实例注册到DriverManager类中。
2、提供JDBC连接的URL
•连接URL定义了连接数据库时的协议、子协议、数据源标识。
•书写形式:协议:子协议:数据源标识
协议:在JDBC中总是以jdbc开始 子协议:是桥连接的驱动程序或是数据库管理系统名称。
数据源标识:标记找到数据库来源的地址与连接端口。
例如:
(MySql的连接URL)
jdbc:mysql: //localhost:3306/test?useUnicode=true&characterEncoding=gbk ;
useUnicode=true:
表示使用Unicode字符集。如果characterEncoding设置为 gb2312或GBK,本参数必须设置为true 。characterEncoding=gbk:字符编码方式。
3、创建数据库的连接
•要连接数据库,需要向java.sql.DriverManager请求并获得Connection对象, 该对象就代表一个数据库的连接。
•使用DriverManager的getConnectin(String url , String username , String password )方法传入指定的欲连接的数据库的路径、数据库的用户名和 密码来获得。
例如: //连接MySql数据库,用户名和密码都是root
String url = "jdbc:mysql://localhost:3306/test" ;
String username = "root" ;
String password = "root" ;
try{
Connection con = DriverManager.getConnection(url , username , password ) ;
}catch(SQLException se){
System.out.println("数据库连接失败!");
se.printStackTrace() ;
}
4、创建一个Statement
•要执行SQL语句,必须获得java.sql.Statement实例,Statement实例分为以下3 种类型:
1、执行静态SQL语句。通常通过Statement实例实现。
2、执行动态SQL语句。通常通过PreparedStatement实例实现。
3、执行数据库存储过程。通常通过CallableStatement实例实现。
具体的实现方式:
Statement stmt = con.createStatement() ; PreparedStatement pstmt = con.prepareStatement(sql) ; CallableStatement cstmt = con.prepareCall("{CALL demoSp(? , ?)}") ;
5、执行SQL语句
Statement接口提供了三种执行SQL语句的方法:executeQuery 、executeUpdate 和execute
1、ResultSet executeQuery(String sqlString):执行查询数据库的SQL语句 ,返回一个结果集(ResultSet)对象。
2、int executeUpdate(String sqlString):用于执行INSERT、UPDATE或 DELETE语句以及SQL DDL语句,如:CREATE TABLE和DROP TABLE等
3、execute(sqlString):用于执行返回多个结果集、多个更新计数或二者组合的 语句。 具体实现的代码:
ResultSet rs = stmt.executeQuery("SELECT * FROM ...") ; int rows = stmt.executeUpdate("INSERT INTO ...") ; boolean flag = stmt.execute(String sql) ;
6、处理结果 两种情况:
1、执行更新返回的是本次操作影响到的记录数。
2、执行查询返回的结果是一个ResultSet对象。
• ResultSet包含符合SQL语句中条件的所有行,并且它通过一套get方法提供了对这些 行中数据的访问。
• 使用结果集(ResultSet)对象的访问方法获取数据:
while(rs.next()){
String name = rs.getString("name") ;
String pass = rs.getString(1) ; // 此方法比较高效
}
(列是从左到右编号的,并且从列1开始)
7、关闭JDBC对象
操作完成以后要把所有使用的JDBC对象全都关闭,以释放JDBC资源,关闭顺序和声 明顺序相反:
1、关闭记录集
2、关闭声明
3、关闭连接对象
if(rs != null){ // 关闭记录集
try{
rs.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(stmt != null){ // 关闭声明
try{
stmt.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
if(conn != null){ // 关闭连接对象
try{
conn.close() ;
}catch(SQLException e){
e.printStackTrace() ;
}
}
‘伍’ 如何命令行连接mysql数据库
使用命令行连接MySQL数据库:
Windows操作系统下,开始——运行,打开"运行"对话框,输入cmd,点击“确定”即可进入DOS窗口。
DOS窗口输入登录MySQL数据库命令
mysql -h 127.0.0.1 -u root -p
命令参数说明:
mysql是登录数据库的命令,-h 后面跟服务器的IP,由于本示例MySql服务器安装在本地,因此IP地址为127.0.0.1;-u 后面跟用户名,本示例采用 root用户登录;-p 后面跟登录密码。
输入上述命令后回车,再输入登录密码,在回车即可完成登录MySQL数据库服务了。跟着可以运行use databaseName语句操作某个数据库了
‘陆’ 如何用C语言连接MYSQL数据库
1、配置ODBC数据源。
2、使用SQL函数进行连接。
对于1、配置数据源,配置完以后就可以编程操作数据库了。
对于2、使用SQL函数进行连接,参考代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<windows.h>
#include<sql.h>
#include<sqlext.h>
void main()
{
HENV henv; //环境句柄
HDBC hdbc; //数据源句柄
HSTMT hstmt; //执行语句句柄
unsigned char datasource[]="数据源名称"; //即源中设置的源名称
unsigned char user[]= "用户名"; //数据库的帐户名
unsigned char pwd[]= "密码"; //数据库的密码
unsigned char search[]="select xm from stu where xh=0";
SQLRETURN retcode; //记录各SQL函数的返回情况
// 分配环境句柄
retcode= SQLAllocEnv(&henv); // 等介于 SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL
, &henv);
// 设置ODBC环境版本号为3.0
retcode= SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void*)SQL_OV_ODBC3, 0);
// 分配连接句柄
retcode= SQLAllocConnect(henv,&hdbc); // 等介于 SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
//设置连接属性,登录超时为*rgbValue秒(可以没有)
// SQLSetConnectAttr(hdbc, SQL_LOGIN_TIMEOUT, (SQLPOINTER)(rgbValue), 0);
//直接连接数据源
// 如果是windows身份验证,第二、三参数可以是
‘柒’ js怎么连接mysql数据库连接
具体连接方法如下:
1、打开HBuilder工具,在Web项目中的js文件夹中,新建JavaScript文件ConnDB.js
使用require()引入mysql模块,然后赋值给变量mysql
‘捌’ jsp连接mysql数据库的语句怎么写
Class.forName("com.mysql.jdbc.Driver");
Connection
con=
DriverManager.getConnection("jdbc:mysql://10.10.2.188:3306/bbs?useUnicode=true&characterEncoding=utf8",
"root","yaoweijq");
Statement
stat=
con.createStatement();
第一句是加载数据库驱动,
自己下一个加到类库中,
我不知道要不要放到tomcat目录下。
第二句是连接数据库,
IP自己改,测试用localhost
3306是端口,mysql的
后面是数据库名你也可以用表名
再后面root是用户名yaoweijq是密码
都是mysql的
大体就是这样
‘玖’ 关于MYSQL数据库的连接语句~~麻烦大家帮帮忙
首先你多了个},再有$_SESSION那两句一定要放在echo那句的前面,另外如果你这个函数在类里面的就没问题,否则$this->name、$this->pwd可能得不到值
‘拾’ 如何使用python连接mysql数据库
在 Python 语言环境下我们这样连接数据库。
In [1]: from mysql import connector
In [2]: cnx = connector.connect(host="172.16.192.100",port=3306,user="appuser",password="xxxxxx")
但是连接数据库的背后发生了什么呢?
答案
当我们通过驱动程序(mysql-connector-python,pymysql)连接 MySQL 服务端的时候,就是把连接参数传递给驱动程序,驱动程序再根据参数会发起到 MySQL 服务端的 TCP 连接。当 TCP 连接建立之后驱动程序与服务端之间会按特定的格式和次序交换数据包,数据包的格式和发送次序由MySQL 协议规定。MySQL 协议:https://dev.mysql.com/doc/internals/en/client-server-protocol.html整个连接的过程中 MySQL 服务端与驱动程序之间,按如下的次序发送了这些包。
MySQL 服务端向客户端发送一个握手包,包里记录了 MySQL-Server 的版本,默认的授权插件,密码盐值(auth-data)。
2. MySQL 客户端发出 ssl 连接请求包(如果有必要的话)。
3. MySQL 客户端发出握手包的响应包,这个包时记录了用户名,密码加密后的串,客户端属性,等等其它信息。
4. MySQL 服务端发出响应包,这个包里记录了登录是否成功,如果没有成功也会给出错误信息。