CODE:-
LibDb.java
package libdb;
import java.sql.*;
public class LibDb {
static final String JdbcDr = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
static final String url = "jdbc:sqlserver://localhost:1433;databaseName=kiran";
static final String user = "sa";
static final String pass = "123456";
public static void main(String[] args) {
Connection conn = null;
Statement stmt = null;
try{
Class.forName(JdbcDr);
System.out.println("Connecting to database....");
conn = DriverManager.getConnection(url,user,pass);
System.out.println("Successfully connected to database.");
System.out.println("Creating table...");
stmt = conn.createStatement();
String sql = "create table librarian"+
"(id integer primary key,"+
"name varchar(20),"+
"password varchar(20),"+
"email varchar(20),"+
"address varchar(25),"+
"city varchar(20),"+
"contact varchar(20))";
stmt.executeUpdate(sql);
System.out.println("Table created successfully...");
}
catch(Exception e){
e.printStackTrace();
}
finally{
try{
if(conn!=null){
conn.close();
}
}
catch(SQLException se){
se.printStackTrace();
}
}
System.out.println("Done.");
}
}
output:
0 Comments