當前位置:首頁 » 數據倉庫 » java查詢sqlserver資料庫
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

java查詢sqlserver資料庫

發布時間: 2022-11-27 20:05:41

㈠ 如何用Java實現資料庫查詢

import java.sql.*;
public class MSSQLText
{
public static void main(String args[])
{
String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=Northwind";
String user="sa";//這里替換成你自已的資料庫用戶名
String password="sa";//這里替換成你自已的資料庫用戶密碼
String sqlStr="select CustomerID, CompanyName, ContactName from Customers";
try
{ //這里的異常處理語句是必需的.否則不能通過編譯!
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
System.out.println("類實例化成功!");

Connection con = DriverManager.getConnection(url,user,password);
System.out.println("創建連接對像成功!");

Statement st = con.createStatement();
System.out.println("創建Statement成功!");

ResultSet rs = st.executeQuery(sqlStr);
System.out.println("操作數據表成功!");
System.out.println("----------------!");

while(rs.next())
{
System.out.print(rs.getString("CustomerID") + " ");
System.out.print(rs.getString("CompanyName") + " ");
System.out.println(rs.getString("ContactName"));
}
rs.close();
st.close();
con.close();
}
catch(Exception err){
err.printStackTrace(System.out);
}
}
}

㈡ 如何用java 連接 sqlserver 資料庫

本文將介紹使用java連接sqlserver資料庫


工具/材料

myeclipse 、 SqlServer資料庫


方法:

1、要向連接資料庫,首先應該保證資料庫服務打開

2、資料庫服務打開之後就可以在環境中編寫連接代碼了。如圖:


連接資料庫就是這兩個步驟:1)載入驅動、2)創建連接。

注意在導包是導入的java.sql下的。

接下來直接運行一下就可以測試是否連接成功了

㈢ JAVA與SQLServer資料庫

如果編程風格跟閣下不同,請摘取有用的...
(需要驅動的JAR包 地址我就不在提供了 網上有)
1、
public final class ProUtil//這是一個終態工具類不能被繼承{
private static Connection conn=null;
private Connect()//私有的構造函數,對象不能被new出來{}
public static Connection getConnection()//返回一個連接對象{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
[conn=new Connection(); 這里需不需要new一下 我也不敢確定,我現在都是用中間件來連資料庫的,很少用JDBC]
conn = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;User=sa;Password=;DatabaseName=qian");
return conn;
}
}
2、
如果求和不用SUM的話 我就只有把數據都讀出來再加咯
得到COL1的值 用subString截取字元串 得到第一個字元 A、B
然後用 like 進行模糊查詢 分別得到所有以 A、B開頭的COL2數據 並保存在LIST裡面 然後對LIST進行循環+ 就可以得到所有以COL1首字元開頭的記錄的COL2的和了
3、跟2問差不多 就是把subString 的參數改一下就行了

希望對你有幫助

㈣ 如何用java 連接 sqlserver 資料庫

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");//載入資料庫驅動
Stringurl="jdbc:sqlserver://127.0.0.1:1433;databaseName=testDB";//IP:埠;databaseName:資料庫名
Stringsql="selectnamefromt_user";//sql查詢語句
Connectioncon=DriverManager.getConnection(url,username,password);//url:
資料庫連接串userName:資料庫登錄賬號passWord:資料庫登錄密碼
Statementstmt=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSetrs=stmt.executeQuery(sql);
while(rs.next()){
System.out.println(rs.getString("name"));
}

㈤ 如何通過JAVA操作SQLserver資料庫

之前遠標教育老師教我們這樣把sqlserver jdbc驅動加到classpath中,三個jar包。
import java.sql.*;
public class DbTest
{
Connection con;
Statement sta;
ResultSet rs;
String driver;
String url;
String user;
String pwd;
public DbTest()
{
driver = "sun.jdbc.odbc.JdbcOdbcDriver";
url = "jdbc:odbc:store_manager";
user = "share";
pwd = "share";
init();
}
public void init()
{
try{
rName(driver);
intln("driver is ok");
con = tConnection(url,user,pwd);
intln("conection is ok");
sta = eateStatement();
rs = sta.executeQuery("select * from room");
while( xt())
intln( tInt("roomNum"));
}
catch(Exception e)
{
intStackTrace();
}
}
public static void main(String[] args)
{
new DbTest();
}
}

㈥ java如何連接SQLserver資料庫

從M$網站下載最新JDBC驅動或都使用maven:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>9.4.1.jre11</version>
</dependency>

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class SQLDatabaseConnection {

// Connect to your database.
// Replace server name, username, and password with your credentials
public static void main(String[] args) {
String connectionUrl =
"jdbc:sqlserver://yourserver.database.windows.net:1433;"
+ "database=AdventureWorks;"
+ "user=yourusername@yourserver;"
+ "password=yourpassword;"
+ "encrypt=true;"
+ "trustServerCertificate=false;"
+ "loginTimeout=30;";

String insertSql = "INSERT INTO SalesLT.Proct (Name, ProctNumber, Color, StandardCost, ListPrice, SellStartDate) VALUES "
+ "('NewBike', 'BikeNew', 'Blue', 50, 120, '2016-01-01');";

ResultSet resultSet = null;

try (Connection connection = DriverManager.getConnection(connectionUrl);
PreparedStatement prepsInsertProct = connection.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS);) {

prepsInsertProct.execute();
// Retrieve the generated key from the insert.
resultSet = prepsInsertProct.getGeneratedKeys();

// Print the ID of the inserted row.
while (resultSet.next()) {
System.out.println("Generated: " + resultSet.getString(1));
}
}
// Handle any errors that may have occurred.
catch (Exception e) {
e.printStackTrace();
}
}
}

㈦ JAVA 查詢SQL資料庫的問題

phone.setText(strSQL);你看你這里就是將"select PhoenNumber from Friend2 where Name= '"+Name+"'";賦值給setText了,
要phone.setText(rs.getObject("PhoenNumber"));

㈧ 如何用java sqlserver資料庫

打開Microsoft
SQL
Server
Managerment
Studio並以windows驗證方式登錄,左側的對象
資源管理器
->安全性->
登錄名
,
右擊
sa->屬性,為sa用戶添加密碼,選擇
sqlServer
身份驗證
,在"狀態"選項中授予連接到資料庫和登錄啟用.
右擊對象資源管理器的根節點,選擇屬性->安全性->sqlServer和windows身份驗證模式,這樣就為sql
server
2008創建了以sql
server身份驗證的用戶sa.
在java代碼中用兩種方式連接
sqlserver2008
資料庫,一種是sa身份驗證模式,另外一種是混合身份驗證模式

㈨ java如何連接SQLserver資料庫

注意:在使用這個類的時候,先將對應資料庫的驅動包(JAR包),復制進項目的WebRoot文件夾下的WEB-INF文件夾下的lib文件夾下,切記必須要對應的JAR包,否則無法使用資料庫的
import java.sql.*;
public class BaseDAO {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";//注意:此驅動是SQL2005及以上版本的導入驅動包連接字元串
private static final String CONNECTION = "jdbc:sqlserver://localhost:1433;databaseName=Employee"; //資料庫連接字元串,databaseName就是你要連接的資料庫名,
private static final String NAME = "sa"; //資料庫用戶名
private static final String PWD = "sa"; //資料庫密碼
public static Connection GetConnection() {
Connection con = null;
try {
Class.forName(DRIVER);
con = DriverManager.getConnection(CONNECTION, NAME, PWD);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return con;
}

public static void close(ResultSet rs, PreparedStatement ps, Connection con) {
try {
if (null != rs) {
rs.close();
}
if (null != ps) {
ps.close();
}
if (null != con) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

㈩ java怎麼連接sqlserver資料庫

java中使用jdbc連接sql server資料庫步驟:
1.JDBC連接SQL Server的驅動安裝 ,前兩個是屬於資料庫軟體,正常安裝即可(注意資料庫登陸不要使用windows驗證)
<1> 將JDBC解壓縮到任意位置,比如解壓到C盤program files下面,並在安裝目錄里找到sqljdbc.jar文件,得到其路徑開始配置環境變數
在環境變數classpath 後面追加 C:\Program Files\Microsoft SQL Server2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar
<2> 設置SQLEXPRESS伺服器:
a.打開SQL Server Configuration Manager -> SQLEXPRESS的協議 -> TCP/IP
b.右鍵單擊啟動TCP/IP
c.雙擊進入屬性,把IP地址中的IP all中的TCP埠設置為1433
d.重新啟動SQL Server 2005服務中的SQLEXPRESS伺服器
e.關閉SQL Server Configuration Manager
<3> 打開 SQL Server Management Studio,連接SQLEXPRESS伺服器, 新建資料庫,起名字為sample
<4> 打開Eclipse
a.新建工程-> Java -> Java project,起名為Test
b.選擇eclipse->窗口->首選項->Java->installed JRE 編輯已經安裝好的jdk,查找目錄添加sqljdbc.jar
c.右鍵單擊目錄窗口中的Test, 選擇Build Path ->Configure Build Path..., 添加擴展jar文件,即把sqljdbc.jar添加到其中
<5> 編寫Java代碼來測試JDBC連接SQL Server資料庫
import java.sql.*;
public class Test {
public static void main(String[] srg) {
//載入JDBC驅動
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
//連接伺服器和資料庫sample
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=sample";
String userName = "sa"; //默認用戶名
String userPwd = "123456"; //密碼

Connection dbConn;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
System.out.println("Connection Successful!"); //如果連接成功 控制台輸出
} catch (Exception e) {
e.printStackTrace();
}
}
}

執行以後就可以連接到sample資料庫了。