📄 jdbctest.java
字号:
package com.cric.onlineshop.util;
public class JdbcTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String userName = "sa";
String passWord = "sa";
String dbUrl = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=onlineShop";
java.sql.Connection conn = null;
java.sql.Statement stmt = null;
java.sql.ResultSet rs = null;
try {
Class.forName(driverName);
System.out.print("Driver OK!");
conn = java.sql.DriverManager.getConnection(dbUrl, userName,
passWord);
System.out.print("conn OK!");
stmt = conn.createStatement();
// String sqlStr="insert into users values('003','marry')";
// String sqlStr="update users set userName='marry' where
// userId='001'";
//String sqlStr = "delete users where userId='001'";
// stmt.execute(sqlStr);
String sqlstr1 = "select * from product";
rs = stmt.executeQuery(sqlstr1);
while (rs.next()) {
int pid=rs.getInt(1);
String pname = rs.getString("productname").trim();
System.out.println(pid + ":" + pname);
}
} catch (java.lang.ClassNotFoundException e) {
e.printStackTrace();
} catch (java.sql.SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null)
rs.close();
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
try {
if (stmt != null)
stmt.close();
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
try {
if (conn != null)
conn.close();
} catch (java.sql.SQLException e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -