Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package sql2008;
- import java.sql.*;
- /**
- *
- * @author Sony
- */
- public class Sql2008 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Connection con = null;
- try
- {
- String userName="sa";
- String password="123456";
- String url="jdbc:sqlserver://localhost:1433;Instance=SQL2008;databaseName=demo;";
- Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
- con = java.sql.DriverManager.getConnection(url,userName,password);
- System.out.println("Da ket noi CSDL");
- java.sql.Statement st=con.createStatement();
- String sqlString ="";
- sqlString="UPDATE sinhvien SET ten='Abc' WHERE masv='01'";
- st.executeUpdate(sqlString);
- sqlString="INSERT INTO sinhvien VALUES(05,'manh','java')";
- st.executeUpdate(sqlString);
- sqlString="SELECT * FROM sinhvien";
- ResultSet rs=st.executeQuery(sqlString);
- while(rs.next())
- {
- System.out.print("masv:"+rs.getString(1)+" ");
- System.out.print("hodem:"+rs.getString(2)+" ");
- System.out.println();
- }
- }
- catch (Exception e)
- {
- System.err.println("khong ket noi duoc :"+e.getMessage());
- }
- finally
- {
- if(con!=null)
- {
- try
- {
- con.close();
- System.out.println("Dong ket noi");
- }
- catch(Exception e){}
- }
- }
- }
- // TODO code application logic here
- }
Add Comment
Please, Sign In to add comment