import java.sql.*;
public class Demo8 {
public static void main(String[] srg) {
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver"; //加载JDBC驱动
String dbURL = "jdbc:sqlserver://localhost:1433; DatabaseName=wyc"; //连接服务器和数据库sample
String userName = "sa"; //默认用户名
String userPwd = "wyc"; //密码
Connection dbConn;
PreparedStatement ps = null;
ResultSet rs = null;
try {
Class.forName(driverName);
dbConn = DriverManager.getConnection(dbURL, userName, userPwd);
//Statement sm = dbConn.createStatement();
String sql = "select name,age from wo";
ps=dbConn.prepareStatement(sql);
rs = ps.executeQuery();// 执行查询
while(rs.next()){
String name=rs.getString("name");//使用字段名也可以取出数据
int age=rs.getInt("age");


System.out.println(name+"\t"+age);
}


System.out.println( "Connection Successful! "); //如果连接成功 控制台输出Connection Successful!
} catch (Exception e) {
e.printStackTrace();
}
}
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
  • 2021-12-03
  • 2021-12-13
  • 2021-08-12
  • 2022-02-05
  • 2022-01-02
猜你喜欢
  • 2021-08-02
  • 2022-01-31
  • 2021-12-26
  • 2021-12-10
  • 2021-07-27
  • 2022-12-23
相关资源
相似解决方案