「java怎么连接sqlserver」Java怎么连接数据库
今天给各位分享java怎么连接sqlserver的知识,其中也会对Java怎么连接数据库进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、怎么用java连接sqlserver数据库
- 2、java如何连接SQLserver数据库?
- 3、java连接数据库的代码
- 4、JAVA如何连接到sqlserver
- 5、如何用java 连接 sqlserver 数据库
怎么用java连接sqlserver数据库
导入SqlServer JDBC的驱动,
SQLServer的JDBC URL=
jdbc:sqlserver://172.30.202.21:1433;DatabaseName=AirAutoMonitor
3. 获得连接的代码
public static Connection getConnection(String url, String username, String password)
throws ResourceDirectoryException {
Connection conn = null;
String driverName = "";
Properties props = new Properties();
props.put("user", username);
props.put("password", password);
if (url != null || !"".equals(url)) {
if (url.indexOf("oracle") -1) {
databaseType = "oracle";
props.put("remarksReporting", "true");
driverName = "oracle.jdbc.driver.OracleDriver";
}
if (url.indexOf("sqlserver") -1) {
databaseType = "sqlserver";
driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
}
if (url.indexOf("mysql") -1) {
databaseType = "mysql";
driverName = "com.mysql.jdbc.Driver";
}
}
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, props);
} catch (ClassNotFoundException e) {
throw new ResourceDirectoryException(e);
} catch (SQLException e) {
throw new ResourceDirectoryException(e);
}
return conn;
}
上面的代码是获得Oracle, MySQL, SqlServer的数据库连接的通用方法。
java如何连接SQLserver数据库?
从M$网站下载最新JDBC驱动或都使用maven:
dependency
groupIdcom.microsoft.sqlserver/groupId
artifactIdmssql-jdbc/artifactId
version9.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.Product (Name, ProductNumber, Color, StandardCost, ListPrice, SellStartDate) VALUES "
+ "('NewBike', 'BikeNew', 'Blue', 50, 120, '2016-01-01');";
ResultSet resultSet = null;
try (Connection connection = DriverManager.getConnection(connectionUrl);
PreparedStatement prepsInsertProduct = connection.prepareStatement(insertSql, Statement.RETURN_GENERATED_KEYS);) {
prepsInsertProduct.execute();
// Retrieve the generated key from the insert.
resultSet = prepsInsertProduct.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连接数据库的代码
package mysql;
import java.sql.*;
/**
* @author xys
*/
public class ConnectMysql {
public static Connection getConnection() throws ClassNotFoundException, SQLException {
String url = "jdbc:mysql://localhost:3306/databaseName";
String user = "mysqluser";
String password = "password";
String driverClass = "com.mysql.cj.jdbc.Driver";
Connection connection = null;
Class.forName(driverClass);
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (connection != null) {
System.out.println("数据库连接成功");
} else {
System.out.println("数据库连接失败");
connection.close();
}
return connection;
}
public void getResult() throws ClassNotFoundException, SQLException {
// 实例化 Statement 对象
Statement statement = getConnection().createStatement();
// 要执行的 Mysql 数据库操作语句(增、删、改、查)
String sql = "";
// 展开结果集数据库
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
// 通过字段检索
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
// 输出数据
System.out.println("ID : " +id);
System.out.println("name :" + name);
}
// 完成后需要依次关闭
resultSet.close();
statement.close();
getConnection().close();
}
}
JAVA如何连接到sqlserver
你这个程序不是从数据库查询,是通过读取zhigong1.txt文件判断查询的啊
如果你要改成从数据库里查询要把以下代码替换掉:
FileInputStream come_in42=new FileInputStream("zhigong1.txt");
ObjectInputStream in42 =new ObjectInputStream(come_in42);
list=(LinkedList)in42.readObject();
in42.close();
替换为:
Connection databaseConnect = null; // 数据库连接
Statement sqlServerStmt = null;
ResultSet sqlServerRset = null;
Statement ps = null;
String localDatabaseDriver = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String localDatabaseUrl =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=qs080521";//注:这里要写你要连接的数据库,把(qs080521)替换掉
try {
Class.forName(localDatabaseDriver);
databaseConnect = DriverManager.getConnection(localDatabaseUrl, "sa", "");
String DanJuHao = "";
sqlServerStmt = databaseConnect.createStatement();
String sqlStr = "";//写SQL查询语句
System.out.println(sqlStr);
sqlServerRset = ps.executeQuery(sqlStr);
while(sqlServerRset.next()){
Wage w = new Wage();
//用sqlServerRset.get...() 方法取出对应的数值
//w.set...();将上面语句放到括弧内,存储到相应字段
list.add(w);
}
sqlServerRset.close();
databaseConnect.close();
下面就什么也不用改了
如何用java 连接 sqlserver 数据库
本文将介绍使用java连接sqlserver数据库
工具/材料
myeclipse 、 SqlServer数据库
方法:
1、要向连接数据库,首先应该保证数据库服务打开
2、数据库服务打开之后就可以在环境中编写连接代码了。如图:
连接数据库就是这两个步骤:1)加载驱动、2)创建连接。
注意在导包是导入的java.sql下的。
接下来直接运行一下就可以测试是否连接成功了
关于java怎么连接sqlserver和Java怎么连接数据库的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-12-14,除非注明,否则均为
原创文章,转载请注明出处。