SN JAVA VB.
NET
1. Import java.sql.*; Using System.Data.SqlClient;
2. - Load the drviver Add necessary references
- Register the Driver
Class.ForName(“com.mysql.jdbc.Driver”); OR
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
3. Create connection Instantiate the connection
Connection con = SqlConnection conn = new
DriverManager.getConnection(“URL”,”UserName”,”Psw”) SqlConnection(
"Data
Connection SQlCnn = Source=(local);Initial
DriverManager.getConnection("jdbc:sqlserver://127.0.0.1; Catalog=Northwind;Integrated
databaseName= VBDtbthoby; user= User1; password= zxc"); Security=SSPI");
4. Create statement // 2. Open the connection
Statement st = con.Createstatement(); conn.Open();
Statement MyDBstmt = SQlCnn.createStatement(); // 3. Pass the connection
ResultSet MyRSet = MyDBstmt.executeQuery("Select * From to a command object
Tbthoby"); SqlCommand cmd = new
SqlCommand("select * from
Customers", conn);
5. Reade result set. // get query results
ResultSet rs = rs.ExecuteQuery(“Select * from table”) rdr =
While (rs.next()) cmd.ExecuteReader();
{rs.getInt(1);}
// print the CustomerID
ResultSet MyRSet = MyDBstmt.executeQuery("Select * From of each record
Tbthoby"); while (rdr.Read())
while( MyRSet.next()) { {
System.out.println(MyRSet.getString("Id") + " | " +
MyRSet.getString("Name")); Console.WriteLine(rdr[0]);
} }
MyRSet.close(); }
MyDBstmt.close();
SQlCnn.close();
6. Close statement // close the reader
St.close if (rdr != null)
Close connection {
Con.Close rdr.Close();
}
// 5. Close the
connection
if (conn != null)
{
conn.Close();
}