?? dbconnection.java
字號:
package util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBConnection{
private static String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
private static String url="jdbc:sqlserver://localhost:1433:DataBaseName=jdbc_text";
private static String user="sa";
private static String password="";
private DBConnection(){
}
static{
try{
Class.forName(driverName);
}catch(ClassNotFoundException e){
e.printStackTrace();
throw new ExceptionInInitializerError(e.getMessage());
}
}
public static Connection getConnection() throws SQLException{
return DriverManager.getConnection(url,user,password);
}
public static void close(ResultSet rs,Statement st,Connection conn){
try{
if(rs!=null){rs.close();}
}catch(SQLException e){
e.printStackTrace();
}finally{
try{
if(st!=null){st.close();}
}catch(SQLException e){
e.printStackTrace();
}finally{
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
}
public static void main(String []arg) throws SQLException{
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try{
conn=DBConnection.getConnection();
String sql="insert into [user](name,password,email,age,birthday,money)"
+
"values('zhangsan','123','a@126.com',21,'1988-1-24',500.0)";
st=conn.createStatement();
st.executeUpdate(sql);
}finally{
DBConnection.close(rs,st,conn);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -