java連接mysql數(shù)據(jù)庫(kù) java連接sql server數(shù)據(jù)庫(kù)
在java的應(yīng)用中,我們經(jīng)常會(huì)對(duì)數(shù)據(jù)庫(kù)進(jìn)行必要的操作,下來(lái)我們就了解一下如何用java連接mysql數(shù)據(jù)庫(kù) 以及java連接sql server數(shù)據(jù)庫(kù)
一、mysql
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestOne {
private static Connection connection;
private static Statement statement;
private static ResultSet result;
public static void main(String[] args) {
try {
//加載jdbc驅(qū)動(dòng)程序
Class.forName("com.mysql.jdbc.Driver");
//指明主機(jī)名(默認(rèn)為:127.0.0.1)和端口號(hào)(默認(rèn)為:3306)以及數(shù)據(jù)庫(kù)名(必須指定)
String url = "jdbc:mysql://localhost:3306/test1";
//與數(shù)據(jù)庫(kù)建立連接
connection = DriverManager.getConnection(url, "root", "123456");
//創(chuàng)建一個(gè)Statement對(duì)象將SQL語(yǔ)句發(fā)送到數(shù)據(jù)庫(kù)
statement = connection.createStatement();
//將查詢結(jié)果返回給result
result = statement.executeQuery("select *from user");
while(result.next()){
System.out.println("name:" + result.getString(1) + " password:" + result.getString(2));
}
connection.close();
result.close();
statement.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
if(connection != null)
connection.close();
if(result != null)
result.close();
if(statement != null)
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**
* mysql> select *from user;
*+----------+----------+
*| name | password |
*+----------+----------+
*| lisi | 123456 |
*| wangwu | 123456 |
*| zhangsan | 123456 |
*+----------+----------+
*3 rows in set (0.54 sec)
*
*在java中的輸出結(jié)果
*name:lisi password:123456
*name:wangwu password:123456
*name:zhangsan password:123456
*/
二、sql server
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDemo {
public static void main(String[] args) {
String url="jdbc:sqlserver://localhost:1433;DatabaseName=Contellation";
Connection conn = null;
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
conn = DriverManager.getConnection(url, "sa", "");
Statement statement=conn.createStatement();
ResultSet rs = statement.executeQuery("select * from dbo.登陸表 ");
while(rs.next()){
System.out.println("用戶名:" + rs.getString(1) + " 密碼:" + rs.getString(2));
}
conn.close();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* java中的輸出結(jié)果
* 用戶名:張三 密碼:123456
*用戶名:李四 密碼:111111
*用戶名:王五 密碼:123654
*用戶名:王延暾 密碼:0123456789
*用戶名:曾安新 密碼:123456
*/
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持本站。
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信