0% found this document useful (0 votes)
22 views

Import Java - Sql.

a little java file

Uploaded by

4lenks46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Import Java - Sql.

a little java file

Uploaded by

4lenks46
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.sql.

*;
public class FirstExample2
{
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/collegee";
static final String USER = "root";
static final String PASS = "root123";

public static void main (String[] args)


{
Connection conn = null;
Statement stmt = null;

try
{
Class.forName(JDBC_DRIVER);
System.out.println("Connecting to Database......" + DB_URL);
conn = DriverManager.getConnection(DB_URL,USER,PASS);
System.out.println("Creating statement......");
stmt = conn.createStatement();
String sql;
sql = "SELECT sno, sname, sex, mark, rank FROM students";
ResultSet rs = stmt.executeQuery(sql);

while(rs.next())
{
int sno = rs.getInt("sno");
int mark = rs.getInt("mark");
String sname = rs.getString("sname");
String sex = rs.getString("sex");
String rank = rs.getString("rank");

System.out.println("Sno:"+sno);
System.out.println("SName:"+sname);
System.out.println("Mark:"+mark);
System.out.println("Sex:"+sex);
System.out.println("Rank:"+rank);

}rs.close();
} catch (SQLException se) {
se.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (stmt != null) stmt.close();
} catch (SQLException se2) {
se2.printStackTrace();
}
try {
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
System.out.println("Goodbye!");
}
}

You might also like