java連接mysql數(shù)據(jù)庫 java連接sql server數(shù)據(jù)庫
更新時(shí)間:2017年02月14日 08:57:44 作者:15191806282
這篇文章主要為大家詳細(xì)介紹了java連接mysql數(shù)據(jù)庫,以及java連接sql server數(shù)據(jù)庫,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
在java的應(yīng)用中,我們經(jīng)常會(huì)對(duì)數(shù)據(jù)庫進(jìn)行必要的操作,下來我們就了解一下如何用java連接mysql數(shù)據(jù)庫 以及java連接sql server數(shù)據(jù)庫
一、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ù)庫名(必須指定)
String url = "jdbc:mysql://localhost:3306/test1";
//與數(shù)據(jù)庫建立連接
connection = DriverManager.getConnection(url, "root", "123456");
//創(chuàng)建一個(gè)Statement對(duì)象將SQL語句發(fā)送到數(shù)據(jù)庫
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í)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- java連接sql server 2008數(shù)據(jù)庫代碼
- java連接SQL Server數(shù)據(jù)庫的方法
- 用Java連接sqlserver數(shù)據(jù)庫時(shí)候幾個(gè)jar包的區(qū)別分析
- java 連接sql server2008數(shù)據(jù)庫配置
- java連接SQL?Server數(shù)據(jù)庫的超詳細(xì)教程
- Java連接sqlserver2008數(shù)據(jù)庫代碼
- Java中String的JdbcTemplate連接SQLServer數(shù)據(jù)庫的方法
- 通過Java連接SQL?Server數(shù)據(jù)庫的超詳細(xì)操作流程
相關(guān)文章
關(guān)于SQL表中drop?table和delete?table的區(qū)別
刪表是一個(gè)比較危險(xiǎn)的操作,這次給了個(gè)機(jī)會(huì)就想嘗試下,記得在mysql表中有兩種操作,drop與delete,但是在maxcompute產(chǎn)品中嘗試時(shí),該產(chǎn)品只支持drop操作。這里說下二者操作的區(qū)別,需要的朋友可以參考下2023-01-01
Sql Server 2012 分頁方法分析(offset and fetch)
最近在分析 Sql Server 2012 中 offset and fetch 的新特性,發(fā)現(xiàn) offset and fetch 無論語法的簡(jiǎn)潔還是功能的強(qiáng)大,都是相當(dāng)相當(dāng)不錯(cuò)的2012-08-08
SQL Server附加數(shù)據(jù)庫及出現(xiàn)5123錯(cuò)誤的解決辦法
在SQL中,可以通過附加數(shù)據(jù)庫的方式將一個(gè)已經(jīng)存在的數(shù)據(jù)庫添加到服務(wù)器上,本文主要介紹了SQL Server附加數(shù)據(jù)庫及出現(xiàn)錯(cuò)誤的解決辦法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04

