赞
踩
package shujuku; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; public class shuju { //注册驱动 private static final String diverName="com.microsoft.sqlserver.jdbc.SQLServerDriver"; //数据库的地址 private static final String dbURL="jdbc:sqlserver://localhost:1433;databaseName=COrdersDB"; //数据库登录用户户名 private static final String userName="sa"; //数据库用户密码 private static final String userPwd="XXXXXXX"; //数据库连接 private static Connection conn=null; //数据库操作 private static Statement stmt=null; // private static ResultSet rs=null; // public static void main(String[] args) { // TODO Auto-generated method stub try { Class.forName(diverName); //Class.forName("com.mysql.jdbc.Driver"); conn=DriverManager.getConnection(dbURL, userName, userPwd); System.out.println(conn); stmt=conn.createStatement(); rs=stmt.executeQuery("select *from product"); while(rs.next()){ String productid=rs.getString(1); String pname=rs.getString(2); String punit=rs.getString(3); String unitstock=rs.getString(4); String stock=rs.getString(5); System.out.println("产品ID:"+productid); System.out.println("产品名称:"+pname); System.out.println("单位数量:"+punit); System.out.println("单价:"+unitstock); System.out.println("库存:"+stock); System.out.println("-----------"); } stmt.close(); conn.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); System.out.println("加载驱动失败"); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。