重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
import java.sql.*;
成都创新互联公司"三网合一"的企业建站思路。企业可建设拥有电脑版、微信版、手机版的企业网站。实现跨屏营销,产品发布一步更新,电脑网络+移动网络一网打尽,满足企业的营销需求!成都创新互联公司具备承接各种类型的网站设计制作、成都做网站项目的能力。经过10多年的努力的开拓,为不同行业的企事业单位提供了优质的服务,并获得了客户的一致好评。
public class SqlUtil
{
public static void main(String[] args)
{
try
{
String url="jdbc:mysql://localhost/mydb";
String user="root"; //用户名
String pwd="123456"; //密码
//加载驱动,这一句也可写为:Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.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());
}技术问题可到itjob技术群交流
}
}
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.com;" +
"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();
首先呢,去Mysql官网下载MySql.Data.dll,然后添加引用
using MySql.Data.MySqlClient;
然后使用连接字符串的时候,使用以下代码:
MySqlConnectionStringBuilder s = new MySqlConnectionStringBuilder();
s.Server = "192.168.0.100";
s.Port = 3306; //mysql端口号
s.Database = "dbName";
s.UserID = "root";
s.Password = "root";
s.CharacterSet = "latin1";
MySqlConnection mcon = new MySqlConnection(s.ConnectionString);
其他的也要用mysql的,比如MySqlCommand等等。有疑问可以追问~
要先包含System.Date类。 连接字符串以及解释如下: connstr="driver={SQL Server};server=(local);uid=sa;pwd=sa;database=Your database" 语法介绍: (1)、driver={SQL Server};始终为这个形式,不变 (2)、server:可以是local