java怎么连接mysql数据库

java怎么连接mysql数据库,第1张

连接数据库简单步骤:

导入驱动包(mysql官网有下),并添加该包为库(add as Library)

注册驱动:Class.forName("com.mysql.jdbc.Driver")

获取连接对象:Connection conn=DirverManager.getConnection("jdbc:mysql://连接IP/数据库名","数据库用户名","密码")

获取sql语句执行对象:Statement stmt=conn.createStatement()

使用对象运行sql语句:如 stmt.executeUpdate("Update A set a=b")

释放资源:stmt.close()conn.close()

也可以用数据库连接池,获得连接,但是要下载相关的jar包,如:Druid.jar

要想写更简单的代码,可以用spring框架,下载相应的jar包就可以了

1.加载驱动2.链接数据库3.执行命令;4.关闭数据库 import java.sql.* class MysqlConnection { /*要是更换数据库,就直接更换这些语句就行了,main函数的那些都不用动的,主要是驱动(DBDRIVER)和链接方式(DBURL)*/ private static String DBDRIVER = "org.gjt.mm.mysql.Driver"//这个是与下载jdbc-mysql里面的那个driver.class文件是对应的,你 //可以解压找下,会发觉驱动就是那个鬼东西的。。 private static String DBURL = "jdbc:mysql://localhost:3306/study"/* jdbc:mysql://localhost:3306:test这句里面分如下解析: jdbc:mysql:// 是指JDBC连接方式; localhost: 是指你的本机地址; 3306SQL数据库的端口号; study 就是你要连接的数据库的地址。 你可以试下不要这个'study',或者胡乱接一个不存在的数据库, 然后还可以执行下面语句来实现连接数据库(a) */ private static String DBUSER = "scott" private static String DBPASSWORD = "tiger" public static void main(String[] args)throws Exception { Class.forName(DBDRIVER)//1.加载驱动 Connection conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD)//2.获得链接 Statement statement = conn.createStatement()//3.执行命令 //statement.executeUpdate("use study")//(a)要是没有上面的那个数据库,就要使用这个函数来连接数据库 ResultSet result = statement.executeQuery("SELECT * FROM emp")//结果收集,迭代 while(result.next()){ printf(result.getObject(1)+" ") printf(result.getObject(2)+" ") printf(result.getObject(3)+" ") printf(result.getObject(4)+"\n") } conn.close() } public static void printf(Object obj){ System.out.print(obj) } public static void printfln(Object obj){ System.out.println(obj) } }

java连线Mysql的连线字串怎么写的

import java.sql.*public class SqlUtil{ public static void main(String[] args) {try{ String url="jdbc:mysql:localhost/mydb" String user="root"使用者名称 String pwd="123456"密码 载入驱动,这一句也可写为:Class.forName(".mysql.jdbc.Driver")Class.forName(".mysql.jdbc.Driver").newInstance() 建立到MySQL的连线 Connection conn = DriverManager.getConnection(url,user, pwd) 执行SQL语句 Statement stmt = conn.createStatement()建立语句物件,用以执行sql语言 ResultSet rs = stmt.executeQuery("select * from student") 处理结果集 while (rs.next()) {String name = rs.getString("name") System.out.println(name) } rs.close()关闭资料库 conn.close() }catch (Exception ex){ System.out.println("Error : " + ex.toString()) } }}

jdbc:mysql:localhost:3306/sample_db?user=root&password=browser&useUnicode=true&characterEncoding=UTF-8

汇入mysql的驱动Jar包

Class.forName(".mysql.jdbc.Driver")

Connection con = null

String url = "资料库的url"

String user = "资料库登入名"

String password = "资料库密码"

con = DriverManager.getConnection(url, user, password)

oracle.jdbc.driver.OracleDriver

jdbc:oracle:thin:@127.0.0.1:1521:ORCL

字串内容

jdbc:mysql:localhost/资料库名称,“登入名”,“密码”

mysql cluster的hibernate连线字串怎么写

Driver template :选择要连线资料库型别我用mysql选择MYSQL Connector/J Driver name :所建立连线名字,随便写

Driver classname:.mysql.jdbc.Driver要Add JARs选择 .mysql.jdbc.Driver路径显示

使用oracle参考我面式

Dreamweaver连线MySql的连线字串

在Dreamweaver里连线mysql可以不用写连线字串的。

1、建立好站点

2、编辑站点属性-->测试伺服器,右边伺服器模型和访问选择:PHP MySQL,本地/网路

3、应用程式-->资料库一栏里点加号建立MySQL连线

4、连线名称(随意),MySQL伺服器(localhost),使用者名称(root),密码(*******),资料库(你要连线的资料库名,键入或选取都行)

5、测试连线-->确定,即可。

c#连线到mysql 的连线字串怎么写

ODBC DSN

ODBC -- MyODBC Driver -- local database

ODBC -- MyODBC Driver -- remote database

ODBC -- MySQL ODBC 3.51 Driver

OleDb

.NET DataProvider from CoreLab

ODBC DSN

using System.Data.Odbc

OdbcConnection conn = new OdbcConnection()

conn.ConnectionString =

"Dsn=DsnName" +

"Uid=UserName" +

"Pwd=Secret"

conn.Open()

ODBC -- MyODBC Driver -- local database

using System.Data.Odbc

OdbcConnection conn = new OdbcConnection()

conn.ConnectionString =

"Driver={MySql}" +

"Server=localhost" +

"Option=16834" +

"DataBase=DataBaseName"

conn.Open()

ODBC -- MyODBC Driver -- remote database

using System.Data.Odbc

OdbcConnection conn = new OdbcConnection()

conn.ConnectionString =

"Driver={MySql}" +

"Server=db.domain." +

"Option=131072" +

"Port=3306" +

"Stmt=" +

"DataBase=DataBaseName" +

"Uid=UserName" +

"Pwd=Secret"

conn.Open()

ODBC -- MySQL ODBC 3.51 Driver

using System.Data.Odbc

OdbcConnection conn = new OdbcConnection()

conn.ConnectionString =

"Driver={MySql ODBC 3.51 Driver}" +

"Server=ServerName" +

"Option=16834" +

"Port=3306" +

"Stmt=" +

"DataBase=DataBaseName" +

"Uid=UserName" +

"Pwd=Secret"

conn.Open()

or

using System.Data.Odbc

OdbcConnection conn = new OdbcConnection()

conn.ConnectionString =

"DRIVER={MySql ODBC 3.51 Driver}" +

"SERVER=ServerName" +

"DATABASE=DataBaseName" +

"USER=UrerName" +

"PASSWORD=Secret"

conn.Open()

OleDb

using System.Data.OleDb

OleDbConnection conn = new OleDbConnection()

conn.ConnectionString =

"Provider=MySqlProv" +

"Data Source=ServerName" +

"User id=UserName" +

"Password=Secret"

conn.Open()

.NET DataProvider from CoreLab

using CoreLab.MySql

MySqlConnection conn = new MySqlConnection()

conn.ConnectionString =

"Host=ServerName" +

"DataBase=DataBaseName" +

"Protocol=TCP" +

"Port=3306" +

"Direct=true" +

"Compress=false" +

"Pooling=true" +

"Min Pool Size=0" +

"Max Pool Size=100" +

"Connection Lifetime=0" +

"User id=UserName" +

"Password=Secret" +

conn.Open()

Server=localhostDatabase=drive_baseUser=otnp80Password=123Use Procedure Bodies=falseCharset=utf8Allow Zero Datetime=TruePooling=falseMax Pool Size=50


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6170543.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-17
下一篇 2023-03-17

发表评论

登录后才能评论

评论列表(0条)

保存