java当中JDBC当中请给出一个sql server的hellowo…
2019-10-13 11:06:50来源:博客园 阅读 ()
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
原文链接:https://www.cnblogs.com/haima1949/p/11667720.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash