import java.sql.*;
成都創(chuàng)新互聯(lián)公司"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機(jī)版的企業(yè)網(wǎng)站。實(shí)現(xiàn)跨屏營(yíng)銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動(dòng)網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營(yíng)銷需求!成都創(chuàng)新互聯(lián)公司具備承接各種類型的網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站項(xiàng)目的能力。經(jīng)過10多年的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評(píng)。
public class SqlUtil
{
public static void main(String[] args)
{
try
{
String url="jdbc:mysql://localhost/mydb";
String user="root"; //用戶名
String pwd="123456"; //密碼
//加載驅(qū)動(dòng),這一句也可寫為:Class.forName("com.mysql.jdbc.Driver");
Class.forName("com.mysql.jdbc.Driver").newInstance();
//建立到MySQL的連接
Connection conn = DriverManager.getConnection(url,user, pwd);
//執(zhí)行SQL語(yǔ)句
Statement stmt = conn.createStatement();//創(chuàng)建語(yǔ)句對(duì)象,用以執(zhí)行sql語(yǔ)言
ResultSet rs = stmt.executeQuery("select * from student");
//處理結(jié)果集
while (rs.next())
{
String name = rs.getString("name");
System.out.println(name);
}
rs.close();//關(guān)閉數(shù)據(jù)庫(kù)
conn.close();
}
catch (Exception ex)
{
System.out.println("Error : " + ex.toString());
}技術(shù)問題可到itjob技術(shù)群交流
}
}
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官網(wǎng)下載MySql.Data.dll,然后添加引用
using MySql.Data.MySqlClient;
然后使用連接字符串的時(shí)候,使用以下代碼:
MySqlConnectionStringBuilder s = new MySqlConnectionStringBuilder();
s.Server = "192.168.0.100";
s.Port = 3306; //mysql端口號(hào)
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" 語(yǔ)法介紹: (1)、driver={SQL Server};始終為這個(gè)形式,不變 (2)、server:可以是local