java当中JDBC当中请给出一个sql server的helloworld例子

1.sql server的helloworld例子:

import java.sql.*;

public class JdbcHelloSqlServer {
  public static void main(String[] args) {
    String dbUrl= "jdbc:jtds:sqlserver://localhost:1433/Northwind";String user = "sa";String password = "1234";
//    String   dbUrl="jdbc:mysql://localhost:3306/test"; String user = "root";String password = "1234";
    String createString;
    Statement stmt;
    createString = "select * from orders";
    try {
      /*this method attempts to locate, load,
       and link the class or interface. The specified class loader is
       used to load the class or interface. If the parameter loader is
       null, the class is loaded through the bootstrap class loader.
       about soundBlaster driver?when you talk to the microphone, it can
        record into the computer and save as a file. your sound is
       transfered into 01 signal and save as a file. all these are done by driver.
       the same thing,  using DB? you also need driver.
       */
       Class.forName("net.sourceforge.jtds.jdbc.Driver");
//       Class.forName("com.mysql.jdbc.Driver");
      /*      When the method
       getConnection is called, the DriverManager will attempt to
       locate a suitable driver from amongst those loaded at initialization
       */
      Connection con = DriverManager.getConnection(dbUrl, user, password);
      System.out.println("Connection is ok");
      /* Statement object is a workspace to create SQL queries, execute them, and retrieve any results that are returned.
 tube has track,vehicle is on the track for coal.  vehicle is only suitable for  this track , not for any other track.
       */
      stmt = con.createStatement();
// stmt.execute(createString);
      /* Resultset object contains the table produced by the SQL query
       */
      ResultSet rs = stmt.executeQuery(createString);
      System.out.println("qixybefore");
      //获得元数据
      //获得元数据
      ResultSetMetaData meta = rs.getMetaData();
      for (int i = 1; i <= meta.getColumnCount(); i++) {
        if (meta.getColumnType(i) == java.sql.Types.INTEGER) { // if it is CHAR
          System.out.println("Types.INTEGER is " +i +" " + meta.getColumnName(i)); // display the name
        }

        System.out.println(meta.getColumnName(i)); //字段名称
      }
      System.out.println("qixylater");
/*boolean next()  throws SQLException  Moves the cursor down one row from its current position. A ResultSet cursor is initially
 positioned before the first row; the first call to the method next makes the first row the current row; the second call makes
 the second row the current row, and so on.  Returns:true if the new current row is valid; false if there are no more rows */
      while (rs.next()) {

更多请见:https://blog.csdn.net/qq_44594249/article/details/100764762

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mark_to_win

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值