使用Java查询Sql Server数据库
生活随笔
收集整理的這篇文章主要介紹了
使用Java查询Sql Server数据库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
import java.sql.*;public class Connect
{static Connection con;static Statement stmt;public static void doConnect() throws SQLException{String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// SQL數據庫引擎String connectDB = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=JavaPattern";// 數據源try {Class.forName(JDriver);// 加載數據庫引擎,返回給定字符串名的類} catch (ClassNotFoundException e) {System.out.println("加載數據庫引擎失敗");System.exit(0);}System.out.println("數據庫驅動成功");try {String user = "sa";String password = "huxiang";con = DriverManager.getConnection(connectDB, user, password);// 連接數據庫對象System.out.println("連接數據庫成功");stmt = con.createStatement();} catch (SQLException e) {e.printStackTrace();System.out.println("數據庫連接錯誤");System.exit(0);}ResultSet rs = stmt.executeQuery("SELECT * FROM Student ");while (rs.next()) {System.out.println(rs.getString("id") + "\t"+ rs.getString("password"));}System.out.println("讀取完畢");stmt.close();con.close();System.out.println("數據庫關閉成功!");}public static void main(String[] args) {try {doConnect();} catch (SQLException e) {e.printStackTrace();}}
}
總結
以上是生活随笔為你收集整理的使用Java查询Sql Server数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 吃减肥药真的可以减肥吗
- 下一篇: 滋水清肝饮的作用